Chat freely about anything...

User avatar
By Sirquil
#75109 schufti, Thank you for the suggestion. Updated the esp8266 core to 2.4.1 in the Arduino IDE.

Could you provide more information about newlibc? First I have learned of newlibc.

Are there any examples of newlbc to "have accurate local time (with automatic dst) synced every hour in the background; returning requests for time almost instantly."

William
User avatar
By schufti
#75120 Hi,
Code: Select all#include <ESP8266WiFi.h>
#include <sys/time.h>                   // struct timeval
#include <time.h>                       // time() ctime()         1)

#define ssid "yourSSID"
#define wpwd "yourPASSPHRASE"

// #define RTC_TEST 1510592825 // = Monday 13 November 2017 17:07:05 UTC
#define RTC_TEST 1499893466 // = Wed 12 July 2017 21:04:26 UTC

int lc = 0;
timeval tv = { RTC_TEST, 0 };

void setup() {
  Serial.begin(74880);
  configTime(0, 0, "pool.ntp.org");      // 2)
  settimeofday(&tv, nullptr);
  setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 3);   // this sets TZ to Brussels/Paris/Vienna     // 2)
  tzset();     // 2)
  WiFi.persistent(false);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, wpwd);

  // don't wait, observe time changing when ntp timestamp is received
}

void loop() {
  char strftime_buf[64];
  time_t tnow = time(nullptr);
  Serial.println(millis());
  // simple localtime
  Serial.print(ctime(&tnow));
  //  formated localtime
  strftime(strftime_buf, sizeof(strftime_buf), "%c", localtime(&tnow));
  Serial.println(strftime_buf);
  //  formated gmtime
  strftime(strftime_buf, sizeof(strftime_buf), "%c", gmtime(&tnow));
  Serial.println(strftime_buf);

  if (lc == 15) {
    WiFi.disconnect();
    WiFi.mode(WIFI_OFF);
    Serial.println("---- WiFi off -----");
  }
  if (lc == 20) {
    tv.tv_sec = 0;
    settimeofday(&tv, nullptr);
    Serial.println("----- back in time -----");
  }

  if (lc == 25) {
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, wpwd);
    Serial.println("----- WiFi on -----");
}

  lc++;
  delay(1000);
}


this example should show you the basics and that it provides correct time even if wifi fails (for some time)

in general it boils down to 1) one include , 2) 3 lines initialisation in setup() and your choice of newlibc functions to get utc or local time from loop()...

reference to newlibc time functions: https://sourceware.org/newlib/libc.html#Timefns
User avatar
By Sirquil
#75125 Thank you for the example code and link to the reference.

What happens if the watchdog timer resets? Correct me if I am wrong, it looks to me like a new time define statement would be required.

With code I have inplace; there is no need to enter a new define statement, since it all comes from the NTP time server. Watchdog timer reset with my code continues on with the correct time.

After changing time zone; looks promising.

William
Attachments
Output of example code with timezone changed for Indianapolis, IN
(44.48 KiB) Downloaded 703 times
User avatar
By rudy
#75130
schufti wrote:
Code: Select all  setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 3);   // this sets TZ to Brussels/Paris/Vienna     // 2)


I had found this in Arduino/tools/sdk/libc/xtensa-lx106-elf/include/stdlib.h

setenv,(const char *__string, const char *__value, int __overwrite)

and then this elsewhere.

Description:
The setenv() function sets the environment variable name to value. If name doesn't exist in the environment, it's created; if name exists and overwrite is nonzero, the variable's old value is overwritten with value; otherwise, it isn't changed.


So does 3 (third parameter) in the example above mean anything? Or is 1, 2, 4, 99 all the same as 3?

In a similar code example I found, and have been using, this

setenv("TZ", "CST6CDT,M3.2.0,M11.1.0", 0);