Difference between revisions of "ESP32: NTPClient"
Jump to navigation
Jump to search
Onnowpurbo (talk | contribs) (Created page with " →******** Rui Santos Complete project details at https://randomnerdtutorials.com Based on the NTP Client library example ********: #include <WiFi.h> #includ...") |
Onnowpurbo (talk | contribs) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | 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 <Wire.h> | ||
+ | #include <TM1650.h> | ||
+ | #include <NTPClient.h> | ||
#include <WiFi.h> | #include <WiFi.h> | ||
− | |||
#include <WiFiUdp.h> | #include <WiFiUdp.h> | ||
// 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); | ||
} | } |
Latest revision as of 19:30, 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 <Wire.h> #include <TM1650.h> #include <NTPClient.h> #include <WiFi.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); }