In this post I’m going to show how to build a simple internet weather station with ESP8266 (ESP-01) Wi-Fi module.
The ESP8266 can access the internet (web pages) and gets weather data from websites that provide free weather information for many cities over the world. In this project I’m going to show how to get weather data from the internet and print it on Arduino IDE serial monitor.
Before I start, if you want to see how to use the ESP-01 module for the first time, how to use it with Arduino IDE and how to program (upload sketches) it with Arduino or FT232RL module, visit the following topic:
ESP8266 WiFi module programming with Arduino UNO board
Hardware Required:
- ESP8266 ESP-01 WiFi module (or NodeMCU module)
- FT232RL USB-to-Serial converter or equivalent
- 3.3V source — or — 5V source and AMS1117-3V3 voltage regulator
- Breadboard
- Jumper wires
Internet weather station with ESP8266 module circuit:
IoT project circuit diagram is shown below.
(All grounded terminals are connected together)
The recommended operating voltage of the ESP8266 (ESP-01) module is 3.3V and I’ve a 5V power source which means I’ve to step-down the 5V into 3.3V, for that I used the AMS1117-3.3V (LM1117-3.3V) voltage regulator. There is no need for the voltage regulator if there is available 3.3V source with 1A or higher.
RX pin of the ESP-01 module is connected to TX pin of the FT232RL USB-to-Serial module.
TX pin of the ESP-01 module is connected to RX pin of the FT232RL USB-to-Serial module.
Internet weather station with ESP8266 module code:
To get weather data, I used openweathermap.org website, so first we’ve to sign up for a free account in order to get an API key which is important in this project. With the openweathermap.org website we can:
- Access current weather data for any location including over 200,000 cities
- Current weather is frequently updated based on global models and data from more than 40,000 weather stations
- Data is available in JSON, XML, or HTML format
- Available for Free and all other paid accounts
Once you sign in to your account (of course after the free registration), you’ll be directed to member area, go to API keys and you’ll find your API key as shown in the following image:
The API code is well shown, we’ll use it later.
With that key we can access weather data provided by the website, all what we do is entering the following URL into web browser (google Chrome, Mozilla Firefox …):
1 | http://api.openweathermap.org/data/2.5/weather?q=CITY,COUNTRY_CODE&APPID=YOUR_API_KEY |
Replace CITY by with the city you want weather data for, COUNTRY_CODE with the country code for that city (for example uk for the United Kingdom, us for the USA …) and YOUR_API_KEY with your API key which is shown above.
For example the weather in London, United Kingdom URL is (with my API key):
http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=1041444a18cfb8448343254a45721b1d
Accessing this URL with a web browser gives:
As mentioned above the openweathermap.org website provides weather data in JSON, XML, or HTML format. In this project we’ll use the JSON format which can be parsed using an Arduino library called ArduinoJSON.
Installing ArduinoJson library:
It’s easy to install the ArduinoJson library, we can do it by going to:
Arduino IDE –> Sketch –> Include Library –> Manage Libraries…
a new window will open, in the search box write: arduinojson
choose library version (I’m using version 5.13.2) and click on install.
We can install the library manually by downloading it from the following URL:
ArduinoJSON library
After the download, extract the zipped file and a folder with name ArduinoJSON-5.13.2 should appear. Move the folder to Arduino libraries folder, the folder should be renamed to ArduinoJSON (remove -5.13.2).
With a built-in library named: ESP8266HTTPClient we can read http pages, accessing a page is simple with this library, just by putting its URL as shown below where I placed the URL of London city weather:
1 | http.begin("http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=1041444a18cfb8448343254a45721b1d"); |
After that we’ve to read and save the whole JSON page as a string:
1 | String payload = http.getString(); //Get the request response payload |
And finally the ArduinoJSON library does its job and decodes the JSON string:
1 | JsonObject& root = jsonBuffer.parseObject(payload); |
the openweathermap.org website provides temperature in °K (degree Kelvin) and to convert the °K into °C (degree Celsius) we’ve to substruct 273.15 from it. For that I used the following line:
1 | float temp = (float)(root["main"]["temp"]) - 273.15; // get temperature |
Also, it gives the pressure in hPa (Hectopascal) with 1 hPa = 100 Pascal = 1/1000 bar:
1 | float pressure = (float)(root["main"]["pressure"]) / 1000; // get pressure |
Humidity is in %, wind speed in m/s (meters per second) and wind degree in degrees (°).
The following website provides a good help with JSON format:
ArduinoJson Assistant
Full Arduino code, don’t forget to replace YOUR SSID and YOUR WIFI PASSWORD:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | /************************************************************************************** Internet weather station using ESP8266 Wi-Fi module This is a free software with NO WARRANTY. http://simple-circuit.com/ ***************************************************************************************/ #include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> // http web access library #include <ArduinoJson.h> // JSON decoding library const char *ssid = "YOUR SSID"; const char *password = "YOUR WIFI PASSWORD"; void setup() { Serial.begin(115200); delay(500); Serial.print("Connecting."); WiFi.begin(ssid, password); // access Wi-FI point while ( WiFi.status() != WL_CONNECTED ) { delay(500); Serial.print("."); } Serial.println("connected\r\n"); } void loop() { if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status HTTPClient http; //Declare an object of class HTTPClient // specify request destination http.begin("http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=1041444a18cfb8448343254a45721b1d"); int httpCode = http.GET(); // send the request if (httpCode > 0) { // check the returning code String payload = http.getString(); //Get the request response payload DynamicJsonBuffer jsonBuffer(512); // Parse JSON object JsonObject& root = jsonBuffer.parseObject(payload); if (!root.success()) { Serial.println(F("Parsing failed!")); return; } float temp = (float)(root["main"]["temp"]) - 273.15; // get temperature int humidity = root["main"]["humidity"]; // get humidity float pressure = (float)(root["main"]["pressure"]) / 1000; // get pressure float wind_speed = root["wind"]["speed"]; // get wind speed int wind_degree = root["wind"]["deg"]; // get wind degree // print data Serial.printf("Temperature = %.2f°C\r\n", temp); Serial.printf("Humidity = %d %%\r\n", humidity); Serial.printf("Pressure = %.3f bar\r\n", pressure); Serial.printf("Wind speed = %.1f m/s\r\n", wind_speed); Serial.printf("Wind degree = %d°\r\n\r\n", wind_degree); } http.end(); //Close connection } delay(60000); // wait 1 minute } // End of code. |
The following image shows my serial monitor output:
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
how to response weather id? I can’t get weather id… 🙁