first, get NTP client library for ESP32, and save in Arduino library folder
https://randomnerdtutorials.com/esp32-ntp-client-date-time-arduino-ide/
Then set time, GMT +5.5 to view Sri Lanka time
Then upload the code and view serial monitor using Arduino IDE, that should indicate as follows
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
// Replace with your network credentials
const char* ssid = "Your SSD"; //
const char* password = "Your Password";
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
// Variables to save date and time
String formattedDate;
String dayStamp;
String timeStamp;
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Initialize a NTPClient to get time
timeClient.begin();
// Set offset time in seconds to adjust for your timezone, for example:
// GMT +1 = 3600
// GMT +8 = 28800
// GMT -1 = -3600
// GMT 0 = 0
timeClient.setTimeOffset(19800);
}
void loop() {
while(!timeClient.update()) {
timeClient.forceUpdate();
}
// The formattedDate comes with the following format:
// 2018-05-28T16:00:13Z
// We need to extract date and time
formattedDate = timeClient.getFormattedDate();
Serial.println(formattedDate);
// Extract date
int splitT = formattedDate.indexOf("T");
dayStamp = formattedDate.substring(0, splitT);
Serial.print("DATE: ");
Serial.println(dayStamp);
// Extract time
timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
Serial.print("HOUR: ");
Serial.println(timeStamp);
delay(1000);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Unix Time
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <TimeLib.h>
unsigned long offset_days = 3; // 3 days
unsigned long t_unix_date1, t_unix_date2;
void setup() {
Serial.begin(115200);
t_unix_date1 = 1564398600;
Serial.print("t_unix_date1: ");
Serial.println(t_unix_date1);
offset_days = offset_days * 86400; // convert number of days to seconds
t_unix_date2 = 1564398600 + offset_days;
Serial.print("t_unix_date2: ");
Serial.println(t_unix_date2);
printf("Date1: %4d-%02d-%02d %02d:%02d:%02d\n", year(t_unix_date1), month(t_unix_date1), day(t_unix_date1), hour(t_unix_date1), minute(t_unix_date1), second(t_unix_date1));
printf("Date2: %4d-%02d-%02d %02d:%02d:%02d\n", year(t_unix_date2), month(t_unix_date2), day(t_unix_date2), hour(t_unix_date2), minute(t_unix_date2), second(t_unix_date2));
}
void loop() {
}
No comments:
Post a Comment