Page 1 of 1

E (80514) wifi:NAN WiFi stop

Posted: Thu Dec 05, 2024 10:51 am
by PsySc0rpi0n
Hello.
I'm working with a device that uses a ESP32-WROVER-E, which is a LilyGo T-SIM7600G-H.

I have been trying to write a very simple code that connects the ESP32 to a WiFi network, perform an action and after the action is performed, I disconnect the ESP32 from the network and shutdown the ESP32 wireless feature so that I can save battery.

The code I have is split in 2 files: project.ino and wifi_handler.cpp.

The part of the code from project.ino that manages the WiFi connection is this:

Code: Select all

    // WiFi connection check and retry logic
    if (millis() - last_wifi_attempt >= WIFI_RETRY_INTERVAL) {
        if (!wifi_connected) {
            // No WiFi connection, try to connect
            SerialMon.println("Attempting to connect to WiFi...");
            int wifi_status = connect_wifi(wifi_ssid, wifi_passwd);

            if (wifi_status == 1) {
                GOT_HERE();
                wifi_connected = true;
                SerialMon.println("Connected to WiFi!");
            } else if (wifi_status == 0) {
                SerialMon.println("Failed to connect to WiFi.");
                GOT_HERE();
            } else if (wifi_status == -1) {
                SerialMon.println("Connection in progress...");
                GOT_HERE();
            }
        }

        // If WiFi is connected, proceed with data submission
        if (wifi_connected) {
            String address = get_address_from_coordinates(loc.lat, loc.lon);
            snprintf(description, BUFFER, address.c_str());
            send_to_database(sensorID,
                             loc.lat, loc.lon,
                             distance,
                             weight,
                             description);
						
	    int disconnect_status = disconnect_wifi();
	    if (disconnect_status == 0){
		SerialMon.println("WiFi Disconnected.");
	    } else if (disconnect_status == 1){
		SerialMon.println("WiFi not connected.");
	    } else {
		SerialMon.println("Failed to disconnect from WiFi");
	    }
            last_wifi_attempt = millis();  // Update last_wifi_attempt to prevent immediate retry
        }
    }
The code I have for connect_wifi() and disconnect_wifi() is the following:
connect_wifi()

Code: Select all

int connect_wifi(const char* ssid, const char* pwd){
    wl_status_t wifi_status = WiFi.status();

    if (WiFi.status() == WL_CONNECTED){
        return 1; // Already connected
    }

    if (wifi_status == WL_DISCONNECTED   ||
        wifi_status == WL_NO_SHIELD      ||
        wifi_status == WL_IDLE_STATUS    ||
        wifi_status == WL_CONNECT_FAILED){
        
        WiFi.mode(WIFI_STA);
        WiFi.begin(ssid, pwd);

        unsigned long connect_start = millis();
        while(WiFi.status() != WL_CONNECTED &&
              (millis() - connect_start < 5000)){
            delay(500);
            SerialMon.print(".");
        }

        if (WiFi.status() == WL_CONNECTED){
            return 1; // Connection successfull
        } else {
            return 0; // Connection to WiFi failed
        }
    } else {
        return 1; // Connection in progress
    }
    return -1;
}
and disconnect_wifi():

Code: Select all

int disconnect_wifi(){
    /*
     * 0 -> WiFi disconnected successfully
     * 1 - WiFi is not connected
     * 2 - Failed to disconenct (timeout)
     */
    
    if (WiFi.status() != WL_CONNECTED){
        return 1; // No action needed. We are not connected,
                  // so no disconnect needed
    }

    WiFi.disconnect(true);

    unsigned long start_time = millis();
    while(WiFi.status() == WL_CONNECTED && millis() - start_time < 1000){
        delay(100);
    }

    if (WiFi.status() != WL_CONNECTED){
        WiFi.mode(WIFI_OFF);
        delay(100);
        return 0; // WiFi disconencted and hardware is OFF to save battery
    } else {
        return 2; // WiFi, for some reason, didn't disconnect
    }
}
What happens is that when the diconnect_function() is called, I always get the E (80514) wifi:NAN WiFi stop, which leads later to more issues like, for some reason, the connect_wifi() functions seems not to be called again as if the WiFi is still connected.

Is there anythin obvious in the code that might be triggering this behaviour?