Difference between revisions of "ESP32: NTPClient"
Jump to navigation
Jump to search
Onnowpurbo (talk | contribs) |
Onnowpurbo (talk | contribs) |
||
Line 11: | Line 11: | ||
− | + | Code | |
/********* | /********* | ||
− | + | Get Time NTP | |
− | |||
− | |||
*********/ | *********/ | ||
Line 24: | Line 22: | ||
// Replace with your network credentials | // Replace with your network credentials | ||
− | const char* ssid = " | + | const char* ssid = "o1"; |
− | const char* password = " | + | const char* password = "Dzaq1993!"; |
+ | char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; | ||
+ | const long utcOffsetInSeconds = 25200; | ||
+ | int hh, mm, ss; | ||
// Define NTP Client to get time | // Define NTP Client to get time | ||
WiFiUDP ntpUDP; | WiFiUDP ntpUDP; | ||
− | NTPClient timeClient(ntpUDP); | + | NTPClient timeClient(ntpUDP, "id.pool.ntp.org", utcOffsetInSeconds); |
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
void setup() { | void setup() { | ||
− | |||
Serial.begin(115200); | Serial.begin(115200); | ||
− | + | ||
− | + | WiFi.begin(ssid, password); | |
− | WiFi.begin(ssid, password); | + | while ( WiFi.status() != WL_CONNECTED ) { |
− | while (WiFi.status() != WL_CONNECTED) { | + | delay ( 500 ); |
− | delay(500); | + | Serial.print ( "." ); |
− | Serial.print("."); | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
timeClient.begin(); | timeClient.begin(); | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
+ | |||
void loop() { | void loop() { | ||
− | + | timeClient.update(); | |
− | + | hh = timeClient.getHours(); | |
− | + | mm = timeClient.getMinutes(); | |
− | + | ss = timeClient.getSeconds(); | |
− | + | Serial.print(daysOfTheWeek[timeClient.getDay()]); | |
− | + | Serial.print(", "); | |
− | + | Serial.print(hh); | |
− | Serial. | + | Serial.print(":"); |
− | + | Serial.print(mm); | |
− | + | Serial.print(":"); | |
− | + | Serial.println(ss); | |
− | + | // Serial.println(timeClient.getFormattedTime()); | |
− | Serial.print(" | ||
− | Serial. | ||
− | |||
− | |||
− | Serial. | ||
− | Serial.println( | ||
delay(1000); | delay(1000); | ||
} | } |
Revision as of 17:48, 24 March 2023
Download Include File
https://github.com/taranais/NTPClient/archive/master.zip
- Click here to download the NTP Client library. You should have a .zip folder in your Downloads
- Unzip the .zip folder and you should get NTPClient-master folder
- Rename your folder from NTPClient-master to NTPClient
- Move the NTPClient folder to your Arduino IDE installation libraries folder
- Finally, re-open your Arduino IDE
Code
/********* Get Time NTP *********/ #include <WiFi.h> #include <NTPClient.h> #include <WiFiUdp.h> // Replace with your network credentials const char* ssid = "o1"; const char* password = "Dzaq1993!"; char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; const long utcOffsetInSeconds = 25200; int hh, mm, ss; // Define NTP Client to get time WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "id.pool.ntp.org", utcOffsetInSeconds);
void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while ( WiFi.status() != WL_CONNECTED ) { delay ( 500 ); Serial.print ( "." ); } timeClient.begin(); } void loop() { timeClient.update(); hh = timeClient.getHours(); mm = timeClient.getMinutes(); ss = timeClient.getSeconds(); Serial.print(daysOfTheWeek[timeClient.getDay()]); Serial.print(", "); Serial.print(hh); Serial.print(":"); Serial.print(mm); Serial.print(":"); Serial.println(ss); // Serial.println(timeClient.getFormattedTime()); delay(1000); }