Daily cron just works on first day
Closed this issue · 5 comments
I'm getting the Systemtime via NTP on the ESP32.
All cronjobs works just for the first day, it looks like after midnight (date change), the cronjobs stop working.
Do you have a idea where the problem might be?
Should i post some snipped of code here?
Interesting news:
I noticed that the problem described above seems to affect only the first cronjob of the day.
For testing purposes I let a function call 3 times a day at the following times: 07:00am, 07:05am and 07:10am.
The first call is not executed, the other two are.
Enclosed is the code snippet with the call of the functions:
_
Cron.create("0 0 7 * * *", Daily_Report, false); // // 7:00am every day send Daily Report
Cron.create("0 5 7 * * *", Daily_Report, false); // // 7:05am every day send Daily Report ->>>>>>>> just for testing
Cron.create("0 10 7 * * *", Daily_Report, false); // // 7:10am every day send Daily Report ->>>>>>>> just for testing
_
Please attach a testing "ino" file.
ESP32_CurrentGuard_mod_for_publish.zip
I have attached a file in which I have commented out the private things (email address, wifi, etc.)
@wanie1
I was expecting the minimal code that reproduced the error. I cannot run your code (because of the use of peripherals in it) and it would take too much of my time (I have time constrictions right now) to analyse it all to extract the essential part.
However, I saw that you use the NTP library which is intended for Arduino. This might be the problem. ESP32 has direct support of NTP in the SDK. Look at the example:
https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/Time/SimpleTime/SimpleTime.ino
I could not reproduce your problem. Upon that example I created some test code that works correctly. May be you can use it as a model to modify yours:
#include <WiFi.h>
#include "time.h"
#include "CronAlarms.h"
#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#endif
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 60 * 60;
const int daylightOffset_sec = 60 * 60;
static timeval tv;
void printLocalTime()
{
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}
void Daily_Report() {
Serial.print("Daily_Report: ");
printLocalTime();
}
void JumpInTime() {
Serial.print("JumpInTime: ");
printLocalTime();
gettimeofday(&tv, nullptr);
constexpr int hours = 23;
constexpr int mins = 59;
tv.tv_sec += hours * 60 * 60 + mins * 60 + 45;
settimeofday(&tv, nullptr);
Serial.print("LandInTime: ");
printLocalTime();
}
void setup()
{
Serial.begin(115200);
//connect to WiFi
Serial.printf("Connecting to %s ", STASSID);
WiFi.mode(WIFI_STA);
WiFi.begin(STASSID, STAPSK);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" CONNECTED");
//init and get the time
//configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", "pool.ntp.org"); // Timezone for CET, check TZ.h or https://www.iana.org/time-zones
printLocalTime();
//disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
delay(1000);
printLocalTime();
struct tm tm_newtime; // set time to Saturday 6:59:00am Oct 1 2020
tm_newtime.tm_year = 2020 - 1900;
tm_newtime.tm_mon = 10 - 1;
tm_newtime.tm_mday = 1;
tm_newtime.tm_hour = 5;
tm_newtime.tm_min = 59;
tm_newtime.tm_sec = 45;
tm_newtime.tm_isdst = 0;
timeval tv = { mktime(&tm_newtime), 0 };
settimeofday( &tv, nullptr );
Cron.create("0 0 7 * * *", Daily_Report, false); // // 7:00:00am every day send Daily Report
Cron.create("5 0 7 * * *", Daily_Report, false); // // 7:00:05am every day send Daily Report ->>>>>>>> just for testing
Cron.create("10 0 7 * * *", JumpInTime, false); // // 7:00:10am every day send Daily Report ->>>>>>>> just for testing
printLocalTime();
}
void loop()
{
Cron.delay();
delay(1000);
printLocalTime();
}
I would really appreciate that you reported back your progress, whether you succeeded in fixing your code, what was the problem and how you did it.
Good luck!
Hi Martin,
sorry for the big set of code, and thanks for your helpful answer.
I did a quick modification of my code and will test it a few days.
But for now it looks god, because before unmasking NTP library (as you recommended), after power up my ESP32 was running each cronjob one time, no matter wich time.
This was not happend yet.
I will give you a feedback in the next days.
Thanks,
Manuel
Hi Martin,
it seems to work stable and reliable.
Thanks for your great support!