macca-nz

Press BTN to update network Time on ESP32

Aug 15th, 2023 (edited)
1,959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.21 KB | Source Code | 0 0
  1. //Sketch will only do an NTP time update when you press the FLASH/BOOT button on the ESP32
  2.  
  3. //On line 21 its worth getting an updated epoch value so when up compile and upload the system clock
  4. //will only be a couple of minutes slow https://www.epochconverter.com/
  5.  
  6. #define TZ_INFO "EST5EDT,M3.2.0,M11.1.0"          //EST/EDT  UTC -5/-4      
  7. //#define TZ_INFO "NZST-12NZDT,M9.5.0,M4.1.0/3"       //NZST/NZDT  UTC +12/+13
  8.  
  9. #define BTN 0
  10.  
  11. #include <WiFi.h>    
  12. time_t now;                                        
  13.  
  14. //SSID and Password
  15. const char* ssid       = "Your_ssid";
  16. const char* password   = "Your_password";
  17.  
  18. uint8_t lastSec;
  19. bool pressed = 0, lastBtnState = 1;
  20. uint32_t previousTime = 0;        
  21.  
  22. int32_t epoch = 1692574435;         //get current epoch value here https://www.epochconverter.com/
  23.  
  24. void updateTime(void){
  25.     uint8_t sec = 0;
  26.     now = time(NULL);
  27.     struct tm *now_tm;
  28.     now_tm = localtime(&now);
  29.     sec = now_tm->tm_sec;
  30.     if(lastSec != sec){
  31.             if(sec == 10 || sec == 20 || sec == 30 || sec == 40 || sec == 50){
  32.                 if(WiFi.status() != WL_CONNECTED){
  33.                     Serial.println(now_tm, "\n[WiFi is Disconnected]\n[UTC %z] %c\n");
  34.                 }else{
  35.                     Serial.println(now_tm, "\n[WiFi is Connected]\n[UTC %z] %c\n");
  36.                 }
  37.             }else{
  38.                 Serial.println(now_tm, "[WiFi is Disconnected]\n[%Z] Current Time %I:%M:%S [%p]");
  39.             }
  40.         lastSec = sec;
  41.     }
  42. }
  43.  
  44. void NTPupdate(void){
  45.     delay(500);
  46.     Serial.printf("\nConnecting to WiFi %s ", ssid);
  47.     WiFi.mode(WIFI_STA);
  48.     WiFi.begin(ssid, password);
  49.     WiFi.setAutoReconnect(false);
  50.     while(WiFi.status() != WL_CONNECTED){Serial.print(".");delay(250);}
  51.     Serial.print("\nIP address: ");Serial.println(WiFi.localIP());
  52.     Serial.println("\nWiFi Connected.....");
  53.     Serial.println("Getting NTP Time Update");
  54.     now = time(nullptr);
  55.     now = time(NULL);
  56.     struct tm *now_tm;
  57.     int64_t t = 0;
  58.     while(t < epoch){
  59.         t = time(&now);
  60.         delay(500);                     //Small delay needed or we miss the packet
  61.     }
  62.     epoch = t;
  63.     now_tm = localtime(&now);
  64.     delay(1000);  
  65.     Serial.println(now_tm," NTP Sync Stamp %A, %B %d %Y %H:%M:%S [%Z]\n");
  66.     lastSec = now_tm->tm_sec;
  67.     if(WiFi.status() == WL_CONNECTED){
  68.         WiFi.disconnect(true);
  69.         WiFi.mode(WIFI_OFF);
  70.         Serial.println(" [WiFi] Disconnected from WiFi and radio off!!\n");
  71.     }
  72. }
  73.  
  74. void setup(void) {
  75.     Serial.begin(115200);
  76.     while(!Serial);
  77.     pinMode(BTN, INPUT);
  78.     configTime(0, 0, "pool.ntp.org", "time.nist.gov");      //Configures 2 x NTP server address's
  79.     setenv("TZ", TZ_INFO, 1); tzset();                      //Setup your Timezone for true local time
  80.     struct timeval tv = { .tv_sec = epoch };
  81.     settimeofday(&tv, NULL);
  82. }
  83.  
  84. void loop(void) {
  85.     updateTime();
  86.     bool btnState = digitalRead(BTN);
  87.  
  88.     if(btnState != lastBtnState){
  89.       pressed = 1;
  90.       previousTime = millis();
  91.     }
  92.     if(millis() - previousTime >= 50){
  93.       if(pressed){
  94.         if(btnState == 0){
  95.           NTPupdate();
  96.         }
  97.       }
  98.       pressed = 0;
  99.     }
  100.     lastBtnState = btnState;
  101. }
Tags: ESP32 time NTP
Advertisement
Add Comment
Please, Sign In to add comment