Multiple netif at the same time for MQTT (WiFi & PPP)

JulianMauro
Posts: 4
Joined: Sun Jan 05, 2025 11:04 pm

Multiple netif at the same time for MQTT (WiFi & PPP)

Postby JulianMauro » Wed Jun 04, 2025 3:30 am

Hello,
We are using WiFi and PPP (LTE CAT-M from a SIM7080G) with the ESP_MODEM component. Since connectivity is crucial, the application must be always connected to either networks.

I've read this post and watched Cermak's video in which talked about having 2 netifs at the same time with different priorities, such that the netif with the highest priority is always the default.

My idea is to initialize both networks, first WiFi and the LTE, set WiFi priority higher so that on success connection, it uses WiFi, or LTE if WiFi fails. Both networks have their respective event_handlers, which will trigger the "switching event" to switch from one network to another.

The idea here is to avoid deleting and recreating netif objects as it causes crashes with PPP. Netif objects are created only once and its the switch event the one responsible of putting them up/down.

However, I have some questions:

1. What is the correct way to keep the netif interface but make it "down" so that applications (HTTP, MQTT) uses the other one? Or is that an automatic process of esp-netif?
2. Is MQTT agnostic of the default interface? Meaning that is it required to destroy and re-create the client each time the highest priority interface changes?
3. Since both WiFi and PPP use the same event loop (esp_event_loop_create_default), is there any problems if both things run at the same time? I do not want any collision on the events.


This is part of the code used in the PPP (SIM 7080G over UART)

Code: Select all

    esp_err_t err;

    esp_event_loop_create_default();
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &on_ip_event, NULL));
    ESP_ERROR_CHECK(esp_event_handler_register(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed, NULL));

    ESP_ERROR_CHECK(lwpa_init_gpio_pwr());

    lpwa_power_on();
    
    esp_netif_inherent_config_t ppp_inherent_config = ESP_NETIF_INHERENT_DEFAULT_PPP();
    ppp_inherent_config.route_prio = 5; // PPP priority is lower than WiFi. WiFi connection is preferred.
    esp_netif_config_t ppp_netif_config = {
        .base = &ppp_inherent_config,
        .driver = NULL,
        .stack = ESP_NETIF_NETSTACK_DEFAULT_PPP
    };

    esp_netif = esp_netif_new(&ppp_netif_config);
    assert(esp_netif);

    esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(providers_apn[config_task->config->provider]);

    event_group = xEventGroupCreate();
    xEventGroupClearBits(event_group, LPWA_CONNECTED_BIT | LPWA_WEAK_SIGNAL_BIT | LPWA_FAILED_BIT); 

    esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_CONFIG();
    dte_config.uart_config.tx_io_num = UART_TX_PIN;
    dte_config.uart_config.rx_io_num = UART_RX_PIN;
    dte_config.uart_config.port_num = UART_NUM_0;
    dte_config.uart_config.flow_control = ESP_MODEM_FLOW_CONTROL_NONE;
    dte_config.uart_config.data_bits = UART_DATA_8_BITS;
    dte_config.uart_config.stop_bits = UART_STOP_BITS_1;
    dte_config.uart_config.parity = UART_PARITY_DISABLE;
    dte_config.uart_config.baud_rate = 115200;
    dte_config.uart_config.rx_buffer_size = 4096;
    dte_config.uart_config.tx_buffer_size = 512;
    dte_config.uart_config.event_queue_size = 30;

    dte_config.task_stack_size = 6144;
    dte_config.dte_buffer_size = 512;

    // We are using driver for SIM7070, most of the commands are compatible with SIM7080
    dce = esp_modem_new_dev(ESP_MODEM_DCE_SIM7070, &dte_config, &dce_config, esp_netif);
    if (dce == NULL)
    {
        SYSTEM_LOG(ERROR, LPWA, "Cannot create the LPWA modem device.");
        xEventGroupSetBits(event_group, LPWA_FAILED_BIT);
    }

This is part of the WiFi implementation:

Code: Select all

   
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    err = esp_wifi_init(&cfg);
    if (err != ESP_OK) { return err; }


    /* Custom Netif to have higher routing priority */
    esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
    esp_netif_config.route_prio = 20; // Higher than PPP


    esp_netif_config_t netif_cfg = {
        .base = &esp_netif_config,
        .driver = ((void *)0),
        .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA
    };

    esp_netif_t *netif = esp_netif_new(&cfg);
    assert(netif);
    ESP_ERROR_CHECK(esp_netif_attach_wifi_station(netif));
    ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers());
    
    ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));

    ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));

Who is online

Users browsing this forum: Semrush [Bot] and 3 guests