IOT Weather Station with NodeMCU OLED:
In this Latest IoT project, we will learn how to make an IoT Based Simple Online Weather Station using ESP8266 NodeMCU (ESP-12E) Wi-Fi development board, and an SSD1306 OLED display (128×64 Pixel). This project is a wonderful project for IoT beginners who want to learn about retrieving data from an Online Internet server to the NodeMCU board.
The NodeMCU pulls weather data like temperature, humidity, pressure, wind speed and wind directional degree from weather website called openweathermap.org and display it on SSD1306 screen.
Check some of the weather station based projects here:
1. ESP8266 & BME280 Based Mini Weather Station
2. IoT Live Weather Station Monitoring with ESP8266
3. MQTT Weather Station with GSM
4. LoRa Based Wireless Weather Station
Bill of Materials
As the topic is clear, i.e IoT Weather Station with NodeMCU OLED & OpenWeatherMap, we just need OLED Display and NodeMCU wifi Module for this project. You can purchase these components from Amazon.
S.N. | Components Name | Quantity | Purchase Links |
---|---|---|---|
1 | NodeMCU ESP8266 Board | 1 | Amazon | AliExpress |
2 | 0.96" I2C OLED Display | 1 | Amazon | AliExpress |
3 | Connecting Wires | 10 | Amazon | AliExpress |
4 | Breadboard | 1 | Amazon | AliExpress |
Block Diagram – IOT Weather Station:
This is a simple block diagram for IoT Weather Station which simply explains how the System works.
The website openweathermap.org provides an online service for weather data and forecast. The data is uploaded to a cloud server. NodeMCU ESP8266 Wifi Module collects the data information online and downloads/retrieves the info like temperature, humidity, pressure, wind speed, and wind directional degree.
These data are displayed on 0.96″ SSD1306 OLED Display along with City.
Circuit Diagram & Connection:
The SDA and SCL lines of the I2C bus come from GPIO4 (D2) and GPIO0 (D3) of the NodeMCU board (respectively), they are connected to SDA and SCL (SCK) pins of the SSD1306 display module. The OLED display module is supplied with 5V from the Vin pin of the NodeMCU board.
Generating API from OpenWeatherMap:
OpenWeatherMap Access current weather data for any location including over 200,000 cities all over the world. Current weather is frequently updated from more than 40,000 weather stations. The data is available in JSON, XML, or HTML format. We will use the data in JSON format here.
Visit https://openweathermap.org and sign in by creating your account.
Create or Generate your API Key as it is needed for retrieving data.
Select the city and country code by entering your city name. Example: Jaipur, IN here Jaipur is a city, and IN is a country code for India. City name and country code need to be entered on the code.
Source Code/Programs:
Below is the code for IoT Weather Station with NodeMCU OLED & OpenWeatherMap. You need 3 different libraries for that, i.e Adafruit_GFX.h, Adafruit_SSD1306.h and ArduinoJson.h. You can get all these libraries from the Library Manager.
Enter the WIFI SSID, Password, and City Name with Country Code. Now you can upload this code to NodeMCU Board.
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
#include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> // http web access library #include <ArduinoJson.h> // JSON decoding library // Libraries for SSD1306 OLED display #include <Wire.h> // include wire library (for I2C devices such as the SSD1306 display) #include <Adafruit_GFX.h> // include Adafruit graphics library #include <Adafruit_SSD1306.h> // include Adafruit SSD1306 OLED display driver #define OLED_RESET 5 // define SSD1306 OLED reset at ESP8266 GPIO5 (NodeMCU D1) Adafruit_SSD1306 display(OLED_RESET); // set Wi-Fi SSID and password const char *ssid = "Your Wifi SSID"; const char *password = "Your Wifi Password"; // set location and API key String Location = "City Name, Country Code"; String API_Key = "Your API Key"; void setup(void) { Serial.begin(9600); delay(1000); Wire.begin(4, 0); // set I2C pins [SDA = GPIO4 (D2), SCL = GPIO0 (D3)], default clock is 100kHz // by default, we'll generate the high voltage from the 3.3v line internally! (neat!) display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64) // init done Wire.setClock(400000L); // set I2C clock to 400kHz display.clearDisplay(); // clear the display buffer display.setTextColor(WHITE, BLACK); display.setTextSize(1); display.setCursor(0, 0); display.println(" Internet Weather"); display.print(" Station - Jaipur"); display.display(); WiFi.begin(ssid, password); Serial.print("Connecting."); display.setCursor(0, 24); display.println("Connecting..."); display.display(); while ( WiFi.status() != WL_CONNECTED ) { delay(500); Serial.print("."); } Serial.println("connected"); display.print("connected"); display.display(); delay(1000); } 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=" + Location + "&APPID=" + API_Key); // !! 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 in °C int humidity = root["main"]["humidity"]; // get humidity in % float pressure = (float)(root["main"]["pressure"]) / 1000; // get pressure in bar float wind_speed = root["wind"]["speed"]; // get wind speed in m/s int wind_degree = root["wind"]["deg"]; // get wind degree in ° // 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); display.setCursor(0, 24); display.printf("Temperature: %5.2f C\r\n", temp); display.printf("Humidity : %d %%\r\n", humidity); display.printf("Pressure : %.3fbar\r\n", pressure); display.printf("Wind speed : %.1f m/s\r\n", wind_speed); display.printf("Wind degree: %d", wind_degree); display.drawRect(109, 24, 3, 3, WHITE); // put degree symbol ( ° ) display.drawRect(97, 56, 3, 3, WHITE); display.display(); } http.end(); //Close connection } delay(60000); // wait 1 minute } // End of code. |
13 Comments
Hello Sir,
The video shows you using a I2C oled, but the circuit diagram shows it’s a SPI oled. I am a bit confused as I made the connections using the circuit diagram and the oled doesn’t show anything. Please help.
Nice job but the code doesn’t work. Keeps giving not declared for this instance error on line 74 DynamicJsonBuffer jsonBuffer(512);
The Json libraries need to be v5.0-5.10. If you change it, I think it will work.
How do I change pressure to read in Hg (America unit) and F not C ?
After reviewing code, and data retruned from the serial console / terminal, the temperature values are never updated from OpenWeatherMap.
The heading for the Name of the weather station doesn’t scroll off the screen.
The temperature stays at a static value of -273.15
So, all we see on the OLED is the 2 above values.
Tried for many hours to troubleshoot why not able to get OpenWeatherMap data.
Yes. Wifi Connects
Yes. API key is valid
Note sure what is missing from the code, but if you can follow up with this comment, this would be a great Weather Station using ESP8266 and OLED, with a cool 3D printed case to house them both.
Hi. It doesn’t work with Hungarian location. What can I do?
I think the SCL is on GPIO5 (D1) not GPIO0 (D3) witch is FLASH. So, code line should be Wire.begin(4, 5);
The address in the code line 0x3C does not match the address in the comment 0x3D.
I wrote nonsense. The sketch above is ok.
Hello, have you ever updated the code for this project? Genesis Technology is just around the corner
This is an Awesome project, but the code needs new JSON strings.
Thanks, Eric
Io lo sto facendo su un esp8266-01 pero mi piacerebbe sapere cosa cambia nel codice? perché mi funziona pero non lo posso vedere ancora nella OLED 0.96 se mi potresti dare una mano con quello grazie mille.
The spi oled is converted to iic by moving a surface mouted resisistor on the back.
C:\Users\siddh\OneDrive\Desktop\sketch_jan15a\sketch_jan15a.ino: In function ‘void loop()’:
C:\Users\siddh\OneDrive\Desktop\sketch_jan15a\sketch_jan15a.ino:66:11: error: call to ‘HTTPClient::begin’ declared with attribute error: obsolete API, use ::begin(WiFiClient, url)
66 | http.begin(“http://api.openweathermap.org/data/2.5/weather?q=” + Location + “&APPID=” + API_Key); // !!
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
exit status 1
Compilation error: call to ‘HTTPClient::begin’ declared with attribute error: obsolete API, use ::begin(WiFiClient, url)
In board manager esp8266 2.7.4v install its work for me.
thanks.