macca-nz

manually pass an epoch on ESP dev board

Aug 22nd, 2023
1,508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.14 KB | Source Code | 0 0
  1. /*
  2.  * Manually set human readable time on an ESP device
  3.  * Sketch will work on both ESP8266 and ESP32
  4.  * The idea of the sketch is to show how an EPOCH Unix time can be passed to
  5.  * the ESP's time function which is enabled by default when you select the board
  6.  * To do this you press the Flash/Boot button on the controller and enter the
  7.  * blocking "Serial Input" mode
  8.  * enter a valid EPOCH value and the system will sync to it.
  9.  * I have also changed the default 1/1/1970 Epoch to 1/1/2021 Midnight plus 2hrs 13min 20sec
  10.  * You need to chage the "TZ_INFO" line 18 to suit your timezone
  11.  * You can find the zone list here <https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv>
  12.  */
  13.  
  14. #if defined (ARDUINO_ARCH_ESP8266)
  15.     #include "sntp.h"
  16. #endif
  17.  
  18. #define BTN 0
  19. #define TZ_INFO "NZST-12NZDT,M9.5.0,M4.1.0/3"     //for local time
  20.  
  21. int32_t epoch = (1609459200 + 8000);         //1st Jan 2021 00:00:00 [UTC] + 2hrs 13min 20sec
  22. bool pressed = 0, btnState, lastBtnState = 1, enableSerial = 0;
  23. uint32_t previousTime = 0;
  24. uint8_t lastSec;
  25.  
  26. void updateTime(void){              //This is the method to produce a
  27.     uint8_t sec = 0;                //Human readable clock in the Serial monitor
  28.     char buf_t[80];
  29.     time_t t = time(NULL);
  30.     struct tm *now_tm;
  31.     now_tm = localtime(&t);
  32.     sec = now_tm->tm_sec;
  33.     if(lastSec != sec){
  34.         strftime(buf_t, 80, "\n[%Z][UTC %z] %c", now_tm);
  35.         Serial.println(buf_t);
  36.         lastSec = sec;
  37.     }
  38. }
  39.  
  40. void setup() {
  41.   Serial.begin(115200);
  42.   setenv("TZ", TZ_INFO, 1); tzset();                   //Setup your Timezone for local time
  43.   #if defined (ARDUINO_ARCH_ESP8266)
  44.     struct timeval _tv = {.tv_sec = epoch};            //passing epoch to timeval
  45.     settimeofday(&_tv, NULL);
  46.     Serial.println("\n\nESP8266 Epoch is Set and clock is starting\n");
  47.   #elif defined (ARDUINO_ARCH_ESP32)
  48.     struct timeval tv = {.tv_sec = epoch};             //passing epoch to timeval
  49.     settimeofday(&tv, NULL);
  50.     Serial.println("\n\nESP32 Epoch is Set and clock is starting\n");
  51.   #endif
  52. }
  53.  
  54. void loop() {
  55.      btnState = digitalRead(BTN);
  56.  
  57.     if(btnState != lastBtnState){
  58.       pressed = 1;
  59.       previousTime = millis();
  60.     }
  61.     if(millis() - previousTime >= 50){
  62.       if(pressed){
  63.         if(btnState == 0){
  64.           enableSerial = 1;
  65.           Serial.println("\n\rSerial is Waiting for a Unix Epoch Value\n\r");
  66.         }
  67.       }
  68.       pressed = 0;
  69.     }
  70.     if(enableSerial){
  71.       if(Serial.available() > 0){
  72.         String inString = Serial.readStringUntil('\n');
  73.         epoch = inString.toInt();
  74.           #if defined (ARDUINO_ARCH_ESP8266)
  75.             struct timeval _tv = {.tv_sec = epoch};            //passing epoch to timeval
  76.             settimeofday(&_tv, NULL);
  77.           #elif defined (ARDUINO_ARCH_ESP32)
  78.             struct timeval tv = {.tv_sec = epoch};            //passing epoch to timeval
  79.             settimeofday(&tv, NULL);
  80.           #endif
  81.         enableSerial = 0;
  82.         Serial.println("\n\rSystem Human Readable Clock has been Updated\n\r");
  83.       }
  84.     }else{
  85.       updateTime();
  86.     }
  87.     lastBtnState = btnState;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment