Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. ESP WiFi hung solution

ESP WiFi hung solution

Scheduled Pinned Locked Moved Development
1 Posts 1 Posters 33 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fsgraz
    wrote on last edited by
    #1

    This is how I solved the issue of ESP32 GW hanging when WiFi connection is lost or not established. It is not 100% my original work, it has been put together using bits and pieces, but it works.

    I simply use a watchdog timer:

    #include "esp_system.h" // added for WDT
    hw_timer_t* timer = NULL;
    
    // WDT related
    void IRAM_ATTR resetModule() {
    	Serial.println("reboot\n");
    	ESP.restart();
    }
    
    void WdtReset()
    {
        timerWrite(timer, 0); //reset timer (feed watchdog)
    }
    
    void StartWatchDog()
    {
        timer = timerBegin(0, 240, true); //timer 0, div 80
        timerAttachInterrupt(timer, &resetModule, true);
        timerAlarmWrite(timer, 5000000, false); //set time in us
        timerAlarmEnable(timer); //enable interrupt
    }
    
    IPAddress ConnectWiFi()
    {
        Serial.println();
        Serial.println();
        Serial.print("Connecting to ");
        Serial.println(MY_WIFI_SSID);
    
        /* Explicitly set the ESP to be a WiFi-client, otherwise, it by default,
           would try to act as both a client and an access-point and could cause
           network-issues with your other WiFi-devices on your WiFi-network. */
    #ifdef ESP32
        WiFi.persistent(false);
        WiFi.mode(WIFI_STA);
    #endif // ESP32
    
        WiFi.begin(MY_WIFI_SSID, MY_WIFI_PASSWORD);
    
    
        while (WiFi.status() != WL_CONNECTED) {
            digitalWrite(LED_BUILTIN, LED_ON);
            wait(500);
            Serial.print(".");
            digitalWrite(LED_BUILTIN, LED_OFF);
            wait(500);
        }
    
    #ifdef ESP32
        WiFi.setAutoReconnect(true);
    #endif // ESP32
    
        return WiFi.localIP();
        
    }
    void before()
    {
    
        StartWatchDog();
    
        // We start by connecting to a WiFi network
        localIP = ConnectWiFi();
        myHostname = WiFi.getHostname();
    
        WdtReset();
    
        Serial.println("");
        Serial.println("WiFi connected");
        Serial.println("IP address: ");
        Serial.println(localIP);
        Serial.println(myHostname);
    
    // do other stuff ...
    
    }
    
    void loop()
    {
       
    
        // check WiFi connection
        if (WiFi.status() != WL_CONNECTED) return; // the WDT is not reset
        WdtReset();
    
    // do other stuff ...
    }
    

    Hope it helps

    1 Reply Last reply
    2
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    9

    Online

    11.7k

    Users

    11.2k

    Topics

    113.1k

    Posts


    Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • MySensors
    • OpenHardware.io
    • Categories
    • Recent
    • Tags
    • Popular