ESP32: NTPClient

From OnnoWiki
Revision as of 19:30, 24 March 2023 by Onnowpurbo (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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);
}