An ESP Weather Monitor with OpenweatherMap

In this ESP tutorial you learn how to build a ESP weather monitor. A ESP82666 (or ESP32) gathers data from OpenWeatherMap and displays the received weather data.

The page is structured in 3 parts:

  • Registration for OpenWeatherMap
  • A first Test Program for OpenWeatherMap
  • Ideas for further development

A final sketch can be found at the end of the page in the links section.

 

Registration for OpenWeatherMap

OpenWeatherMap is an online service that provides weather data via an API. It includes current weather data, forecasts and historical data. In the example sketch you will use the JSON data format. To use this service you must register as user, apply for an API key and you must register for a billing plan.

After the user registration you will need to generate an API key. You can generate several API keys for different purposes. You can reuse one key for several ESPs

OpenWeatherMap API 3.0 key

Also the free services requires that you register a credit/debit card. The free limit is 1000 calls per day. Do avoid charging I recommend you set an additional limit "calls per day" to avoid charging if something gets wrong. For example you could set a low 200 calls limit. If you plan to request data once an hour you will need 24 requests per day. During your development phase you might need more calls.

OpenWeatherMap free billing plan

When you have applied for a billing plan, it might take several hours until the key is working. That will give you enough time to write an ESP program.

JSON data of OpenWeather API 3.0

The OpenWeather API 3.0 is described on their homepage. Here I give just some recommendations/learnings:

  • The output format JSON can be processed with the ArduinoJSON library. I recommend to use the latest version ArduinoJSON 7 for new developments.
  • OpenWeather provides lot of different data. I do some filtering in the sketch to make the payload smaller. Nevertheless you can activate other data also.
  • I don't use the minute and hourly forecast.
  • The sketch is configured that ArduinoJSON reads from the HTTPClient stream without an additional buffering.
  • You need to provide the coordinates of your town to get the appropriate data and you can set the basic units and your local language

A first Test Program for OpenWeatherMap

Purpose of the test sketch is to make the first requests working. It will output data to the Serial monitor. You should be able to adopt this for any kind of display or the output on a webpage.

The sketch is organized in several tabs:

  • openweather.h provides the API call to openweathermap
  • output.h outputs data to Serial. You can use this tab also to build your own output functions. For example for a LCD.
  • OTA.h activates the over the air update via the Arduino IDE

I decided for following program logic

loop()
calls weatherTimer()
weatherTimer()
checks if it is time to update weather information
weatherUpdate()
gathers data from the OpenWeather. The incoming stream from the HTTP Client gets forwarded in the JsonDocument doc.
The needed data from the JsonDocument will be copied to the global data structure weather.
serialOutput()
prints the currently stored data to serial.
outputTimer()
checks if it is time to output data. This is currently used to trigger the output to Serial.

Currently the sketch prints out the gathered weather data on serial:

current temp:  30.18
feels like:    30.15
pressure:      1012
humidity:      42
wind speed:    8.23
main:          Clear
description:   clear sky
summary:       Expect a day of partly cloudy with clear spells
alert_0_sender:GeoSphere Austria
event:         Heatwarning
description:   Increased heat stress must be expected.
tag:           Extreme high temperature

Ideas for further development

Add a LCD

To add a LCD you can program a function similar to serialOutput(). Instead of printing to the serial monitor you can print the data to your LCD.

Display Weather Data on Webserver

All weather data is kept in the global structure weather. You can easily reuse that data on webpages of your webserver.

Add more Weather Data

Currently only a subset of weather data is kept in the data structure weather. To add more data simply follow 3 steps:

  • add the needed variables to the data structure weather
  • add the additional variables in the function weatherUpdate()
  • add the additional variables to your output function

WiFi Setup and OpenWeather API 3.0 Setup

In the configuration of the main tab you need to provide your WiFi credentials or you can link to a file with your credentials.

//#include <credentials.h>      // if you have a config file with your credentials

#ifndef STASSID
#define STASSID "your-ssid"     // set your SSID
#define STAPSK "your-password"  // set your wifi password
#endif

Furthermore you need to provide the OpenWeather API key and the coordinates of your town:

constexpr char owKey[] {"ff1234567890aabbccddeeff"}; // register, subscribe for a plan and get an API key
constexpr float owLat {48.2083};                     // your latitude (North-South) - example for Vienna/Austria
constexpr float owLon {16.3731};                     // your longitude (West-East)

Summary

The example code shows how to get data from OpenWeatherMap and how to parse the JSON.

(*) Disclosure: Some of the links above are affiliate links, meaning, at no additional cost to you I will earn a (little) comission if you click through and make a purchase. I only recommend products I own myself and I'm convinced they are useful for other makers.

History

First upload: 2024-03-07 | Version: 2024-09-07