The following code is supposed to do these actions:
Upon first run: connect to WiFi, synchronize the RTC with NTP, get the time into variables, print the time and reset reason.
Then go to deep sleep for 35 seconds.
Upon retun from deep sleep, detect that state, do not reconnect to WiFi, do not resynchronize the RTC, get the time into variables from the RTC, print the time and reset reason.
Then go to deep sleep again for 35 seconds and so on.
According to the serial Monitor, it does what I want.
The only problem is that it still draws 72mA @ 4,1V from the LiPo, that isnt exactly what I expected from deep sleep!
Code:
Code: Select all
#include <WiFi.h>
#include "time.h"
#include <rom/rtc.h> //needed to get reset reason
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 35 /* ESP32 will go to sleep for (x seconds) */
const char* ssid = "SSID";
const char* password = "PASS";
const char* ntpServer = "de.pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
int second;
int minute;
int hour;
int day;
int month;
int year;
int weekday;
struct tm timeinfo;
int resetReason0;
int resetReason1;
RTC_DATA_ATTR int bootCount = 0; //Boot count should be stored in the non volatile RTC Memory
//Parameters see: http://www.cplusplus.com/reference/ctime/tm/
void setup()
{
Serial.begin(115200);
resetReason0 = rtc_get_reset_reason(0);
resetReason1 = rtc_get_reset_reason(1);
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch (wakeup_reason)
{
case 1 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case 2 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case 3 : Serial.println("Wakeup caused by timer");
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
break;
case 4 : Serial.println("Wakeup caused by touchpad"); break;
case 5 : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.println("Wakeup was not caused by deep sleep");
if (!getLocalTime(&timeinfo))
{
//connect to WiFi
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" CONNECTED");
//init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();
//disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}
break;
}
bootCount++;
}
void loop()
{
//printLocalTime();
getTimeValues();
Serial.print("Time from variables: ");
Serial.print(day);
Serial.print(".");
Serial.print(month);
Serial.print(".");
Serial.print(year);
Serial.print(" --- ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.print(second);
Serial.print(" Boot count: ");
Serial.print(bootCount);
Serial.print(" Reset reason: ");
Serial.println(resetReason0);
Serial.println("Setup ESP32 to sleep for " + String(TIME_TO_SLEEP) + " Seconds");
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Going to sleep now");
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
void print_wakeup_reason()
{
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch (wakeup_reason)
{
case 1 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case 2 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case 3 : Serial.println("Wakeup caused by timer"); break;
case 4 : Serial.println("Wakeup caused by touchpad"); break;
case 5 : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.println("Wakeup was not caused by deep sleep"); break;
}
}
void getTimeValues()
{
getLocalTime(&timeinfo);
second = timeinfo.tm_sec;
minute = timeinfo.tm_min;
hour = timeinfo.tm_hour;
day = timeinfo.tm_mday;
month = timeinfo.tm_mon + 1;
year = timeinfo.tm_year + 1900;
weekday = timeinfo.tm_wday + 1;
}
void printLocalTime()
{
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%A, %d %B %Y %H:%M:%S");
//Parameters see: http://www.cplusplus.com/reference/ctime/strftime/
}
Code: Select all
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:812
load:0x40078000,len:0
load:0x40078000,len:11404
entry 0x40078a28
Wakeup was not caused by deep sleep
Connecting to GW-FM-MA .... CONNECTED
Wednesday, 04 April 2018 17:18:10
Time from variables: 4.4.2018 --- 17:18:10 Boot count: 1
Reset reason: 1
Setup ESP32 to sleep for 35 Seconds
Going to sleep now
ets Jun 8 2016 00:22:57
rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:812
load:0x40078000,len:0
load:0x40078000,len:11404
entry 0x40078a28
Wakeup caused by timer
Time from variables: 4.4.2018 --- 15:18:45 Boot count: 2
Reset reason: 5
Setup ESP32 to sleep for 35 Seconds
Going to sleep now
ets Jun 8 2016 00:22:57
rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:812
load:0x40078000,len:0
load:0x40078000,len:11404
entry 0x40078a28
Wakeup caused by timer
Time from variables: 4.4.2018 --- 15:19:21 Boot count: 3
Reset reason: 5
Setup ESP32 to sleep for 35 Seconds
Going to sleep now
ets Jun 8 2016 00:22:57