Last ESP8266 NodeMCU project I made a simple real time clock with DS3231 RTC chip and SSD1306 OLED, time and date were displayed on the SSD1306 screen and they could be set with two push buttons.
With the NodeMCU WiFi development board we can use the internet to get an accurate local time, therefore there is no need for a real time clock chip such as: DS1302, DS1307, DS3231 …
This project shows how to build an internet clock with the NodeMCU WiFi module where time and date are displayed on SSD1306 OLED display.
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 ….
Related Projects:
ESP8266 NodeMCU interfacing with SSD1306 OLED
NodeMCU Internet clock and weather station | IoT Projects
Real time clock using NodeMCU, DS3231 and SSD1306 OLED
ESP8266 WiFi Internet real time clock
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 with SSD1306 display 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 with SSD1306 display code:
The Arduino IDE code below uses Adafruit SSD1306 OLED driver and Adafruit GFX library for the communication between the NodeMCU and the screen module.
The other library is NTPClient, 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 last one is Arduino Time library, 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 NTPClient library is configured to get time information (Unix epoch) from the server time.nist.gov (GMT time) and an offset of 1 hour ( ==> GMT + 1 time zone) which is equal to 3600 seconds, configuration line is below:
NTPClient timeClient(ntpUDP, “time.nist.gov”, 3600, 60000);
Rest of code is described through comments.
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 | /************************************************************************************** WiFi Internet clock (NTP) with ESP8266 NodeMCU (ESP-12E) and SSD1306 OLED display This is a free software with NO WARRANTY. http://simple-circuit.com/ ***************************************************************************************/ #include <ESP8266WiFi.h> #include <WiFiUdp.h> #include <NTPClient.h> // include NTPClient library #include <TimeLib.h> // include Arduino time library // Libraries for SSD1306 OLED display #include <Wire.h> // include wire library (for I2C devices such as 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); const char *ssid = "YOUR SSID"; const char *password = "YOUR PASSWORD"; 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); 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); draw_text(2, 0, "Wi-Fi Internet Clock", 1); display.display(); WiFi.begin(ssid, password); Serial.print("Connecting."); while ( WiFi.status() != WL_CONNECTED ) { delay(500); Serial.print("."); } Serial.println("connected"); timeClient.begin(); } char Time[] = " : : "; char Date[] = " - -20 "; byte last_second, second_, minute_, hour_, wday, day_, month_, year_; void loop() { 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_) { 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_wday(); draw_text(4, 29, Date, 2); // display date (format: dd-mm-yyyy) draw_text(16, 50, Time, 2); // display time (format: hh:mm:ss) display.display(); last_second = second_; } delay(200); } void display_wday() { switch(wday){ case 1: draw_text(40, 15, " SUNDAY ", 1); break; case 2: draw_text(40, 15, " MONDAY ", 1); break; case 3: draw_text(40, 15, " TUESDAY ", 1); break; case 4: draw_text(40, 15, "WEDNESDAY", 1); break; case 5: draw_text(40, 15, "THURSDAY ", 1); break; case 6: draw_text(40, 15, " FRIDAY ", 1); break; default: draw_text(40, 15, "SATURDAY ", 1); } } void draw_text(byte x_pos, byte y_pos, char *text, byte text_size) { display.setCursor(x_pos, y_pos); display.setTextSize(text_size); display.print(text); } // End of code. |
NodeMCU Internet clock with SSD1306 display video:
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
please change the code for lcd i2c display 20X4