Hi,
I'm working on a World clock development with an ESP8266. But my code is always crashing after a while. I used as the basis for my development the NTP-TZ-DST example.
I condensed my code to force the issue, after about 114 to 127 cycles the system is crashing.
I like to know if I do not understand the time functions correctly or if there is a memory leak in the time(zone) management.
While I try and error for a while now, I would be happy to get help here.
Thanks for your support.
Jens
#include <TZ.h>
#include <ESP8266WiFi.h>
#include <coredecls.h> // settimeofday_cb()
#include <Schedule.h>
#include <PolledTimeout.h>
#include <time.h> // time() ctime()
#include <sys/time.h> // struct timeval
#include <sntp.h> // sntp_servermode_dhcp()
// for testing purpose:
extern "C" int clock_gettime(clockid_t unused, struct timespec *tp);
static timeval tv;
static timespec tp;
static time_t now;
bool NTP_Time_is_set;
void Debug(String Text);
String Left(String Text,int len);
String Get_Time_Zone(int TZ_Number);
void NTP_time_is_set_scheduled();
String NTP_Time_Text(String TimeZone);
void Debug( String Text)
{
String NTP_Time_Text = NTP_Get_Time(TZ_Europe_Berlin) + ":" + Text;
Serial.println(NTP_Time_Text);
}
String Left(String Text,int len)
{
Text = Text.substring(Text.length()-len, Text.length());
return Text;
}
String Get_Time_Zone(int TZ_Number)
{
switch (TZ_Number)
{
case 0:
return TZ_Europe_Berlin;
break;
case 1:
return TZ_Europe_London;
break;
case 2:
return TZ_America_New_York;
break;
case 3:
return TZ_America_New_York;
break;
case 4:
return TZ_America_Chicago;
break;
case 5:
return TZ_America_Los_Angeles;
break;
case 6:
return TZ_Asia_Kolkata;
break;
case 7:
return TZ_Australia_Sydney;
break;
case 8:
return TZ_Asia_Manila;
break;
}
}
void NTP_time_is_set_scheduled()
{
NTP_Time_is_set = true;
Debug("NTP was updated");
for(int i = 0 ; i <= 1000; i++)
for(int j = 0 ; j <= 8; j++)
Debug(Left("0000" + String(i),4) + ":" + String(j) + ":" + NTP_Get_Time(Get_Time_Zone(j)));
}
String NTP_Get_Time(String TimeZone)
{
String Time_Return;
if(NTP_Time_is_set == true)
{
char TimeZoneBuffer[100];
TimeZone.toCharArray(TimeZoneBuffer, 99);
setenv("TZ", TimeZoneBuffer, 1);
tzset();
gettimeofday(&tv, nullptr);
clock_gettime(0, &tp);
now = time(nullptr);
const tm* tm = localtime(&now);
Time_Return = Left("00" + String(tm->tm_hour),2) + ":" + Left("00" + String(tm->tm_min),2) + ":" + Left("00" + String(tm->tm_sec),2);
}
else
Time_Return = Left("00000000" + String(millis()),8);
return Time_Return;
}
void setup() {
Serial.begin(115200);
Debug("Setup NTP...");
time_t rtc = 0;
timeval tv = { rtc, 0 };
settimeofday(&tv, nullptr);
settimeofday_cb(NTP_time_is_set_scheduled);
configTime(0, 0, "fritz.box","192.168.100.254","pool.ntp.org");
gettimeofday(&tv, nullptr);
clock_gettime(0, &tp);
now = time(nullptr);
setenv("TZ", "UTC0", 1);
Debug("Setup NTP Done");
Debug("Time is currently set by a constant.");
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.begin("testwifi","1234567890abcdef");
}
void loop() {
yield();
}