Skip to content

Commit 0fcb447

Browse files
committed
add pppos client restart in example
Closes espressif/esp-idf#4268
1 parent 6d70b7c commit 0fcb447

File tree

2 files changed

+70
-57
lines changed

2 files changed

+70
-57
lines changed

examples/protocols/pppos_client/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/l
5252

5353
## Example Output
5454

55-
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.
55+
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.
5656

5757
### BG96 Output
5858

examples/protocols/pppos_client/main/pppos_client_main.c

Lines changed: 69 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
167167
}
168168

169169
static void on_ppp_changed(void *arg, esp_event_base_t event_base,
170-
int32_t event_id, void *event_data)
170+
int32_t event_id, void *event_data)
171171
{
172172
ESP_LOGI(TAG, "PPP state changed event %d", event_id);
173173
if (event_id == NETIF_PPP_ERRORUSER) {
@@ -179,7 +179,7 @@ static void on_ppp_changed(void *arg, esp_event_base_t event_base,
179179

180180

181181
static void on_ip_event(void *arg, esp_event_base_t event_base,
182-
int32_t event_id, void *event_data)
182+
int32_t event_id, void *event_data)
183183
{
184184
ESP_LOGD(TAG, "IP event! %d", event_id);
185185
if (event_id == IP_EVENT_PPP_GOT_IP) {
@@ -227,11 +227,6 @@ void app_main(void)
227227

228228
event_group = xEventGroupCreate();
229229

230-
// Init netif object
231-
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_PPP();
232-
esp_netif_t *esp_netif = esp_netif_new(&cfg);
233-
assert(esp_netif);
234-
235230
/* create dte object */
236231
esp_modem_dte_config_t config = ESP_MODEM_DTE_DEFAULT_CONFIG();
237232
/* setup UART specific configuration based on kconfig options */
@@ -245,67 +240,85 @@ void app_main(void)
245240
config.event_queue_size = CONFIG_EXAMPLE_MODEM_UART_EVENT_QUEUE_SIZE;
246241
config.event_task_stack_size = CONFIG_EXAMPLE_MODEM_UART_EVENT_TASK_STACK_SIZE;
247242
config.event_task_priority = CONFIG_EXAMPLE_MODEM_UART_EVENT_TASK_PRIORITY;
248-
config.line_buffer_size = CONFIG_EXAMPLE_MODEM_UART_RX_BUFFER_SIZE/2;
243+
config.line_buffer_size = CONFIG_EXAMPLE_MODEM_UART_RX_BUFFER_SIZE / 2;
244+
249245
modem_dte_t *dte = esp_modem_dte_init(&config);
250246
/* Register event handler */
251247
ESP_ERROR_CHECK(esp_modem_set_event_handler(dte, modem_event_handler, ESP_EVENT_ANY_ID, NULL));
252-
/* create dce object */
248+
249+
// Init netif object
250+
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_PPP();
251+
esp_netif_t *esp_netif = esp_netif_new(&cfg);
252+
assert(esp_netif);
253+
254+
void *modem_netif_adapter = esp_modem_netif_setup(dte);
255+
esp_modem_netif_set_default_handlers(modem_netif_adapter, esp_netif);
256+
257+
while (1) {
258+
modem_dce_t *dce = NULL;
259+
/* create dce object */
253260
#if CONFIG_EXAMPLE_MODEM_DEVICE_SIM800
254-
modem_dce_t *dce = sim800_init(dte);
261+
dce = sim800_init(dte);
255262
#elif CONFIG_EXAMPLE_MODEM_DEVICE_BG96
256-
modem_dce_t *dce = bg96_init(dte);
263+
dce = bg96_init(dte);
257264
#else
258265
#error "Unsupported DCE"
259266
#endif
260-
assert(dce != NULL);
261-
ESP_ERROR_CHECK(dce->set_flow_ctrl(dce, MODEM_FLOW_CONTROL_NONE));
262-
ESP_ERROR_CHECK(dce->store_profile(dce));
263-
/* Print Module ID, Operator, IMEI, IMSI */
264-
ESP_LOGI(TAG, "Module: %s", dce->name);
265-
ESP_LOGI(TAG, "Operator: %s", dce->oper);
266-
ESP_LOGI(TAG, "IMEI: %s", dce->imei);
267-
ESP_LOGI(TAG, "IMSI: %s", dce->imsi);
268-
/* Get signal quality */
269-
uint32_t rssi = 0, ber = 0;
270-
ESP_ERROR_CHECK(dce->get_signal_quality(dce, &rssi, &ber));
271-
ESP_LOGI(TAG, "rssi: %d, ber: %d", rssi, ber);
272-
/* Get battery voltage */
273-
uint32_t voltage = 0, bcs = 0, bcl = 0;
274-
ESP_ERROR_CHECK(dce->get_battery_status(dce, &bcs, &bcl, &voltage));
275-
ESP_LOGI(TAG, "Battery voltage: %d mV", voltage);
276-
/* setup PPPoS network parameters */
267+
assert(dce != NULL);
268+
ESP_ERROR_CHECK(dce->set_flow_ctrl(dce, MODEM_FLOW_CONTROL_NONE));
269+
ESP_ERROR_CHECK(dce->store_profile(dce));
270+
/* Print Module ID, Operator, IMEI, IMSI */
271+
ESP_LOGI(TAG, "Module: %s", dce->name);
272+
ESP_LOGI(TAG, "Operator: %s", dce->oper);
273+
ESP_LOGI(TAG, "IMEI: %s", dce->imei);
274+
ESP_LOGI(TAG, "IMSI: %s", dce->imsi);
275+
/* Get signal quality */
276+
uint32_t rssi = 0, ber = 0;
277+
ESP_ERROR_CHECK(dce->get_signal_quality(dce, &rssi, &ber));
278+
ESP_LOGI(TAG, "rssi: %d, ber: %d", rssi, ber);
279+
/* Get battery voltage */
280+
uint32_t voltage = 0, bcs = 0, bcl = 0;
281+
ESP_ERROR_CHECK(dce->get_battery_status(dce, &bcs, &bcl, &voltage));
282+
ESP_LOGI(TAG, "Battery voltage: %d mV", voltage);
283+
/* setup PPPoS network parameters */
277284
#if !defined(CONFIG_EXAMPLE_MODEM_PPP_AUTH_NONE) && (defined(CONFIG_LWIP_PPP_PAP_SUPPORT) || defined(CONFIG_LWIP_PPP_CHAP_SUPPORT))
278-
esp_netif_ppp_set_auth(esp_netif, auth_type, CONFIG_EXAMPLE_MODEM_PPP_AUTH_USERNAME, CONFIG_EXAMPLE_MODEM_PPP_AUTH_PASSWORD);
285+
esp_netif_ppp_set_auth(esp_netif, auth_type, CONFIG_EXAMPLE_MODEM_PPP_AUTH_USERNAME, CONFIG_EXAMPLE_MODEM_PPP_AUTH_PASSWORD);
279286
#endif
280-
void *modem_netif_adapter = esp_modem_netif_setup(dte);
281-
esp_modem_netif_set_default_handlers(modem_netif_adapter, esp_netif);
282-
/* attach the modem to the network interface */
283-
esp_netif_attach(esp_netif, modem_netif_adapter);
284-
/* Wait for IP address */
285-
xEventGroupWaitBits(event_group, CONNECT_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
286-
/* Config MQTT */
287-
esp_mqtt_client_config_t mqtt_config = {
288-
.uri = BROKER_URL,
289-
.event_handle = mqtt_event_handler,
290-
};
291-
esp_mqtt_client_handle_t mqtt_client = esp_mqtt_client_init(&mqtt_config);
292-
esp_mqtt_client_start(mqtt_client);
293-
xEventGroupWaitBits(event_group, GOT_DATA_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
294-
esp_mqtt_client_destroy(mqtt_client);
295-
/* Exit PPP mode */
296-
ESP_ERROR_CHECK(esp_modem_stop_ppp(dte));
287+
/* attach the modem to the network interface */
288+
esp_netif_attach(esp_netif, modem_netif_adapter);
289+
/* Wait for IP address */
290+
xEventGroupWaitBits(event_group, CONNECT_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
291+
292+
/* Config MQTT */
293+
esp_mqtt_client_config_t mqtt_config = {
294+
.uri = BROKER_URL,
295+
.event_handle = mqtt_event_handler,
296+
};
297+
esp_mqtt_client_handle_t mqtt_client = esp_mqtt_client_init(&mqtt_config);
298+
esp_mqtt_client_start(mqtt_client);
299+
xEventGroupWaitBits(event_group, GOT_DATA_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
300+
esp_mqtt_client_destroy(mqtt_client);
301+
302+
/* Exit PPP mode */
303+
ESP_ERROR_CHECK(esp_modem_stop_ppp(dte));
304+
305+
xEventGroupWaitBits(event_group, STOP_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
306+
#if CONFIG_EXAMPLE_SEND_MSG
307+
const char *message = "Welcome to ESP32!";
308+
ESP_ERROR_CHECK(example_send_message_text(dce, CONFIG_EXAMPLE_SEND_MSG_PEER_PHONE_NUMBER, message));
309+
ESP_LOGI(TAG, "Send send message [%s] ok", message);
310+
#endif
311+
/* Power down module */
312+
ESP_ERROR_CHECK(dce->power_down(dce));
313+
ESP_LOGI(TAG, "Power down");
314+
ESP_ERROR_CHECK(dce->deinit(dce));
315+
316+
ESP_LOGI(TAG, "Restart after 60 seconds");
317+
vTaskDelay(pdMS_TO_TICKS(60000));
318+
}
319+
297320
/* Destroy the netif adapter withe events, which internally frees also the esp-netif instance */
298321
esp_modem_netif_clear_default_handlers(modem_netif_adapter);
299322
esp_modem_netif_teardown(modem_netif_adapter);
300-
xEventGroupWaitBits(event_group, STOP_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
301-
#if CONFIG_EXAMPLE_SEND_MSG
302-
const char *message = "Welcome to ESP32!";
303-
ESP_ERROR_CHECK(example_send_message_text(dce, CONFIG_EXAMPLE_SEND_MSG_PEER_PHONE_NUMBER, message));
304-
ESP_LOGI(TAG, "Send send message [%s] ok", message);
305-
#endif
306-
/* Power down module */
307-
ESP_ERROR_CHECK(dce->power_down(dce));
308-
ESP_LOGI(TAG, "Power down");
309-
ESP_ERROR_CHECK(dce->deinit(dce));
310323
ESP_ERROR_CHECK(dte->deinit(dte));
311324
}

0 commit comments

Comments
 (0)