E (80514) wifi:NAN WiFi stop

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:

    // 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()

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():

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 anything obvious in the code that might be triggering this behaviour?

So that we can understand your project and then help you, please post your complete code.

The complete code of the whole project is split in like 9 pairs of .cpp and .h files. I split the code in several files so that each functionality is also concealed in their respective files for readability.

smartbin.ino, smartbin.h
gsm_handler.cpp, gsm_handler.h
wifi_handler.cpp, wifi_handler.h
gps_handler.cpp, gps_handler.h
database_handler_cpp, database_handler.h
locadcell_handler.cpp, loadcell_handler.h
ultrassonic_handler.cpp, ultrassonic_handler.h
modem_handler.cpp, modem_handler.h
location_handler.cpp, location_handler.h

You still want me to post the whole code of all the files or only from wifi_handler.cpp and project.ino ?

The complete code in smartbin.ino, smartbin.h is:

#include "smartbin.h"
#define GOT_HERE() do {Serial.print('['); Serial.print(__LINE__); Serial.println(']'); Serial.flush();} while(0)
#define BUFFER 128 
#define WIFI_RETRY_INTERVAL 60000UL

unsigned long last_wifi_attempt = 0UL;
bool wifi_connected = false;

float distance;
float latitude, longitude, weight;
char description[BUFFER];
String sensorID = "sensorABC";
Coordinate loc;

void setup() {
    SerialMon.begin(BAUD_RATE);
    SerialMon.setRxBufferSize(1024);
    delay(10);

    SerialAT.begin(BAUD_RATE, SERIAL_8N1, MODEM_RX, MODEM_TX);
    scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);

    pinMode(LED_PIN, OUTPUT);
    pinMode(TRIG_PIN, OUTPUT);
    pinMode(ECHO_PIN, INPUT);

    WiFi.disconnect(true);
    delay(1000);
    WiFi.mode(WIFI_STA);

    toggleLed();
    start_modem();         // Modem init
    config_modem_gps();    // GPS init
    setup_scale();         // Set up the scale
    toggleLed();
    configTime(gmt_offset_sec, daylight_offset_sec, ntp_server);
}

void loop() {
    loc = get_nmea_message();  // Always retrieve GPS data
    if (loc.lat == 0.0 && loc.lon == 0.0) {
        SerialMon.println("Failed to get GPS data");
    }
    SerialMon.print("Distance: ");
    distance = get_distance();
    SerialMon.println(distance);
    SerialMon.print("Weight: ");
    weight = get_weight();
    SerialMon.println(weight, 1);
    delay(1000);
    toggleLed();
    
    // 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
        }
    }

    // If WiFi is not connected, you might want to try GSM or other logic
    // e.g. else { attempt GSM connection }
}

bool toggleLed() {
    static bool ledState = false;
    ledState = !ledState;
    digitalWrite(LED_PIN, ledState ? HIGH : LOW);
    return ledState;
}

smartbin.h

#ifndef SMARTBIN_H
#define SMARTBIN_H

#define TINY_GSM_MODEM_SIM7600
#include <TinyGsmClient.h>

#define SerialAT Serial1
#define SerialMon Serial

#define LED_PIN 12
#define BAUD_RATE 115200

#include "modem_handler.h"
#include "gsm_handler.h"
#include "gps_handler.h"
#include "wifi_handler.h"
#include "loadcell_handler.h"
#include "ultrassonic_handler.h"
#include "database_handler.h"
#include "location_handler.h"
bool toggleLed(void);
#endif

wifi_handler.cpp

#include "wifi_handler.h"

const char* wifi_ssid = "hidden";
const char* wifi_passwd = "hidden";

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;
}

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
    }
}

wifi_handler.h

#ifndef WIFI_HANDLER_H
#define WIFI_HANDLER_H

#define SerialMon Serial

#include <Arduino.h>
#include <WiFi.h>

#define RETRY_INTERVAL 5000UL

extern const char* wifi_ssid;
extern const char* wifi_passwd;
extern WiFiClient client_wifi;

int connect_wifi(const char* ssid, const char* pwd);
int disconnect_wifi();
#endif

Edited;
Somehow the indentation seems a bit messed when the code was pasted here. :frowning:

If I remove the disconnect function and logic from smartbin.ino, the error doesn't show up!

Bump... Still waiting for any tips or suggestions!
Thanks

Apparently this is not an error but "an informative message" caused by turning off the wifi. See here:

Both of the two statements WiFi.disconnect(true) and WiFi.mode(WIFI_OFF) cause the message so I guess they both turn off the wifi so you probably don't need both.

I have Charger Doctor, a gizmo that plugs into the USB lead and shows the voltage and current being used. I took a look at the current draw for various situations using an ntpclient sketch that uses wifi and the same stmts:
Wifi off (either or both of the two statements) - 60mA
Wifi.disconnect(false) - the default, I don't know what this does - 60mA
Don't disconnect at all - 70mA

This is compared with a null sketch (empty setup and loop and no wifi) - 90mA

So it looks like the wifi doesn't contribute much if it's not being used (I am using an ESP32-WROOM-32 so may be different from yours).

I hope this helps.

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