IoT Based Analog/Digital OLED Clock using NodeMCU:
In this post, we will learn how to make IoT Based Analog/Digital OLED Clock using NodeMCU. There is no need for any RTC Module like DS1307 or DS3231. Simply the time and date will be downloaded from an online server. We can display the downloaded time either in Analog form or in digital form.
You can check our earlier post related to Clock:
1. Arduino & DS3231 Based Real Time Clock (RTC) & Temperature Monitor
2. Arduino DS3231 Based Real Time Clock with Alarm & Temperature Monitor
3. Arduino GPS Clock System
Components Required
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 |
0.96″ I2C OLED Display:
This is a 0.96 inch blue OLED display module. The display module can be interfaced with any microcontroller using SPI/IIC protocols. It is having a resolution of 128×64. The package includes display board, display,4 pin male header pre-soldered to board.
OLED (Organic Light-Emitting Diode) is a self light-emitting technology composed of a thin, multi-layered organic film placed between an anode and cathode. In contrast to LCD technology, OLED does not require a backlight. OLED possesses high application potential for virtually all types of displays and is regarded as the ultimate technology for the next generation of flat-panel displays.
Circuit Diagram & Connection:
With the help of the below Circuit diagram, you can make IoT Based Analog/Digital OLED Clock using NodeMCU. So assemble the circuit as shown in the figure below.
Connect the SCL Pin of OLED to the D1 Pin of NodeMCU.
Also, Connect the SDA pin of OLED to D2 Pin of NodeMCU.
Connect 3.3V & GND pin of OLED to 3.3V & GND Pin of NodeMCU.</p
Working of the Project:
The Arduino IDE is used to develop a sketch that accesses an NPT time server to accurately set the date and time. The date and time are then displayed on an OLED display and also via a web browser accessing the ESP8266 webserver. The web page allows the setting of options like Time Zone, 24-hour clock, daylight savings and a variable to control the time server update interval. The web page uses AJAX to update the time without reloading the entire web page.
The NTPClient library connects the ESP8266 WiFi to a time server, this server sends time information to the module. NTP: Network Time Protocol.
The last one is the 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.
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, the configuration line is below:
1 |
configTime(timezone, dst, "pool.ntp.org","time.nist.gov"); |
Source Code/Program for Analog OLED Clock:
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 |
#include <ESP8266WiFi.h> #include <time.h> #include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define OLED_RESET LED_BUILTIN //4 Adafruit_SSD1306 display(OLED_RESET); const char* ssid = "AlexHome"; const char* password = "hngzhowxiantan"; int ledPin = 13; int timezone = 7 * 3600; int dst = 0; #if (SSD1306_LCDHEIGHT != 64) #error("Height incorrect, please fix Adafruit_SSD1306.h!"); #endif void setup() { display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Clear the buffer. display.clearDisplay(); display.display(); pinMode(ledPin,OUTPUT); digitalWrite(ledPin,LOW); Serial.begin(115200); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0,0); display.println("Wifi connecting to "); display.println( ssid ); WiFi.begin(ssid,password); display.println("\nConnecting"); display.display(); while( WiFi.status() != WL_CONNECTED ){ delay(500); display.print("."); display.display(); } // Clear the buffer. display.clearDisplay(); display.display(); display.setCursor(0,0); display.println("Wifi Connected!"); display.print("IP:"); display.println(WiFi.localIP() ); display.display(); configTime(timezone, dst, "pool.ntp.org","time.nist.gov"); display.println("\nWaiting for NTP..."); while(!time(nullptr)){ Serial.print("*"); delay(1000); } display.println("\nTime response....OK"); display.display(); delay(1000); display.clearDisplay(); display.display(); } void loop() { time_t now = time(nullptr); struct tm* p_tm = localtime(&now); int r = 35; // Now draw the clock face display.drawCircle(display.width()/2, display.height()/2, 2, WHITE); // //hour ticks for( int z=0; z < 360;z= z + 30 ){ //Begin at 0° and stop at 360° float angle = z ; angle=(angle/57.29577951) ; //Convert degrees to radians int x2=(64+(sin(angle)*r)); int y2=(32-(cos(angle)*r)); int x3=(64+(sin(angle)*(r-5))); int y3=(32-(cos(angle)*(r-5))); display.drawLine(x2,y2,x3,y3,WHITE); } // display second hand float angle = p_tm->tm_sec*6 ; angle=(angle/57.29577951) ; //Convert degrees to radians int x3=(64+(sin(angle)*(r))); int y3=(32-(cos(angle)*(r))); display.drawLine(64,32,x3,y3,WHITE); // // display minute hand angle = p_tm->tm_min * 6 ; angle=(angle/57.29577951) ; //Convert degrees to radians x3=(64+(sin(angle)*(r-3))); y3=(32-(cos(angle)*(r-3))); display.drawLine(64,32,x3,y3,WHITE); // // display hour hand angle = p_tm->tm_hour * 30 + int((p_tm->tm_min / 12) * 6 ); angle=(angle/57.29577951) ; //Convert degrees to radians x3=(64+(sin(angle)*(r-11))); y3=(32-(cos(angle)*(r-11))); display.drawLine(64,32,x3,y3,WHITE); display.setTextSize(1); display.setCursor((display.width()/2)+10,(display.height()/2) - 3); display.print(p_tm->tm_mday); // update display with all data display.display(); delay(100); display.clearDisplay(); } |
Source Code/Program for Digital OLED Clock:
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 |
#include <ESP8266WiFi.h> #include <time.h> #include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define OLED_RESET LED_BUILTIN //4 Adafruit_SSD1306 display(OLED_RESET); const char* ssid = "AlexHome"; const char* password = "hngzhowxiantan"; int ledPin = 13; int timezone = 7 * 3600; int dst = 0; #if (SSD1306_LCDHEIGHT != 64) #error("Height incorrect, please fix Adafruit_SSD1306.h!"); #endif void setup() { display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Clear the buffer. display.clearDisplay(); display.display(); pinMode(ledPin,OUTPUT); digitalWrite(ledPin,LOW); Serial.begin(115200); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0,0); display.println("Wifi connecting to "); display.println( ssid ); WiFi.begin(ssid,password); display.println("\nConnecting"); display.display(); while( WiFi.status() != WL_CONNECTED ){ delay(500); display.print("."); display.display(); } // Clear the buffer. display.clearDisplay(); display.display(); display.setCursor(0,0); display.println("Wifi Connected!"); display.print("IP:"); display.println(WiFi.localIP() ); display.display(); configTime(timezone, dst, "pool.ntp.org","time.nist.gov"); display.println("\nWaiting for NTP..."); while(!time(nullptr)){ Serial.print("*"); delay(1000); } display.println("\nTime response....OK"); display.display(); delay(1000); display.clearDisplay(); display.display(); } void loop() { time_t now = time(nullptr); struct tm* p_tm = localtime(&now); Serial.print(p_tm->tm_mday); Serial.print("/"); Serial.print(p_tm->tm_mon + 1); Serial.print("/"); Serial.print(p_tm->tm_year + 1900); Serial.print(" "); Serial.print(p_tm->tm_hour); Serial.print(":"); Serial.print(p_tm->tm_min); Serial.print(":"); Serial.println(p_tm->tm_sec); // Clear the buffer. display.clearDisplay(); display.setTextSize(3); display.setTextColor(WHITE); display.setCursor(0,0); display.print(p_tm->tm_hour); display.print(":"); if( p_tm->tm_min <10) display.print("0"); display.print(p_tm->tm_min); display.setTextSize(2); display.setCursor(90,5); display.print("."); if( p_tm->tm_sec <10) display.print("0"); display.print(p_tm->tm_sec); display.setTextSize(1); display.setCursor(0,40); display.print(p_tm->tm_mday); display.print("/"); display.print(p_tm->tm_mon + 1); display.print("/"); display.print(p_tm->tm_year + 1900); display.display(); delay(1000); // update every 1 sec } |
4 Comments
hi, can you help to add a relay based on time with this script ?
Hi, fine tutorial. I use the analog clock.
But I get the error:
#error (“Height incorrect, please fix Adafruit_SSD1306.h!”);
I use adafruit_ssd1306 version 2.4.1
What version do you use.
Thanks
Dear Mr Alam . . . . I am making a few Analog Watches for famliy members for Christmas presents. In less than an hour I have an ESP8266 programmed, conncts to my internest fast . . . and displaying the clock face . . . after 20 seconds is connected to the time server for GMT (here in the UK) . . . . . but the image of the clock and date is upside down! How can I invert it in the software sketch???
Your help please . . . Thank you.
Steve/G4MJW
Trash that shketch contains many errors!