Wi-Fi Auto Reconnect

Does Zephyr support automatic reconnection of the Wi-Fi if a connection is lost?  I can't find any documentation on this, or code examples.  It seems like every example connects to WiFi on boot, and doesn't have any provision to retry it if the connection is lost (e.g.- if the Access Point goes down due to a power glitch).

I can write my own handler that gets triggered by the connection lost event, but I'm hoping there is something built into Zephyr, or a best-practices example somewhere.

Parents
  • I ended up just calling wifi_connect() from the disconnect event handler to try to reconnect after any unintentional disconnects.  I tested it by turning the Wi-Fi Access Point (hub) off for a while, and it seems to work well.  To move the wifi_connect() call out of the interrupt context, it is run from the work queue:

    static void process_reconnect(struct k_work *work) {
        wifi_connect();
    }
    K_WORK_DEFINE(reconnect_work, process_reconnect);
    

    This is added to the work queue from the interrupt disconnect event handler:

    static void handle_wifi_disconnect_result(struct net_mgmt_event_callback *cb)
    {
    	const struct wifi_status *status =
    		(const struct wifi_status *) cb->info;
    
    	if (context.disconnect_requested) {
    		LOG_INF("Disconnection request %s (%d)",
    			 status->status ? "failed" : "done",
    					status->status);
    		context.disconnect_requested = false;
    	} else {
    		LOG_INF("Disconnected");
    		context.connected = false;
    		wifi_active_off();
            // Since disconnect was not requested, try to reconnect
            k_work_submit(&reconnect_work);
    	}
    
    	cmd_wifi_status();
    }
    

Reply
  • I ended up just calling wifi_connect() from the disconnect event handler to try to reconnect after any unintentional disconnects.  I tested it by turning the Wi-Fi Access Point (hub) off for a while, and it seems to work well.  To move the wifi_connect() call out of the interrupt context, it is run from the work queue:

    static void process_reconnect(struct k_work *work) {
        wifi_connect();
    }
    K_WORK_DEFINE(reconnect_work, process_reconnect);
    

    This is added to the work queue from the interrupt disconnect event handler:

    static void handle_wifi_disconnect_result(struct net_mgmt_event_callback *cb)
    {
    	const struct wifi_status *status =
    		(const struct wifi_status *) cb->info;
    
    	if (context.disconnect_requested) {
    		LOG_INF("Disconnection request %s (%d)",
    			 status->status ? "failed" : "done",
    					status->status);
    		context.disconnect_requested = false;
    	} else {
    		LOG_INF("Disconnected");
    		context.connected = false;
    		wifi_active_off();
            // Since disconnect was not requested, try to reconnect
            k_work_submit(&reconnect_work);
    	}
    
    	cmd_wifi_status();
    }
    

Children
No Data
Related