In the last NodeMCU WiFi development board project, I made an internet weather station where weather data (temperature, humidity, pressure, wind speed and wind degree) are sent serially to the laptop and displayed on SSD1306 OLED screen (128×64 Pixel). In that project the NodeMCU pulls whether data from weather website provides free API keys and JSON format openweathermap.org.
Before that I posted an other IoT project which shows how to build an internet clock using the NodeMCU board. The SSD1306 display was also used to display local time and date.
This project is just a combination of the previous two ones, it shows how to make an internet clock with local time and weather station. All data will be displayed on the SSD1306 OLED display.
Related Projects:
ESP8266 NodeMCU interfacing with SSD1306 OLED
NodeMCU Internet clock with SSD1306 display | IoT Projects
NodeMCU Internet weather station with SSD1306 OLED | IoT Projects
The SSD1306 OLED used in this project is configured to work in I2C mode, some SSD1306 OLED boards may require a small hardware modifications (to select between SPI mode or I2C mode) such as soldering, placing jumpers ….
Hardware Required:
- ESP8266 NodeMCU development board
- SSD1306 OLED display with 128×64 Pixel resolution
- micro USB cable (for programming and powering the circuit)
- Breadboard
- Jumper wires
NodeMCU Internet clock and weather station circuit:
Project circuit schematic diagram is shown below.
And the following image shows fritzing circuit:
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.
Reset pin (RES) of the display module is connected to GPIO5 (D1) of the NodeMCU development board.
The SSD1306 display module is supplied with 3.3V which comes from the NodeMCU board.
NodeMCU Internet clock and weather station code:
Project code uses some libraries which we have to install, download links are below:
The first library is NTPClient (NTPClient.h), this library connects the ESP8266 WiFi to a time server, this server sends time information to the module. NTP: Network Time Protocol.
NTPClient Library — direct link
The 2nd library is Arduino Time library (TimeLib.h), this library converts Unix timestamp (Unix epoch) into: seconds, minutes, hours, day of the week, day, month and year. The Unix epoch is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT).
Basically the time server sends time in Unix epoch format which needs to be converted, this library does all the job. Download links are below:
Arduino Time Library — direct link
The 3rd library is ArduinoJson library (ArduinoJson.h), we can install 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).
To install Adafruit graphics library (Adafruit_GFX.h) and Adafruit SSD1306 OLED driver (Adafruit_SSD1306.h), see the following post:
ESP8266 NodeMCU interfacing with SSD1306 OLED
The other are built-in libraries including: ESP8266WiFi.h, WiFiUdp.h, ESP8266HTTPClient.h and Wire.h.
The weather (of London, United Kingdom) is updated every 1 minute, for that I used the following:
1 2 3 4 5 6 7 | if (last_minute != minute_) // update weather every 1 minute { . . . last_minute = minute_; } |
Full code:
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | /************************************************************************************** ESP8266 NodeMCU WiFi Internet clock and weather station and with SSD1306 OLED display This is a free software with NO WARRANTY. http://simple-circuit.com/ ***************************************************************************************/ // ESP8266 WiFi main library #include <ESP8266WiFi.h> // Libraries for internet time #include <WiFiUdp.h> #include <NTPClient.h> // include NTPClient library #include <TimeLib.h> // include Arduino time library // Libraries for internet weather #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 = "DJAWEB"; const char *password = "ABCdef0123456789uvwXYZ@*123*@"; WiFiUDP ntpUDP; // 'time.nist.gov' is used (default server) with +1 hour offset (3600 seconds) 60 seconds (60000 milliseconds) update interval NTPClient timeClient(ntpUDP, "time.nist.gov", 3600, 60000); // set location and API key String Location = "London,uk"; String API_Key = "1041444a18cfb8448343254a45721b1d"; 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, 0x3D); // 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.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(); timeClient.begin(); delay(1000); } char Time[] = " : : "; char Date[] = " - -20 "; byte last_second, last_minute, second_, minute_, hour_, wday, day_, month_, year_; void loop() { if (WiFi.status() == WL_CONNECTED) // check WiFi connection status { timeClient.update(); unsigned long unix_epoch = timeClient.getEpochTime(); // get UNIX Epoch time second_ = second(unix_epoch); // get seconds from the UNIX Epoch time if (last_second != second_) // update time & date every 1 second { minute_ = minute(unix_epoch); // get minutes (0 - 59) hour_ = hour(unix_epoch); // get hours (0 - 23) wday = weekday(unix_epoch); // get minutes (1 - 7 with Sunday is day 1) day_ = day(unix_epoch); // get month day (1 - 31, depends on month) month_ = month(unix_epoch); // get month (1 - 12 with Jan is month 1) year_ = year(unix_epoch) - 2000; // get year with 4 digits - 2000 results 2 digits year (ex: 2018 --> 18) Time[7] = second_ % 10 + '0'; Time[6] = second_ / 10 + '0'; Time[4] = minute_ % 10 + '0'; Time[3] = minute_ / 10 + '0'; Time[1] = hour_ % 10 + '0'; Time[0] = hour_ / 10 + '0'; Date[9] = year_ % 10 + '0'; Date[8] = year_ / 10 + '0'; Date[4] = month_ % 10 + '0'; Date[3] = month_ / 10 + '0'; Date[1] = day_ % 10 + '0'; Date[0] = day_ / 10 + '0'; display.setCursor(0, 0); display.print("Time:"); display.setCursor(60, 0); display.print(Time); // display time (format: hh:mm:ss) display.setCursor(0, 11); display_wday(); display.print(Date); // display date (format: dd-mm-yyyy) display.display(); last_second = second_; } if (last_minute != minute_) // update weather every 1 minute { 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 ° 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 last_minute = minute_; } delay(200); } } void display_wday() { switch(wday) { case 1: display.print("SUNDAY "); break; case 2: display.print("MONDAY "); break; case 3: display.print("TUESDAY "); break; case 4: display.print("WEDNESDAY "); break; case 5: display.print("THURSDAY "); break; case 6: display.print("FRIDAY "); break; default: display.print("SATURDAY "); } } // End of code. |
NodeMCU Internet clock and weather station video:
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
Compilation error: ‘second’ was not declared in this scope; did you mean ‘second_’?
What do i now?
This project helped me to understand several protocols and now I have a wider range to use this esp8266.
After a few changes on the code, update a few more things and choose a smaller breakout board, I have finished the ESP-01, SDD1306 and a small module for 3.3v step down.
https://www.facebook.com/ruben.lomeli.54/posts/pfbid0Wstrp7FUkLAR6bHHLR4B9vSHKGb8xGEhXnS5aof221sx2zy5gpbVeB9Nh8dJV882l
Can you do the code with a display SH1106
I am trying to use a part of your example project with a clock that syncs over the internet (udp). I need the lib ‘WiFiUdp.h’. I can’t even find where to download. Can you help me out?
.