#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
// the name of the network (Wokwi-GUEST by default in wokwi simulator)
const char* ssid = "Wokwi-GUEST";
// the password of the network in this case it's an open network (no password)
const char* password = "";
// create a UDP object
WiFiUDP ntpUDP;
// create an NTP client object using UDP commuication protocol
NTPClient timeClient(ntpUDP);
void setup() {
Serial.begin(115200);
Serial.print("Connecting to ");
Serial.println(ssid);
// start the connection with specified ssid and password and channel
// channel in this case is 6 to saves about 4 seconds when connecting to wifi
WiFi.begin(ssid, password, 6);
// loop to ensure that the status of wifi is connected
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.print("IP address: (");
Serial.print(WiFi.localIP());
Serial.println(")\n");
// initializes the NTP client
timeClient.begin();
// sets the time offset to GMT +3 (the time zone for EGYPT)
timeClient.setTimeOffset(10800);
/*
GMT +1 = 3600 seconds
so!
GMT +3 = 10800 secodns
*/
}
void loop() {
// updates the time from the NTP server
timeClient.update();
// retrieves the current time formatted as a string (HH:MM:SS)
String time = timeClient.getFormattedTime(); // it's 24 hours format
Serial.print("the time right now is: ");
Serial.println(time);
delay(1000);
}