|
| 1 | +/* PPPoS Client Example |
| 2 | +
|
| 3 | + This example code is in the Public Domain (or CC0 licensed, at your option.) |
| 4 | +
|
| 5 | + Unless required by applicable law or agreed to in writing, this |
| 6 | + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 7 | + CONDITIONS OF ANY KIND, either express or implied. |
| 8 | +*/ |
| 9 | +#include <string> |
| 10 | +#include "freertos/FreeRTOS.h" |
| 11 | +#include "freertos/event_groups.h" |
| 12 | +#include "esp_netif.h" |
| 13 | +#include "esp_netif_ppp.h" |
| 14 | +#include "mqtt_client.h" |
| 15 | +#include "esp_modem_config.h" |
| 16 | +#include "cxx_include/esp_modem_api.hpp" |
| 17 | +#include "sock_dce.hpp" |
| 18 | +#include "esp_log.h" |
| 19 | + |
| 20 | +#define BROKER_URL "mqtt://mqtt.eclipseprojects.io" |
| 21 | + |
| 22 | +static const char *TAG = "modem_client"; |
| 23 | +static EventGroupHandle_t event_group = NULL; |
| 24 | +static const int CONNECT_BIT = BIT0; |
| 25 | +static const int GOT_DATA_BIT = BIT2; |
| 26 | + |
| 27 | +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) |
| 28 | +{ |
| 29 | + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); |
| 30 | + esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)event_data; |
| 31 | + esp_mqtt_client_handle_t client = event->client; |
| 32 | + int msg_id; |
| 33 | + switch ((esp_mqtt_event_id_t)event_id) { |
| 34 | + case MQTT_EVENT_CONNECTED: |
| 35 | + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); |
| 36 | + msg_id = esp_mqtt_client_subscribe(client, "/topic/esp-pppos", 0); |
| 37 | + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); |
| 38 | + break; |
| 39 | + case MQTT_EVENT_DISCONNECTED: |
| 40 | + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); |
| 41 | + break; |
| 42 | + case MQTT_EVENT_SUBSCRIBED: |
| 43 | + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); |
| 44 | + msg_id = esp_mqtt_client_publish(client, "/topic/esp-pppos", "esp32-pppos", 0, 0, 0); |
| 45 | + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); |
| 46 | + break; |
| 47 | + case MQTT_EVENT_UNSUBSCRIBED: |
| 48 | + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); |
| 49 | + break; |
| 50 | + case MQTT_EVENT_PUBLISHED: |
| 51 | + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); |
| 52 | + break; |
| 53 | + case MQTT_EVENT_DATA: |
| 54 | + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); |
| 55 | + printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); |
| 56 | + printf("DATA=%.*s\r\n", event->data_len, event->data); |
| 57 | + xEventGroupSetBits(event_group, GOT_DATA_BIT); |
| 58 | + break; |
| 59 | + case MQTT_EVENT_ERROR: |
| 60 | + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); |
| 61 | + break; |
| 62 | + default: |
| 63 | + ESP_LOGI(TAG, "MQTT other event id: %d", event->event_id); |
| 64 | + break; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +extern "C" void app_main(void) |
| 76 | +{ |
| 77 | + |
| 78 | + /* Init and register system/core components */ |
| 79 | + ESP_ERROR_CHECK(esp_netif_init()); |
| 80 | + ESP_ERROR_CHECK(esp_event_loop_create_default()); |
| 81 | + |
| 82 | + event_group = xEventGroupCreate(); |
| 83 | + |
| 84 | + /* Configure and create the UART DTE */ |
| 85 | + esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_CONFIG(); |
| 86 | + /* setup UART specific configuration based on kconfig options */ |
| 87 | + dte_config.uart_config.tx_io_num = CONFIG_EXAMPLE_MODEM_UART_TX_PIN; |
| 88 | + dte_config.uart_config.rx_io_num = CONFIG_EXAMPLE_MODEM_UART_RX_PIN; |
| 89 | + dte_config.uart_config.rts_io_num = CONFIG_EXAMPLE_MODEM_UART_RTS_PIN; |
| 90 | + dte_config.uart_config.cts_io_num = CONFIG_EXAMPLE_MODEM_UART_CTS_PIN; |
| 91 | + dte_config.uart_config.rx_buffer_size = CONFIG_EXAMPLE_MODEM_UART_RX_BUFFER_SIZE; |
| 92 | + dte_config.uart_config.tx_buffer_size = CONFIG_EXAMPLE_MODEM_UART_TX_BUFFER_SIZE; |
| 93 | + dte_config.uart_config.event_queue_size = CONFIG_EXAMPLE_MODEM_UART_EVENT_QUEUE_SIZE; |
| 94 | + dte_config.task_stack_size = CONFIG_EXAMPLE_MODEM_UART_EVENT_TASK_STACK_SIZE*2; |
| 95 | + dte_config.task_priority = CONFIG_EXAMPLE_MODEM_UART_EVENT_TASK_PRIORITY; |
| 96 | + dte_config.dte_buffer_size = CONFIG_EXAMPLE_MODEM_UART_RX_BUFFER_SIZE / 2; |
| 97 | + |
| 98 | + auto dte = esp_modem::create_uart_dte(&dte_config); |
| 99 | + assert(dte); |
| 100 | + |
| 101 | + /* Configure the DCE */ |
| 102 | + esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(CONFIG_EXAMPLE_MODEM_APN); |
| 103 | + |
| 104 | + /* create the DCE and initialize network manually (using AT commands) */ |
| 105 | + auto dce = sock_dce::create(&dce_config, std::move(dte)); |
| 106 | + if (!dce->init_network()) { |
| 107 | + ESP_LOGE(TAG, "Failed to setup network"); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + dce->init(1883); |
| 112 | + esp_mqtt_client_config_t mqtt_config = {}; |
| 113 | + mqtt_config.broker.address.uri = "mqtt://127.0.0.1"; |
| 114 | + mqtt_config.session.message_retransmit_timeout = 10000; |
| 115 | + esp_mqtt_client_handle_t mqtt_client = esp_mqtt_client_init(&mqtt_config); |
| 116 | + esp_mqtt_client_register_event(mqtt_client, static_cast<esp_mqtt_event_id_t>(ESP_EVENT_ANY_ID), mqtt_event_handler, NULL); |
| 117 | + esp_mqtt_client_start(mqtt_client); |
| 118 | + if (!dce->start("test.mosquitto.org", 1883)) { |
| 119 | + ESP_LOGE(TAG, "Failed to start DCE"); |
| 120 | + return; |
| 121 | + } |
| 122 | + while (1) { |
| 123 | + while (dce->perform()) { |
| 124 | + ESP_LOGD(TAG, "...performing"); |
| 125 | + } |
| 126 | + ESP_LOGE(TAG, "Loop exit.. retrying"); |
| 127 | + // handle disconnections errors |
| 128 | + if (!dce->init_network()) { |
| 129 | + ESP_LOGE(TAG, "Failed to reinit network"); |
| 130 | + return; |
| 131 | + } |
| 132 | + if (!dce->start("test.mosquitto.org", 1883)) { |
| 133 | + ESP_LOGI(TAG, "Network reinitialized, retrying"); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments