Difference between revisions of "WeMOS: D1 R1 mini NTP via Serial"

From OnnoWiki
Jump to navigation Jump to search
 
Line 48: Line 48:
 
   //Serial.println(timeClient.getFormattedTime());
 
   //Serial.println(timeClient.getFormattedTime());
 
   delay(1000);
 
   delay(1000);
 +
}
 +
 +
 +
 +
Alternatif Code
 +
 +
/*
 +
  Rui Santos
 +
  Complete project details at https://RandomNerdTutorials.com/esp8266-nodemcu-date-time-ntp-client-server-arduino/
 +
 
 +
  Permission is hereby granted, free of charge, to any person obtaining a copy
 +
  of this software and associated documentation files.
 +
 
 +
  The above copyright notice and this permission notice shall be included in all
 +
  copies or substantial portions of the Software.
 +
*/
 +
 +
#include <ESP8266WiFi.h>
 +
#include <NTPClient.h>
 +
#include <WiFiUdp.h>
 +
 +
// Replace with your network credentials
 +
// const char *ssid    = "REPLACE_WITH_YOUR_SSID";
 +
// const char *password = "REPLACE_WITH_YOUR_PASSWORD";
 +
const char *ssid    = "o1";
 +
const char *password = "Dzaq1993!";
 +
 +
// Define NTP Client to get time
 +
WiFiUDP ntpUDP;
 +
NTPClient timeClient(ntpUDP, "pool.ntp.org");
 +
 +
//Week Days
 +
String weekDays[7]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
 +
 +
//Month names
 +
String months[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
 +
 +
void setup() {
 +
  // Initialize Serial Monitor
 +
  Serial.begin(115200);
 +
 
 +
  // Connect to Wi-Fi
 +
  Serial.print("Connecting to ");
 +
  Serial.println(ssid);
 +
  WiFi.begin(ssid, password);
 +
  while (WiFi.status() != WL_CONNECTED) {
 +
    delay(500);
 +
    Serial.print(".");
 +
  }
 +
 +
// Initialize a NTPClient to get time
 +
  timeClient.begin();
 +
  // Set offset time in seconds to adjust for your timezone, for example:
 +
  // GMT +1 = 3600
 +
  // GMT +7 = 25200
 +
  // GMT +8 = 28800
 +
  // GMT -1 = -3600
 +
  // GMT 0 = 0
 +
  timeClient.setTimeOffset(25200);
 +
}
 +
 +
void loop() {
 +
  timeClient.update();
 +
 +
  time_t epochTime = timeClient.getEpochTime();
 +
  Serial.print("Epoch Time: ");
 +
  Serial.println(epochTime);
 +
 
 +
  String formattedTime = timeClient.getFormattedTime();
 +
  Serial.print("Formatted Time: ");
 +
  Serial.println(formattedTime); 
 +
 +
  int currentHour = timeClient.getHours();
 +
  Serial.print("Hour: ");
 +
  Serial.println(currentHour); 
 +
 +
  int currentMinute = timeClient.getMinutes();
 +
  Serial.print("Minutes: ");
 +
  Serial.println(currentMinute);
 +
   
 +
  int currentSecond = timeClient.getSeconds();
 +
  Serial.print("Seconds: ");
 +
  Serial.println(currentSecond); 
 +
 +
  String weekDay = weekDays[timeClient.getDay()];
 +
  Serial.print("Week Day: ");
 +
  Serial.println(weekDay);   
 +
 +
  //Get a time structure
 +
  struct tm *ptm = gmtime ((time_t *)&epochTime);
 +
 +
  int monthDay = ptm->tm_mday;
 +
  Serial.print("Month day: ");
 +
  Serial.println(monthDay);
 +
 +
  int currentMonth = ptm->tm_mon+1;
 +
  Serial.print("Month: ");
 +
  Serial.println(currentMonth);
 +
 +
  String currentMonthName = months[currentMonth-1];
 +
  Serial.print("Month name: ");
 +
  Serial.println(currentMonthName);
 +
 +
  int currentYear = ptm->tm_year+1900;
 +
  Serial.print("Year: ");
 +
  Serial.println(currentYear);
 +
 +
  //Print complete date:
 +
  String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay);
 +
  Serial.print("Current date: ");
 +
  Serial.println(currentDate);
 +
 +
  Serial.println("");
 +
 +
  delay(2000);
 
  }
 
  }

Latest revision as of 20:16, 3 April 2023

/*
   NTP Display via Serial
*/

#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char *ssid     = "o1";
const char *password = "Dzaq1993!";

const long utcOffsetInSeconds = 25200;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "asia.pool.ntp.org", utcOffsetInSeconds);

int hh, mm, ss;

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


Alternatif Code

/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp8266-nodemcu-date-time-ntp-client-server-arduino/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

// Replace with your network credentials
// const char *ssid     = "REPLACE_WITH_YOUR_SSID";
// const char *password = "REPLACE_WITH_YOUR_PASSWORD";
const char *ssid     = "o1";
const char *password = "Dzaq1993!";

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");

//Week Days
String weekDays[7]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

//Month names
String months[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

void setup() {
  // Initialize Serial Monitor
  Serial.begin(115200);
  
  // Connect to Wi-Fi
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  } 

// Initialize a NTPClient to get time
  timeClient.begin();
  // Set offset time in seconds to adjust for your timezone, for example:
  // GMT +1 = 3600
  // GMT +7 = 25200
  // GMT +8 = 28800
  // GMT -1 = -3600
  // GMT 0 = 0
  timeClient.setTimeOffset(25200);
}

void loop() {
  timeClient.update(); 

  time_t epochTime = timeClient.getEpochTime();
  Serial.print("Epoch Time: ");
  Serial.println(epochTime);
  
  String formattedTime = timeClient.getFormattedTime();
  Serial.print("Formatted Time: ");
  Serial.println(formattedTime);   

  int currentHour = timeClient.getHours();
  Serial.print("Hour: ");
  Serial.println(currentHour);   

  int currentMinute = timeClient.getMinutes();
  Serial.print("Minutes: ");
  Serial.println(currentMinute); 
   
  int currentSecond = timeClient.getSeconds();
  Serial.print("Seconds: ");
  Serial.println(currentSecond);   

  String weekDay = weekDays[timeClient.getDay()];
  Serial.print("Week Day: ");
  Serial.println(weekDay);    

  //Get a time structure
  struct tm *ptm = gmtime ((time_t *)&epochTime); 

  int monthDay = ptm->tm_mday;
  Serial.print("Month day: ");
  Serial.println(monthDay); 

  int currentMonth = ptm->tm_mon+1;
  Serial.print("Month: ");
  Serial.println(currentMonth); 

  String currentMonthName = months[currentMonth-1];
  Serial.print("Month name: ");
  Serial.println(currentMonthName); 

  int currentYear = ptm->tm_year+1900;
  Serial.print("Year: ");
  Serial.println(currentYear); 

  //Print complete date:
  String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay);
  Serial.print("Current date: ");
  Serial.println(currentDate); 

  Serial.println("");

  delay(2000);
}