IOT Based Analog/Digital OLED Clock using NodeMCU

IOT Based Analog Digital OLED Clock using NodeMCU (Last Updated On: February 24, 2019)

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 to any RTC Module like DS1307 or DS3231. Simply the time and date will be downloaded from 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





Components Required:

1. NodeMCU ESP8266 12E (Buy Online from Amazon)
2. SSD1306 0.96" OLED Display (Buy Online from Amazon)
3. Connecting Wires
4. Breadboard

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.

ECG Display using Pulse Sensor with OLED & Arduino

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 below Circuit diagram you can make IOT Based Analog/Digital OLED Clock using NodeMCU. So assemble the circuit as shown in the figure below.

Analog Digital OLED IOT Clock Circuit Diagram

Connect the SCL Pin of OLED to 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.




Working of the Project:

The Arduino IDE is used to develop a sketch that access a 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 web server. 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 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, configuration line is below:

configTime(timezone, dst, "pool.ntp.org","time.nist.gov");

Source Code/Program for Analog OLED Clock:

analog clock nodemcu

#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:

digital clock nodemcu oled

#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

}

Video Tutorial:

Recommended For You

About the Author: Alex Newton

Ask A Question .......

Close