Crashing when Wi-Fi got Disconnected

Hello, I have a project where I am trying to implement FreeRTOS with Firebase ESP Client since I wanted to log some data to the Firebase Realtime Database. However, it keeps on triggering the WDT Reset when my Wi-Fi get disconnected. I also have another different ESP32 board and the same issue persists. Here is the sample code:

#include <Arduino.h>
#include <WiFi.h>
#include <Firebase_ESP_Client.h>
#include <addons/RTDBHelper.h>
#include <addons/TokenHelper.h>
#include <time.h>

#define WIFI_SSID ""
#define WIFI_PASSWORD ""

#define DATABASE_URL ""
#define API_KEY ""

#define USER_EMAIL ""
#define USER_PASSWORD ""

FirebaseData fbdo;
FirebaseAuth auth;
FirebaseJson json;
FirebaseConfig config;

void wifi(void *param)
{

  String uid = "";
  String databasePath, parentPath;
  int timestamp = 0;

  bool timeSynched = false;
  unsigned long previousTimeSync = 0;
  unsigned long wifiConnectedMillis = 0;
  unsigned long wifiReconnectMillis = millis();

  struct tm timeinfo;

  for (;;)
  {

    if (WiFi.status() != WL_CONNECTED)
    {
      if (wifiConnectedMillis == 0)
      {
        WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
        Serial.println();
        Serial.print("Connecting to Wi-Fi");
      }
      else
      {
        if (millis() - wifiReconnectMillis > 1000)
        {
          Serial.println();
          Serial.println("Reconnecting to WiFi");
          wifiReconnectMillis = millis();
          WiFi.reconnect();
        }
      }

      unsigned long wifiTmo = millis();
      while (WiFi.status() != WL_CONNECTED && millis() - wifiTmo < 5000)
      {
        Serial.print(".");
        delay(300);
      }
      Serial.println();

      if (WiFi.status() == WL_CONNECTED)
      {
        wifiConnectedMillis = millis();
        Serial.print("Connected with IP: ");
        Serial.println(WiFi.localIP());
        Serial.println();
      }
      else
      {
        Serial.println("Connection timed out");
        Serial.println();
      }
    }

    if (WiFi.status() == WL_CONNECTED)
    {
      if (!timeSynched && millis() - previousTimeSync > 3000)
      {
        previousTimeSync = millis();
        configTime(8 * 3600, 0, "pool.ntp.org");
      }

      if (!timeSynched)
      {
        timeSynched = getLocalTime(&timeinfo);
      }
    }

    if (Firebase.ready())
    {
      uid = auth.token.uid.c_str();

      Serial.println();

      Serial.print("UID: ");
      Serial.println(uid);

      databasePath = "/userdata/" + uid + "/readings";

      time_t now;
      if (!getLocalTime(&timeinfo))
      {
        Serial.println("Failed to obtain time");
        continue;
      }
      time(&now);
      timestamp = now;
      Serial.printf("Time: %s", asctime(&timeinfo));

      parentPath = databasePath + "/" + String(timestamp);

      json.clear(); // Clear all elements

      json.add("t", "TEST");
      json.add("h", "TEST");
      json.add("p", "TEST");
      json.add("f", "TEST");
      json.add("ts", String(timestamp));
      delay(0);

      Serial.printf("Set json... %s\n", Firebase.RTDB.setJSON(&fbdo, parentPath.c_str(), &json) ? "ok" : fbdo.errorReason().c_str());
    }

    vTaskDelay(2000 / portTICK_PERIOD_MS);
  }
}

void setup()
{
  Serial.begin(115200);

  WiFi.mode(WIFI_STA);

  config.api_key = API_KEY;
  auth.user.email = USER_EMAIL;
  auth.user.password = USER_PASSWORD;
  config.database_url = DATABASE_URL;

  fbdo.setResponseSize(4096);
  config.token_status_callback = tokenStatusCallback;

  Firebase.begin(&config, &auth);

  xTaskCreatePinnedToCore(
    wifi,
    "WiFi",
    10240,
    NULL,
    1,
    NULL,
    0
  );
}

void loop()
{
}

The WDT reset error that I keep on receiving is like this:

The following tasks did not reset the watchdog in time:
E (60250) task_wdt:  - IDLE (CPU 0)
E (60250) task_wdt: Tasks currently running:
E (60250) task_wdt: CPU 0: WiFi
E (60250) task_wdt: CPU 1: loopTask
E (60250) task_wdt: Aborting.

abort() was called at PC 0x400f904c on core 0

Backtrace:0x40083611:0x3ffbea4c |<-CORRUPTED

ELF file SHA256: 0000000000000000

Rebooting...
ets Jul 29 2019 12:21:46

rst:0xc (SW_CPU_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:1
load:0x3fff0030,len:1184
load:0x40078000,len:12812
load:0x40080400,len:3032
entry 0x400805e4

I installed Arduino IDE and the ESP Exception Decoder and pasted that error, this is what shows up:

Decoding stack results
0x40083611: panic_abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_system/panic.c line 402

However, there is also some chance that I received this error when I disconnect Wi-Fi during the vTaskDelay() period of 2 secs.

[ 60374][E][ssl_client.cpp:36] _handle_error(): [data_to_read():343]: (-76) UNKNOWN ERROR CODE (004C)
Set json... response payload read timed out due to network issue or too large data size

I am using:

  • ESP32 NodeMCU-32S
  • Firebase Client
  • PlatformIO IDE v2.4.3 using Arduino Framework
  • I also have Arduino IDE 1.8.19 installed with ESP Exception Decoder

Help is very much appreciated. Thank you!

The Arduino core for the ESP32 automatically configures FreeRTOS so that core_0 is the RF-ESP-protocol and core_1 is Arduino. As configured, you should use loop() because yield() IS automatically added. If more core_0 time is required, you can experiment with adding extra yield() or delay(0) as needed. Google for difference.

I already found a solution/workaround to this problem. I fixed it by removing the config.token_status_callback = tokenStatusCallback; in the void setup() {}.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.