I am using an ESP32-CAM clone.
The project is to have a PIR sensor trigger video capture to SD card. The whole thing is battery powered so the intention is to have the ESP32 sleep until the PIR triggers, take the video then go back to sleep.
Everything works pretty well, except I noticed the timestamps on the files created were not set so I can't really tell when the files were created.
So, my plan was to set the ESP32's RTC time when everything is powered on. My problem is trying to set the time of the ESP32 RTC, via NTP, and specifically there are 2 weird things happening.
1). During Power On startup the time should be initialised, but if I omit the call to routine displayLocalTime
in initialiseTime
the the time does not seem to be set. The second call (in the setup routine) also fails.
If I leave the call in to displayLocalTime
not only does that call succeed but so does the one in setup
.
2). The other weird thing is that after sleeping, and being woke up from interrupt the time seems to have lost the timezone offset, as it is out by 13 hours.
Here is a cut down version of the troublesome code
#include <WiFi.h>
#include <ESP32Time.h>
ESP32Time rtc;
void setup()
{
Serial.begin(115200);
while(!Serial);
esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause();
if (wakeup_reason == ESP_SLEEP_WAKEUP_EXT0)
Serial.println("Wake up due to interupt");
else
{
Serial.println("Power on startup");
initialiseTime();
}
displayLocalTime();
setInteruptAndSleep();
}
void loop(){}
void initialiseTime()
{
const char *ssid = "PK1"; // WiFi network to connect to.
const char *password = "******"; // Password.
const char *ntpServer = "pool.ntp.org"; // NTP server.
const long gmtOffset_sec = 46800; // New Zealand GMT (+13 hours).
const int daylightOffset_sec = 3600; // 1 hour daylight savings.
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected.");
Serial.println("Getting time.");
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
//displayLocalTime(); // <<<< If this line present time is obtained.
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}
void displayLocalTime()
{
struct tm timeinfo;
if(getLocalTime(&timeinfo))
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
else
Serial.println("Failed to obtain time");
}
void setInteruptAndSleep()
{
esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, 1);
Serial.println("Going to sleep");
delay(500);
esp_deep_sleep_start();
}
Here is the output if I omit the routine call from initialiseTime
Here is the output if I leave the call to in (note the time after starting from interrupt)
Not sure where I'm going wrong.., any help appreciated.