From 6d70b7c352d8cf0340ac14e127372a7a83f4779a Mon Sep 17 00:00:00 2001 From: morris Date: Tue, 26 May 2020 13:54:57 +0800 Subject: [PATCH 1/2] fix esp_modem switch mode timeout Closes https://github.com/espressif/esp-idf/issues/3506 Closes https://github.com/espressif/esp-idf/issues/4324 --- .../pppos_client/components/modem/include/esp_modem_dce.h | 2 +- .../protocols/pppos_client/components/modem/src/esp_modem.c | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/protocols/pppos_client/components/modem/include/esp_modem_dce.h b/examples/protocols/pppos_client/components/modem/include/esp_modem_dce.h index a0633cb86a..7c180b4214 100644 --- a/examples/protocols/pppos_client/components/modem/include/esp_modem_dce.h +++ b/examples/protocols/pppos_client/components/modem/include/esp_modem_dce.h @@ -52,7 +52,7 @@ typedef struct modem_dte modem_dte_t; */ #define MODEM_COMMAND_TIMEOUT_DEFAULT (500) /*!< Default timeout value for most commands */ #define MODEM_COMMAND_TIMEOUT_OPERATOR (75000) /*!< Timeout value for getting operator status */ -#define MODEM_COMMAND_TIMEOUT_MODE_CHANGE (3000) /*!< Timeout value for changing working mode */ +#define MODEM_COMMAND_TIMEOUT_MODE_CHANGE (5000) /*!< Timeout value for changing working mode */ #define MODEM_COMMAND_TIMEOUT_HANG_UP (90000) /*!< Timeout value for hang up */ #define MODEM_COMMAND_TIMEOUT_POWEROFF (1000) /*!< Timeout value for power down */ diff --git a/examples/protocols/pppos_client/components/modem/src/esp_modem.c b/examples/protocols/pppos_client/components/modem/src/esp_modem.c index eb41771860..928fbbfa51 100644 --- a/examples/protocols/pppos_client/components/modem/src/esp_modem.c +++ b/examples/protocols/pppos_client/components/modem/src/esp_modem.c @@ -412,6 +412,8 @@ modem_dte_t *esp_modem_dte_init(const esp_modem_dte_config_t *config) res = uart_driver_install(esp_dte->uart_port, config->rx_buffer_size, config->tx_buffer_size, config->event_queue_size, &(esp_dte->event_queue), 0); MODEM_CHECK(res == ESP_OK, "install uart driver failed", err_uart_config); + res = uart_set_rx_timeout(esp_dte->uart_port, 1); + MODEM_CHECK(res == ESP_OK, "set rx timeout failed", err_uart_config); /* Set pattern interrupt, used to detect the end of a line. */ res = uart_enable_pattern_det_baud_intr(esp_dte->uart_port, '\n', 1, MIN_PATTERN_INTERVAL, MIN_POST_IDLE, MIN_PRE_IDLE); From 0fcb4477071e5f281bfb7e75ae345f4d3d48e4ad Mon Sep 17 00:00:00 2001 From: morris Date: Tue, 26 May 2020 16:24:26 +0800 Subject: [PATCH 2/2] add pppos client restart in example Closes https://github.com/espressif/esp-idf/issues/4268 --- examples/protocols/pppos_client/README.md | 2 +- .../pppos_client/main/pppos_client_main.c | 125 ++++++++++-------- 2 files changed, 70 insertions(+), 57 deletions(-) diff --git a/examples/protocols/pppos_client/README.md b/examples/protocols/pppos_client/README.md index a449a261c7..0fe442c22c 100644 --- a/examples/protocols/pppos_client/README.md +++ b/examples/protocols/pppos_client/README.md @@ -52,7 +52,7 @@ See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/l ## Example Output -The example will get module and operator's information after start up, and then go into PPP mode to start mqtt client operations. This example will also send a short message to someone's phone if you have enabled this feature in menuconfig. +The example will get module and operator's information after start up, and then go into PPP mode to start mqtt client operations. This example will also send a short message to someone's phone if you have enabled this feature in menuconfig. The PPP connection will get restarted after 60 seconds. ### BG96 Output diff --git a/examples/protocols/pppos_client/main/pppos_client_main.c b/examples/protocols/pppos_client/main/pppos_client_main.c index 9d6ab93d25..179590b7eb 100644 --- a/examples/protocols/pppos_client/main/pppos_client_main.c +++ b/examples/protocols/pppos_client/main/pppos_client_main.c @@ -167,7 +167,7 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) } static void on_ppp_changed(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) + int32_t event_id, void *event_data) { ESP_LOGI(TAG, "PPP state changed event %d", event_id); if (event_id == NETIF_PPP_ERRORUSER) { @@ -179,7 +179,7 @@ static void on_ppp_changed(void *arg, esp_event_base_t event_base, static void on_ip_event(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) + int32_t event_id, void *event_data) { ESP_LOGD(TAG, "IP event! %d", event_id); if (event_id == IP_EVENT_PPP_GOT_IP) { @@ -227,11 +227,6 @@ void app_main(void) event_group = xEventGroupCreate(); - // Init netif object - esp_netif_config_t cfg = ESP_NETIF_DEFAULT_PPP(); - esp_netif_t *esp_netif = esp_netif_new(&cfg); - assert(esp_netif); - /* create dte object */ esp_modem_dte_config_t config = ESP_MODEM_DTE_DEFAULT_CONFIG(); /* setup UART specific configuration based on kconfig options */ @@ -245,67 +240,85 @@ void app_main(void) config.event_queue_size = CONFIG_EXAMPLE_MODEM_UART_EVENT_QUEUE_SIZE; config.event_task_stack_size = CONFIG_EXAMPLE_MODEM_UART_EVENT_TASK_STACK_SIZE; config.event_task_priority = CONFIG_EXAMPLE_MODEM_UART_EVENT_TASK_PRIORITY; - config.line_buffer_size = CONFIG_EXAMPLE_MODEM_UART_RX_BUFFER_SIZE/2; + config.line_buffer_size = CONFIG_EXAMPLE_MODEM_UART_RX_BUFFER_SIZE / 2; + modem_dte_t *dte = esp_modem_dte_init(&config); /* Register event handler */ ESP_ERROR_CHECK(esp_modem_set_event_handler(dte, modem_event_handler, ESP_EVENT_ANY_ID, NULL)); - /* create dce object */ + + // Init netif object + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_PPP(); + esp_netif_t *esp_netif = esp_netif_new(&cfg); + assert(esp_netif); + + void *modem_netif_adapter = esp_modem_netif_setup(dte); + esp_modem_netif_set_default_handlers(modem_netif_adapter, esp_netif); + + while (1) { + modem_dce_t *dce = NULL; + /* create dce object */ #if CONFIG_EXAMPLE_MODEM_DEVICE_SIM800 - modem_dce_t *dce = sim800_init(dte); + dce = sim800_init(dte); #elif CONFIG_EXAMPLE_MODEM_DEVICE_BG96 - modem_dce_t *dce = bg96_init(dte); + dce = bg96_init(dte); #else #error "Unsupported DCE" #endif - assert(dce != NULL); - ESP_ERROR_CHECK(dce->set_flow_ctrl(dce, MODEM_FLOW_CONTROL_NONE)); - ESP_ERROR_CHECK(dce->store_profile(dce)); - /* Print Module ID, Operator, IMEI, IMSI */ - ESP_LOGI(TAG, "Module: %s", dce->name); - ESP_LOGI(TAG, "Operator: %s", dce->oper); - ESP_LOGI(TAG, "IMEI: %s", dce->imei); - ESP_LOGI(TAG, "IMSI: %s", dce->imsi); - /* Get signal quality */ - uint32_t rssi = 0, ber = 0; - ESP_ERROR_CHECK(dce->get_signal_quality(dce, &rssi, &ber)); - ESP_LOGI(TAG, "rssi: %d, ber: %d", rssi, ber); - /* Get battery voltage */ - uint32_t voltage = 0, bcs = 0, bcl = 0; - ESP_ERROR_CHECK(dce->get_battery_status(dce, &bcs, &bcl, &voltage)); - ESP_LOGI(TAG, "Battery voltage: %d mV", voltage); - /* setup PPPoS network parameters */ + assert(dce != NULL); + ESP_ERROR_CHECK(dce->set_flow_ctrl(dce, MODEM_FLOW_CONTROL_NONE)); + ESP_ERROR_CHECK(dce->store_profile(dce)); + /* Print Module ID, Operator, IMEI, IMSI */ + ESP_LOGI(TAG, "Module: %s", dce->name); + ESP_LOGI(TAG, "Operator: %s", dce->oper); + ESP_LOGI(TAG, "IMEI: %s", dce->imei); + ESP_LOGI(TAG, "IMSI: %s", dce->imsi); + /* Get signal quality */ + uint32_t rssi = 0, ber = 0; + ESP_ERROR_CHECK(dce->get_signal_quality(dce, &rssi, &ber)); + ESP_LOGI(TAG, "rssi: %d, ber: %d", rssi, ber); + /* Get battery voltage */ + uint32_t voltage = 0, bcs = 0, bcl = 0; + ESP_ERROR_CHECK(dce->get_battery_status(dce, &bcs, &bcl, &voltage)); + ESP_LOGI(TAG, "Battery voltage: %d mV", voltage); + /* setup PPPoS network parameters */ #if !defined(CONFIG_EXAMPLE_MODEM_PPP_AUTH_NONE) && (defined(CONFIG_LWIP_PPP_PAP_SUPPORT) || defined(CONFIG_LWIP_PPP_CHAP_SUPPORT)) - esp_netif_ppp_set_auth(esp_netif, auth_type, CONFIG_EXAMPLE_MODEM_PPP_AUTH_USERNAME, CONFIG_EXAMPLE_MODEM_PPP_AUTH_PASSWORD); + esp_netif_ppp_set_auth(esp_netif, auth_type, CONFIG_EXAMPLE_MODEM_PPP_AUTH_USERNAME, CONFIG_EXAMPLE_MODEM_PPP_AUTH_PASSWORD); #endif - void *modem_netif_adapter = esp_modem_netif_setup(dte); - esp_modem_netif_set_default_handlers(modem_netif_adapter, esp_netif); - /* attach the modem to the network interface */ - esp_netif_attach(esp_netif, modem_netif_adapter); - /* Wait for IP address */ - xEventGroupWaitBits(event_group, CONNECT_BIT, pdTRUE, pdTRUE, portMAX_DELAY); - /* Config MQTT */ - esp_mqtt_client_config_t mqtt_config = { - .uri = BROKER_URL, - .event_handle = mqtt_event_handler, - }; - esp_mqtt_client_handle_t mqtt_client = esp_mqtt_client_init(&mqtt_config); - esp_mqtt_client_start(mqtt_client); - xEventGroupWaitBits(event_group, GOT_DATA_BIT, pdTRUE, pdTRUE, portMAX_DELAY); - esp_mqtt_client_destroy(mqtt_client); - /* Exit PPP mode */ - ESP_ERROR_CHECK(esp_modem_stop_ppp(dte)); + /* attach the modem to the network interface */ + esp_netif_attach(esp_netif, modem_netif_adapter); + /* Wait for IP address */ + xEventGroupWaitBits(event_group, CONNECT_BIT, pdTRUE, pdTRUE, portMAX_DELAY); + + /* Config MQTT */ + esp_mqtt_client_config_t mqtt_config = { + .uri = BROKER_URL, + .event_handle = mqtt_event_handler, + }; + esp_mqtt_client_handle_t mqtt_client = esp_mqtt_client_init(&mqtt_config); + esp_mqtt_client_start(mqtt_client); + xEventGroupWaitBits(event_group, GOT_DATA_BIT, pdTRUE, pdTRUE, portMAX_DELAY); + esp_mqtt_client_destroy(mqtt_client); + + /* Exit PPP mode */ + ESP_ERROR_CHECK(esp_modem_stop_ppp(dte)); + + xEventGroupWaitBits(event_group, STOP_BIT, pdTRUE, pdTRUE, portMAX_DELAY); +#if CONFIG_EXAMPLE_SEND_MSG + const char *message = "Welcome to ESP32!"; + ESP_ERROR_CHECK(example_send_message_text(dce, CONFIG_EXAMPLE_SEND_MSG_PEER_PHONE_NUMBER, message)); + ESP_LOGI(TAG, "Send send message [%s] ok", message); +#endif + /* Power down module */ + ESP_ERROR_CHECK(dce->power_down(dce)); + ESP_LOGI(TAG, "Power down"); + ESP_ERROR_CHECK(dce->deinit(dce)); + + ESP_LOGI(TAG, "Restart after 60 seconds"); + vTaskDelay(pdMS_TO_TICKS(60000)); + } + /* Destroy the netif adapter withe events, which internally frees also the esp-netif instance */ esp_modem_netif_clear_default_handlers(modem_netif_adapter); esp_modem_netif_teardown(modem_netif_adapter); - xEventGroupWaitBits(event_group, STOP_BIT, pdTRUE, pdTRUE, portMAX_DELAY); -#if CONFIG_EXAMPLE_SEND_MSG - const char *message = "Welcome to ESP32!"; - ESP_ERROR_CHECK(example_send_message_text(dce, CONFIG_EXAMPLE_SEND_MSG_PEER_PHONE_NUMBER, message)); - ESP_LOGI(TAG, "Send send message [%s] ok", message); -#endif - /* Power down module */ - ESP_ERROR_CHECK(dce->power_down(dce)); - ESP_LOGI(TAG, "Power down"); - ESP_ERROR_CHECK(dce->deinit(dce)); ESP_ERROR_CHECK(dte->deinit(dte)); }