Switch from WiFi to LTE modem when WiFi connection is lost

alqine
Posts: 8
Joined: Thu Jun 24, 2021 8:14 pm

Switch from WiFi to LTE modem when WiFi connection is lost

Postby alqine » Thu Jun 24, 2021 8:39 pm

Hi all,

I am trying to write code based on ESP-IDF library, which should do two things. First of all, connect to WiFi network specified by settings. And then, when connection is lost, device should automatically switch to LTE modem. However there are few problems I am facing:

1) I am not sure what is the better way to implement the switching. Should both Wifi and LTE modem initialize it's own netifs at the same time? Is the switchning between the two as simple as setting default netif?
Or is better way to init only one netif (Wifi or modem) based on current usage?

2) When using multiple netifs (as described in point 1) is there some mechanism, which would loop through all interfaces available to find one, which is working? So I could just init netif for Wifi and LTE and in case of Wifi error, this "mechanism" would automatically switch to LTE, which would be the second initialized interface. Would this switch back to Wifi, when it starts working again?

3) In case of WiFi error, disconnect etc., how should the switch be implemented? I suppose the only way I can get information about the Wifi not working properly is handler registered via esp_event_handler_instance_register for WIFI_EVENT base. But I think this handler is running in different thread than my code. Wouldn't initialization of LTE connection from this thread ruin things? Because this thread would be ended soon after LTE init:

Code: Select all

static void handler(void *argument, esp_event_base_t event_base, int32_t event_id, void *event_data) {
    if (event_base == WIFI_EVENT) {
        switch (event_id) {
	    // ... other events ...
	    
            case WIFI_EVENT_STA_DISCONNECTED:
                if (retries < WIFI_MAX_RETRIES) {
                    esp_wifi_connect();
                    retries++;
                } else {
                    // Connection failed...
                    // Would this work? Switch to modem would stop wifi and stop this event handler...
                    switch_to_modem();
                }
            break;
        }
    }
}
The same problem exists for LTE -> WIFI switching. This may happen, when user sends message to the device, that WiFi network is working again. Implemented method should be working both ways: Wifi -> LTE and LTE -> Wifi.

Am I missing something? Is there better way to handle this problem?

Thank you :)

sohaib.q
Posts: 3
Joined: Fri Jun 21, 2024 2:26 pm

Re: Switch from WiFi to LTE modem when WiFi connection is lost

Postby sohaib.q » Fri Jun 21, 2024 2:30 pm

Hello , I hope you are doing well.
Can you share your findings regarding the problem you were facing, i know it's too old, just trying my luck to get reply from you.
I am trying exactly same thing, switching between wifi and LTE (EG91) using ESP USB Modem.

Who is online

Users browsing this forum: doragasu, Google [Bot], jimmy98035@gmail.com and 102 guests