Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This sketch has been tested to work on both
- * ESP32 (Arduino Board Profile V2.0.8) and ESP8266 (Arduino Board Profile V3.1.2)
- *
- * It provide you with a simple NTP Clock working in the following order
- * > Connect to WiFi
- * > Set TimeZone and Get local local time (Note: local time is in tm elements format at this point)
- * > Do a "strftime" detailed time stamp
- * > Convert local time "tmelements" to a "Local Time Unix Epoch" value using "mktime"
- * > Use to set the Arduino "TimeLib.h" library
- * > Use "Local Time Unix Epoch" to "setTime()" in "TimeLib.h"
- * > Create a one second and one minute task
- * > Disconnect and turn off WiFi
- * > Task #1 Simple Custom Serial Print Time Stamp every second using TimeLib functions
- * > Task #2 On the minute rollover repeat the time sync routine
- *
- * Under normal use NTP time updates would only be needed a few times a day or even once a day
- *
- * See https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv for Timezone codes for your region
- *
- */
- #if defined (ESP32) // Auto detect ESP Chip type
- #include <WiFi.h>
- #elif defined (ESP8266)
- #include <ESP8266WiFi.h>
- #endif
- #include <sys/time.h> // C#/C++ time library
- #include <TimeLib.h> // Arduino Time Library
- #define TZ_INFO "NZST-12NZDT,M9.5.0,M4.1.0/3" // Timezone string
- const char* ntpServer = "pool.ntp.org"; // All that's needed to connect to NTP server pools
- const char* ssid = "Your_ssid";
- const char* password = "Your_password";
- uint8_t count = 0, syncMin, lastSec; // A few variables for time tasks
- time_t tmConvert_t(int YYYY, byte MM, byte DD, byte hh, byte mm, byte ss){
- tmElements_t tmSet;
- tmSet.Year = YYYY;
- tmSet.Month = MM; // This function makes our local epoch value
- tmSet.Day = DD;
- tmSet.Hour = hh;
- tmSet.Minute = mm;
- tmSet.Second = ss;
- return makeTime(tmSet);
- }
- void setMyClock(void){
- int yr, mo, dy, hr, mn, sc;
- time_t now = time(NULL);
- struct tm *now_tm;
- now_tm = localtime(&now);
- sc = now_tm->tm_sec;
- mn = now_tm->tm_min;
- hr = now_tm->tm_hour;
- yr = now_tm->tm_year -70;
- mo = now_tm->tm_mon +1;
- dy = now_tm->tm_mday;
- int32_t epoch = tmConvert_t(yr,mo,dy,hr,mn,sc);
- setTime(epoch);
- syncMin = minute();
- lastSec = second();
- Serial.println("Clock Set\n");
- return;
- }
- void getLocalTime(void){
- time_t now = time(nullptr);
- struct tm *now_tm;
- now = time(NULL);
- while(now_tm != localtime(&now)){now_tm = localtime(&now);}
- //Serial.println(now_tm, "%A, %B %d %Y %H:%M:%S [%Z]"); //This can be used on ESP32 if preferred
- char buffer [80];
- strftime (buffer,80,"%A, %B %d %Y %H:%M:%S [%Z]",now_tm);
- Serial.println(buffer);
- return;
- }
- void doWiFi(void){
- //connect to WiFi
- delay(200);
- Serial.printf("\nConnecting to %s ", ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println(" WiFi now Connected\n");
- getLocalTime();
- //disconnect WiFi as it's no longer needed
- WiFi.disconnect(true);
- WiFi.mode(WIFI_OFF);
- Serial.println("WiFi is Disconnected");
- }
- void myClock(void){
- char buff[50];
- sprintf(buff,"My Clock Time %02d:%02d:%02d Date: %02d/%02d/%d", hour(),minute(),second(),day(),month(),year());
- Serial.println(buff);
- Serial.println("");
- return;
- }
- void setup(){
- Serial.begin(115200);
- configTime(0, 0, ntpServer);
- setenv("TZ", TZ_INFO, 1);
- tzset();
- doWiFi();
- setMyClock();
- }
- void loop(){
- if(lastSec != second()){
- myClock();
- lastSec = second();
- if(syncMin != minute()){
- doWiFi();
- setMyClock();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment