I am building device which sends API when the button is pressed more than 3 seconds else it goes to sleep mode. The sending API part is fine and going to sleep after that part is also fine. The issue is if the button is not pressed less than 3 seconds it is considered as false call . So the device wait for interaction for about 15 seconds then got sleep again . When the device go to sleep after false call i got this error
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
Soft WDT reset
Exception (4):
epc1=0x40233cec epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
>>>stack>>>
ctx: cont
sp: 3ffffe40 end: 3fffffd0 offset: 0160
3fffffa0: 0fffffff 40201130 3ffee638 40201127
3fffffb0: 3fffdad0 00000000 3ffee6b4 40205360
3fffffc0: feefeffe feefeffe 3fffdab0 40100d59
<<<stack<<<
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x4010f000, len 3424, room 16
tail 0
chksum 0x2e
load 0x3fff20b8, len 40, room 8
tail 0
chksum 0x2b
csum 0x2b
v00046aa0
~ld
Checking the Error is ESP exception decoder it shows this
Exception 4: Level1Interrupt: Level-1 interrupt as indicated by set level-1 bits in the INTERRUPT register
PC: 0x40233cec
EXCVADDR: 0x00000000
Decoding stack results
0x40201130: light_sleep() at D:\Projects\Projects\modifiedLight_sleep\modifiedLight_sleep.ino:62
0x40201127: light_sleep() at D:\Projects\Projects\modifiedLight_sleep\modifiedLight_sleep.ino:59
0x40205360: loop_wrapper() at C:\Users\user\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp:258
Here is my source code
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#include <user_interface.h> // For Wi-Fi power management functions
#define WAKEUP_GPIO_PIN D3 // Define GPIO pin for wake-up (D3)
// const int pin = D3;
const unsigned long duration = 3000; // Duration to check in milliseconds (3 seconds)
const unsigned long awake = 15000;
unsigned long startTime = 0; // Variable to store the start time
bool pinWasLow = false;
bool apiSND = false;
bool wake = false;
// Function to connect to Wi-Fi
void connectToWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin("SSID", "PASS");
// Wait for connection
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
Serial.print(".");
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println();
Serial.println("Connected to Wi-Fi");
} else {
Serial.println();
Serial.println("Failed to connect to Wi-Fi");
}
}
// Function to enter light sleep
void light_sleep() {
gpio_pin_wakeup_enable(WAKEUP_GPIO_PIN, GPIO_PIN_INTR_LOLEVEL);
// Disconnect from Wi-Fi
WiFi.disconnect();
wifi_station_disconnect(); // Ensure disconnection
wifi_set_opmode(NULL_MODE); // Set Wi-Fi mode to NULL_MODE
// Set the sleep type to light sleep
wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);
if (wifi_fpm_get_sleep_type() != LIGHT_SLEEP_T) {
Serial.println("Error setting sleep type");
return;
}
// Serial.println("Set to light sleep");
wifi_fpm_open();
wifi_fpm_set_wakeup_cb(wakeup);
Serial.println("Entering light sleep");
// Enter light sleep
wifi_fpm_do_sleep(0xFFFFFFF); // Sleep indefinitely until GPIO interrupt
// Short delay to ensure the CPU enters light sleep
delay(10);
}
// Wake-up callback function
void wakeup(void) {
Serial.flush();
wifi_fpm_close();
gpio_pin_wakeup_disable();
wake = true;
// apiSND = false;
// The ESP8266 will reset and execute the setup function upon waking up
}
void setup() {
Serial.begin(115200);
pinMode(WAKEUP_GPIO_PIN, INPUT_PULLUP);
connectToWiFi(); // Connect to Wi-Fi
// Enter light sleep
light_sleep();
}
void loop() {
if (wake) {
Serial.println();
Serial.println("Woke up after sleep");
wifi_set_opmode(STATION_MODE);
WiFi.begin("ssid", "PASS");
wake = false;
}
bool pinState = digitalRead(WAKEUP_GPIO_PIN);
// Check if the pin is HIGH
if ((!pinState) && (!apiSND)) {
if (!pinWasLow) {
// Pin has just become HIGH, record the start time
startTime = millis();
pinWasLow = true;
} else {
// Pin has been HIGH for some time, check if it has been HIGH for the required duration
if (millis() - startTime >= duration) {
Serial.println("Pin has been LOW for 3 seconds!");
apiSND = true;
}
}
} else {
// Pin is not HIGH, reset the flag
pinWasLow = false;
// connectToWiFi();
// String b = get1();
// light_sleep();
}
if (WiFi.status() == WL_CONNECTED) {
if (apiSND) {
get1();
delay(2000);
light_sleep();
} else if (millis() - startTime >= awake) {
Serial.println("False Call");
delay(100);
light_sleep();
}
}
}
void get1() {
HTTPClient http; // Declare object of class HTTPClient
WiFiClient client;
String Link1 = "API is here";
http.begin(client, Link1); // Specify request destination
int httpCode1 = http.GET(); // Send the request
String payload = http.getString(); // Get the response payload
Serial.println(payload);
http.end(); // Close connection
apiSND = false;
// return payload;
}
it showing issue with these lines ..
wifi_fpm_do_sleep(0xFFFFFFF); // Sleep indefinitely until GPIO interrupt
// Short delay to ensure the CPU enters light sleep
delay(10);
i don't know how to fix this .
NB: When the value of awake const unsigned long awake = 15000;
is less than 13000
13sec it doesn't have any issue .
Device : ESP8266
Power : USB from PC
Other connections : Switch connected to D3 with GND to pull low when switch is triggered