ESP Alarm Clock

by choimik26 in Circuits > Arduino

3 Views, 0 Favorites, 0 Comments

ESP Alarm Clock

IMG_1385.jpg
arduino.png

Welcome to this ESP Alarm Clock Tutorial. Follow the steps below to learn how to code and wire the LCD and ESP together to create an internet alarm clock which tracks time on its own RTC module and automatically sends requests when internet is available to update its time.

Supplies

LCD 16x2 Display

ESP32

Male to Female Connectors

Breadboard

RTC Module

USB C Cable Connector

Connecting ESP to LCD

IMG_1385.jpg

First, you will need to connect the ESP pins to the LCD pins.


// Create An LCD Object. Signals: [ RS, EN, D4, D5, D6, D7 ]

LiquidCrystal lcd(13, 12, 14, 27, 26, 25);


Notice how in the code snippet above we have assigned pins 13, 12, 14, 27, 26, 25 to respective pins of the LCD object to the ESP. These are the corresponding pins you will need to attach the female to male connectors in order for the code snippet above to work correctly. I suggest you follow the pin numbering above as it will be easier to use for the future. After attaching all these pins remember to attach the ground and 5V connectors to the breadboard to create a power source for the following pins. Finally, make sure to attach the potentiometer to vary the LCD screen's brightness.

Code the Arduino

arduino.png

You can copy and paste the code snippet below to get the ESP to connect to WiFi correctly and start updating time from the time servers online. Make sure to change the WiFi name and password to connect properly to your home network. When you are done copying and pasting make sure to have the correct setup to push the code into bytecode for your ESP. (ESP requires a special library download to do so linked below for another tutorial to do so ) ESP Tutorial Link


CODE BELOW:


#include <LiquidCrystal.h>

#include <WiFi.h>

#include "time.h"

#include "sntp.h"


#define WIFI_NETWORK "dlink"

#define WIFI_PASSWORD ""

#define WIFI_TIMEOUT_MS 20000


// Create An LCD Object. Signals: [ RS, EN, D4, D5, D6, D7 ]

LiquidCrystal lcd(13, 12, 14, 27, 26, 25);


const char* ntpServer1 = "pool.ntp.org";

const char* ntpServer2 = "time.nist.gov";

//Time difference between UTC and ET in seconds

const long UTCOffset_sec = -18000;

//The one hour on daylight savings time in seconds

const int daylightOffset_sec = 3600;

//Place a leap year offset for every Feb 29



unsigned long fetchElapsed = 0;


//struct to store time

struct tm timeinfo;

struct tm* timeinfoPtr = &timeinfo;


void connectToWiFi(){

Serial.println("Connecting to WiFi");

WiFi.mode(WIFI_STA);

WiFi.begin(WIFI_NETWORK, WIFI_PASSWORD);

unsigned long startAttemptTime = millis();




while(WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < WIFI_TIMEOUT_MS){

Serial.print(".");

delay(100);

}



if(WiFi.status() != WL_CONNECTED){

Serial.println("Failed");

//either reboot esp or deep sleep

}

else {

Serial.println("Connected");

Serial.println(WiFi.localIP());



//Print to lcd

lcd.clear();

lcd.print("Connected");

lcd.setCursor(0, 1);

lcd.print(WiFi.localIP());

}

}



void printLocalTime()

{

/*

//C++ already created object

if(!getLocalTime(&timeinfo)){

Serial.println("No time available (yet)");

return;

}

*/



//Display Date & Time

lcd.clear();

lcd.print(timeinfoPtr, "%B %d %Y"); //November 22 2022

lcd.setCursor(0,1);

lcd.print(timeinfoPtr, "%A,%H:%M:%S"); //Tuesday 10:30:05

}





//Function call for first fetching of time when alarm clock starts

void fetchTime(){

configTime(UTCOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2);

getLocalTime(&timeinfo);

}

/*

//function to call time and run on side if not work on the first time

void fetchTime(unsigned long startMillis){

}

*/



void setup() {

// Initialize The LCD. Parameters: [ Columns, Rows ]

lcd.begin(16, 2);

// Clears The LCD Display

lcd.clear();



//Serial.begin(115200);

connectToWiFi();

fetchTime();

printLocalTime();

}




void loop() {

delay(990);

if(fetchElapsed == 120){

fetchTime();

fetchElapsed = 0;

}

else{

timeinfoPtr->tm_sec++;

mktime(&timeinfo);

fetchElapsed++;

}

printLocalTime();

}