OpenWeatherMap forecast on LCD with Raspberry Pi, Java and Pi4J

Pi4J contains helper methods to minimize the work needed to use certain hardware modules on the Pi with Java. As an example, we will be using “GpioLcdDisplay” to control an LCD with 2 rows of 16 characters. After requesting the weather forecast from a public website, we will visualize this on the LCD display.


Not all these types of displays have completely the same connections, so you’ll need to check your display to setup the wiring like this:


The sources can be found as an example project within all the sources from the book on GitHub.

It all starts with a request to the OpenWeatherMap API to receive the forecast for a specific location.

public static String getForecast(String location, String appId) {
    StringBuilder rt = new StringBuilder();

    try {
        URL url = new URL(""
                + "?units=metric"
                + "&q=" + location
                + "&appid=" + appId);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        int responseCode = conn.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String readLine;
            while ((readLine = in.readLine()) != null) {
        } else {
            System.err.println("Wrong response code: " + responseCode);
    } catch (Exception ex) {
        System.err.println("Request error: " + ex.getMessage());
    return rt.toString();

By using jackson.databind we can convert the received Json from OpenWeatherMap to Java objects.

            "description":"broken clouds",

Each part of the Json data is parsed by a separate class in the example application. For example the description:


import com.fasterxml.jackson.annotation.JsonProperty;

public class WeatherDescription {
    public long id;

    public String main;

    public String description;

    public String icon;

Once we have the Forecast object, it becomes very easy to display this on the LCD.

private void showTemperatures() {
    System.out.println("Showing temperature " + forecast.weatherInfo.temperature);

    try {

        lcd.write(LCD_ROW_1, "Temp", 
        lcd.write(LCD_ROW_1, String.valueOf(forecast.weatherInfo.temperature), 
        lcd.write(LCD_ROW_2, "Min/Max " 
            + String.valueOf(forecast.weatherInfo.temperatureMinimum)
            + "/" 
            + String.valueOf(forecast.weatherInfo.temperatureMaximum), 
    } catch (Exception ex) {
        System.err.println("Error while handling content for the LCD: " 
            + ex.getMessage());

The full code is available in the GitHub repository and fully described in the book, including unit tests to validate the forecast request and parsing work as required.



For this example I wanted to show you how easy it is to get data from a public source and show it on the LCD. Of course you could also show a sensor measurement or a database value or … Again, your imagination is the only limit!