ESP32S2 using arduino 2.0.16. Static IP IOT node that is going to spend most of its time asleep running from batteries. powered over USB for testing. If ESP32S2 periodically reset by timer connected to ENH/RST pin then boots and reliably connects to WiFi in 85-95ms and reliably pulls information over http from local subnet.
When node allowed to go to deep sleep it unreliably wakes up, sometimes after it has woken a watchdog reboots it before it establishes WiFi connection, other times it just takes an unpredictable, but generally several seconds to connect to WiFi.
The sketch below demonstrates the problem
Use code tags to format code for the forum
#include <WiFi.h>
#include <HTTPClient.h>
RTC_DATA_ATTR int nv;
const char* ssid = "****"; // Replace with your network credentials
const char* password = "****";
void setup() {
uint32_t start;
int httpCode;
HTTPClient http;
esp_reset_reason_t reason = esp_reset_reason();
IPAddress local_IP(192, 168, 18,149); // Set Static IP address
IPAddress gateway(192, 168, 18, 1); // Set Gateway IP address
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8); // Needed for NTP
IPAddress secondaryDNS(1,1,1,1); // Backup for NTP
Serial1.begin(9600,SERIAL_8N1,-1,39); // Tx only
delay(10);
Serial1.println("");
switch (reason) {
case ESP_RST_POWERON:Serial1.println("Power ON");break;
case ESP_RST_SW:Serial1.println("Reset ESPrestart()");break;
case ESP_RST_PANIC:Serial1.println("Reset exception/panic");break;
case ESP_RST_UNKNOWN:Serial1.println("Reset UNKNOW");break;
case ESP_RST_EXT:Serial1.println("Reset by external pin (not applicable for ESP32)");break;
case ESP_RST_INT_WDT:Serial1.println("Reset (software or hardware) by interrupt WATCHDOG");break;
case ESP_RST_TASK_WDT:Serial1.println("Reset WATCHDOG");break;
case ESP_RST_WDT:Serial1.println("Reset others WATCHDOG´s");break;
case ESP_RST_DEEPSLEEP:Serial1.println("Reset DEEP SLEEP MODE");break;
case ESP_RST_BROWNOUT:Serial1.println("Brownout reset (software or hardware)");break;
case ESP_RST_SDIO:Serial1.println("SDIO Reset");break;
default:
break;
}
Serial1.printf("nv = %d\r\n",nv);
nv++;
start=millis();
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) Serial1.println("Unable to set Static IP address");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
if (millis()-start > 20000) {
Serial1.println("WiFi Failed - Rebooting");
Serial1.flush();
ESP.restart(); // Fatal unable to establish WiFi force reboot
} else delay(10);
}
Serial1.printf(" CONNECTED %ld ms ",millis()-start);
start=millis();
http.begin("http://192.168.18.240/unixtime");
httpCode = http.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial1.printf(" HTTP %ld\r\n",millis()-start);
}
} else {
Serial1.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
Serial1.flush();
esp_sleep_enable_timer_wakeup(90000000); // 90 sec
esp_deep_sleep_start();
}
void loop() { // never executed
}
Typical output below
Power ON
nv = 0
CONNECTED 94 ms HTTP 2823
Reset DEEP SLEEP MODE
nv = 1
Reset others WATCHDOG´s
nv = 0
CONNECTED 96 ms HTTP 125
Reset DEEP SLEEP MODE
nv = 1
Reset others WATCHDOG´s
nv = 0
CONNECTED 105 ms HTTP 121
Reset DEEP SLEEP MODE
nv = 1
CONNECTED 3075 ms
Reset others WATCHDOG´s
nv = 0
CONNECTED 94 ms HTTP 46
Reset DEEP SLEEP MODE
nv = 1
CONNECTED 7145 ms HTTP 91
Reset DEEP SLEEP MODE
nv = 2
Reset others WATCHDOG´s
nv = 0
CONNECTED 95 ms HTTP 111
Reset DEEP SLEEP MODE
nv = 1
Reset others WATCHDOG´s
nv = 0
CONNECTED 91 ms HTTP 67
Reset DEEP SLEEP MODE
nv = 1
CONNECTED 5255 ms
Reset others WATCHDOG´s
nv = 0
CONNECTED 95 ms HTTP 72
Reset DEEP SLEEP MODE
nv = 1
CONNECTED 6164 ms
Reset others WATCHDOG´s
nv = 0
CONNECTED 95 ms HTTP 84
Reset DEEP SLEEP MODE
nv = 1
Reset others WATCHDOG´s
nv = 0
CONNECTED 94 ms HTTP 77
If I don't use WiFi the node runs for days waking every 90 seconds and increases nv by 1 each time and never produces a watchdog timeout.
How should I be loading/unloading WiFi whereby it quickly connects to to static network and can communicate to subnet.
A workaround is to use a hardware timer to keep rebooting the ESP32/S2 but that doesn't maintain the RTC_RAM.