Difference between revisions of "ESP32: NTPClient"

From OnnoWiki
Jump to navigation Jump to search
 
 
(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
  
 
  /*********
 
  /*********
   Rui Santos
+
   Get Time NTP
  Complete project details at https://randomnerdtutorials.com
 
  Based on the NTP Client library example
 
 
  *********/
 
  *********/
 
   
 
   
 +
#include <Wire.h>
 +
#include <TM1650.h>
 +
#include <NTPClient.h>
 
  #include <WiFi.h>
 
  #include <WiFi.h>
#include <NTPClient.h>
 
 
  #include <WiFiUdp.h>
 
  #include <WiFiUdp.h>
 
   
 
   
 
  // Replace with your network credentials
 
  // Replace with your network credentials
  const char* ssid    = "Dosen_5Ghz";
+
  const char* ssid    = "o1";
  const char* password = "ITTStangsel2023";
+
  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);
 
// Variables to save date and time
 
String formattedDate;
 
String dayStamp;
 
String timeStamp;
 
 
   
 
   
 
  void setup() {
 
  void setup() {
  // Initialize Serial Monitor
 
 
   Serial.begin(115200);
 
   Serial.begin(115200);
  Serial.print("Connecting to ");
+
  Serial.println(ssid);
+
   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(".");
 
 
   }
 
   }
  // 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();
 
   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(3600);
 
 
  }
 
  }
 +
 
  void loop() {
 
  void loop() {
   while(!timeClient.update()) {
+
   timeClient.update();
    timeClient.forceUpdate();
+
  hh = timeClient.getHours();
   }
+
   mm = timeClient.getMinutes();
  // The formattedDate comes with the following format:
+
   ss = timeClient.getSeconds();
  // 2018-05-28T16:00:13Z
+
   Serial.print(daysOfTheWeek[timeClient.getDay()]);
   // We need to extract date and time
+
   Serial.print(", ");
  formattedDate = timeClient.getFormattedDate();
+
   Serial.print(hh);
   Serial.println(formattedDate);  
+
   Serial.print(":");
+
   Serial.print(mm);
   // Extract date
+
   Serial.print(":");
  int splitT = formattedDate.indexOf("T");
+
   Serial.println(ss);
   dayStamp = formattedDate.substring(0, splitT);
+
   // Serial.println(timeClient.getFormattedTime());
   Serial.print("DATE: ");
 
   Serial.println(dayStamp);
 
   // Extract time
 
  timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
 
   Serial.print("HOUR: ");
 
   Serial.println(timeStamp);
 
 
   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);
}