ESP32 project: display for weather forecast
How will the weather be tomorrow and the day after tomorrow? The classic weather station projects that you see in the scene can not say anything about it, because they only display and log the current temperature values. We choose a different approach here: With the ESP32 (https://az-delivery.de/products/esp32-developmentboard) we download the weather forecast for the next five days from https://openweathermap.org and show them on a small OLED display (https://az-delivery.de/products/0-96zolldisplay) on.
The project is implemented in three sections: (1) preparation of the hardware, (2) generation of an API key for OpenWeatherMap to access data, and (3) software for downloading and displaying the weather forecast.
- ESP32 (https://az-delivery.de/products/esp32-developmentboard) for downloading the weather forecast and controlling the display
- Display, e.g. https://az-delivery.de/products/0-96zolldisplay as a display for the weather data
The display is connected to the ESP32 with 3.3 V and GND. For the I²C connection, SCL are also connected to G22 and SDA to G21. The following figure shows the fully wired hardware.
Graphics can even be shown on the mini display. With the sample sketches from the library, these are just a few clicks away.
OpenWeatherMap is an IT company that provides weather data. On a small scale even free of charge. To get weather data you have to send HTTP requests to the OpenWeatherMap API (https://openweathermap.org/forecast5) send. This requires authentication using an API key. You can get your API key on the price page (https://openweathermap.org/price). For example, it looks like this:
If you now have the URL in the browser http://api.openweathermap.org/data/2.5/forecast?q=Karlsruhe,de&appid=a254362gegeg715dce096a37b32c17aa , you will get the weather forecast for Karlsruhe for the next five days. In the URL, replace Karlsruhe with the name of your city and the API key behind appid = through your.
The data returned is in JSON format. The attribute temp gives the predicted temperature in Kelvin for the time German on. The latter is a Unix timestamp, which is once again in readable form as dt_txt is sent along. Example for starting a response:
This section outlines how the program works. For a deeper understanding, take a look at the code (https://github.com/Simsso/Microcontroller-Examples/tree/master/ESP32/WeatherDisplay) essential. The project files do the following:
- WeatherDisplay.ino: Arduino-Sketch, Main program: Connects all components.
- OpenWeatherMapAPI.h: OpenWeatherMap API header file: Defines how the OpenWeatherMap can be accessed.
- OpenWeatherMapAPI.cpp: OpenWeatherMap API code file: implements the methods of the previous header file.
- OLEDScreen.h: OLED screen header file: Controls the OLED display and outputs the weather data graphically.
- WeatherForecastSample.cpp: Weather forecast sample: Class that represents a single weather forecast.
API access with the ESP32
In order to display the weather information, the ESP32 must receive the data listed above in part from the OpenWeatherMap API. Because of its length, the code for downloading and reading is in separate C ++ files that are included in the main ketch #include be involved. In order to be able to run the sample program, you must have the entire folder content of https://github.com/Simsso/Microcontroller-Examples/tree/master/ESP32/WeatherDisplay download and then open the .ino file with the Arduino IDE. The program downloads weather data at regular intervals (2 hours by default). These are shared with the ArduinoJson Library (https://github.com/bblanchon/ArduinoJson/) parsed, that is, read so that they can be used in the program.
To be able to put the code into operation you have to install the libraries
- Enter your WLAN SSID and password (file WeatherDisplay.ino),
- update the city for which a forecast is to be made (file WeatherDisplay.ino, variable query) and
- enter a valid OpenWeatherMap API key (file WeatherDisplay.ino, variable apiKey).
Display the weather forecast
The display that shows the downloaded weather data can be controlled, for example, with the Ai_Ardulib_SSD1306 library (https://github.com/acrobotic/Ai_Ardulib_SSD1306). At the time of publication of this post, this contained a bug that was fixed in this pull request: https://github.com/barbeque/Ai_Ardulib_SSD1306/tree/patch-1. To be on the safe side, download the code from the latter link.
In the current version, the WeatherDisplay program shows the weather forecast in plain form, i.e. without further formatting. You are cordially invited to prettify the display with cloud / sun graphics, for example. http://www.instructables.com/id/Display-Images-on-OLED-Screen-With-Arduino-ATtiny8/.
With this weather forecast - 13 ° C - playing around with the ESP32 is simply the best thing to do.