Exception 4: Level1Interrupt: Level-1 interrupt as indicated by set level-1 bits in the INTERRUPT register

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

Mod edit:
In case there's any doubt this reply is AI generated so should be treated with care as it has not been verified as correct. It has been checked for hidden spam and none has been found.

It looks like your ESP8266 is encountering a Soft WDT (Watchdog Timer) reset, which is typically caused by the system getting stuck in a state for too long or having a blocking operation. The error you're seeing indicates that the system was interrupted at a low-level interrupt (Level-1 interrupt).

In your case, the likely cause is the wifi_fpm_do_sleep(0xFFFFFFF); call, which is intended to put the ESP8266 into light sleep indefinitely until a GPIO interrupt occurs. This method can sometimes lead to problems, especially with the watchdog timer if not properly handled.

Here are some suggestions to address the issue:

  1. Reduce the Sleep Duration Constant: You mentioned that when the awake constant is set to less than 13 seconds, the problem doesn't occur. This suggests that the watchdog timer might be expiring during the sleep duration. Try reducing the sleep time or modifying your logic to ensure the device isn't in sleep mode for too long.
  2. Ensure Proper Light Sleep Handling: Make sure the wifi_fpm_do_sleep function is correctly used. The parameter 0xFFFFFFF might be too long and could cause issues. Instead, try using a more reasonable value for sleep duration. For example:
    wifi_fpm_do_sleep(10 * 1000);  // Sleep for 10 seconds
    
  3. Prevent Watchdog Timer Timeout: Watchdog timers reset the ESP8266 if the system hangs or takes too long to complete an operation. To prevent this, you should periodically call yield() in your loop, which gives control back to the watchdog timer to reset it. Ensure that your loop() function doesn't get stuck for long periods.For example, update your loop() function:
    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);
    
      if ((!pinState) && (!apiSND)) {
        if (!pinWasLow) {
          startTime = millis();
          pinWasLow = true;
        } else {
          if (millis() - startTime >= duration) {
            Serial.println("Pin has been LOW for 3 seconds!");
            apiSND = true;
          }
        }
      } else {
        pinWasLow = false;
      }
    
      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();
        }
      }
    
      // Yield to prevent watchdog timer reset
      yield();
    }
    
  4. Adjust GPIO Wake-Up Configuration: Make sure the GPIO wake-up configuration is set correctly. Verify that the GPIO pin you are using for wake-up is configured as expected and that no other configurations might be interfering with its operation.
  5. Update Libraries and Firmware: Ensure that you are using the latest versions of the ESP8266 libraries and firmware. Sometimes issues are resolved in newer versions.

By implementing these changes, you should be able to address the Soft WDT reset issue and ensure more reliable operation of your ESP8266-based device.

i am using this parameter to sleep untill trigger is given . And yield() is not solving the issue. The code is running in main loop so i don't understand why it triggers WDT. I think i use it with less awake time then.

@bruce963
Get lost, chatGPT.

Nani?..

That is how it started.

:rofl: I am just little surprised strict actions against chat-gpt