mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-22 23:07:28 +02:00
213 lines
8.0 KiB
C
213 lines
8.0 KiB
C
/* PPPoS Client Example
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
*/
|
|
#include <string.h>
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/event_groups.h"
|
|
#include "esp_netif.h"
|
|
#include "esp_netif_ppp.h"
|
|
#include "mqtt_client.h"
|
|
#include "esp_modem_api.h"
|
|
#include "esp_log.h"
|
|
|
|
#define BROKER_URL "mqtt://mqtt.eclipseprojects.io"
|
|
|
|
static const char *TAG = "pppos_example";
|
|
static EventGroupHandle_t event_group = NULL;
|
|
static const int CONNECT_BIT = BIT0;
|
|
static const int GOT_DATA_BIT = BIT2;
|
|
|
|
static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
|
|
{
|
|
esp_mqtt_client_handle_t client = event->client;
|
|
int msg_id;
|
|
switch (event->event_id) {
|
|
case MQTT_EVENT_CONNECTED:
|
|
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
|
|
msg_id = esp_mqtt_client_subscribe(client, "/topic/esp-pppos", 0);
|
|
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
|
|
break;
|
|
case MQTT_EVENT_DISCONNECTED:
|
|
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
|
|
break;
|
|
case MQTT_EVENT_SUBSCRIBED:
|
|
ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
|
|
msg_id = esp_mqtt_client_publish(client, "/topic/esp-pppos", "esp32-pppos", 0, 0, 0);
|
|
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
|
|
break;
|
|
case MQTT_EVENT_UNSUBSCRIBED:
|
|
ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
|
|
break;
|
|
case MQTT_EVENT_PUBLISHED:
|
|
ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
|
|
break;
|
|
case MQTT_EVENT_DATA:
|
|
ESP_LOGI(TAG, "MQTT_EVENT_DATA");
|
|
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
|
|
printf("DATA=%.*s\r\n", event->data_len, event->data);
|
|
xEventGroupSetBits(event_group, GOT_DATA_BIT);
|
|
break;
|
|
case MQTT_EVENT_ERROR:
|
|
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
|
|
break;
|
|
default:
|
|
ESP_LOGI(TAG, "MQTT other event id: %d", event->event_id);
|
|
break;
|
|
}
|
|
return ESP_OK;
|
|
}
|
|
|
|
static void on_ppp_changed(void *arg, esp_event_base_t event_base,
|
|
int32_t event_id, void *event_data)
|
|
{
|
|
ESP_LOGI(TAG, "PPP state changed event %d", event_id);
|
|
if (event_id == NETIF_PPP_ERRORUSER) {
|
|
/* User interrupted event from esp-netif */
|
|
esp_netif_t *netif = event_data;
|
|
ESP_LOGI(TAG, "User interrupted event from netif:%p", netif);
|
|
}
|
|
}
|
|
|
|
|
|
static void on_ip_event(void *arg, esp_event_base_t event_base,
|
|
int32_t event_id, void *event_data)
|
|
{
|
|
ESP_LOGD(TAG, "IP event! %d", event_id);
|
|
if (event_id == IP_EVENT_PPP_GOT_IP) {
|
|
esp_netif_dns_info_t dns_info;
|
|
|
|
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
|
|
esp_netif_t *netif = event->esp_netif;
|
|
|
|
ESP_LOGI(TAG, "Modem Connect to PPP Server");
|
|
ESP_LOGI(TAG, "~~~~~~~~~~~~~~");
|
|
ESP_LOGI(TAG, "IP : " IPSTR, IP2STR(&event->ip_info.ip));
|
|
ESP_LOGI(TAG, "Netmask : " IPSTR, IP2STR(&event->ip_info.netmask));
|
|
ESP_LOGI(TAG, "Gateway : " IPSTR, IP2STR(&event->ip_info.gw));
|
|
esp_netif_get_dns_info(netif, 0, &dns_info);
|
|
ESP_LOGI(TAG, "Name Server1: " IPSTR, IP2STR(&dns_info.ip.u_addr.ip4));
|
|
esp_netif_get_dns_info(netif, 1, &dns_info);
|
|
ESP_LOGI(TAG, "Name Server2: " IPSTR, IP2STR(&dns_info.ip.u_addr.ip4));
|
|
ESP_LOGI(TAG, "~~~~~~~~~~~~~~");
|
|
xEventGroupSetBits(event_group, CONNECT_BIT);
|
|
|
|
ESP_LOGI(TAG, "GOT ip event!!!");
|
|
} else if (event_id == IP_EVENT_PPP_LOST_IP) {
|
|
ESP_LOGI(TAG, "Modem Disconnect from PPP Server");
|
|
} else if (event_id == IP_EVENT_GOT_IP6) {
|
|
ESP_LOGI(TAG, "GOT IPv6 event!");
|
|
|
|
ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
|
|
ESP_LOGI(TAG, "Got IPv6 address " IPV6STR, IPV62STR(event->ip6_info.ip));
|
|
}
|
|
}
|
|
|
|
|
|
void app_main(void)
|
|
{
|
|
|
|
/* Init and register system/core components */
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
ESP_ERROR_CHECK(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));
|
|
|
|
event_group = xEventGroupCreate();
|
|
|
|
/* Configure the DTE */
|
|
esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_CONFIG();
|
|
/* setup UART specific configuration based on kconfig options */
|
|
dte_config.uart_config.tx_io_num = CONFIG_EXAMPLE_MODEM_UART_TX_PIN;
|
|
dte_config.uart_config.rx_io_num = CONFIG_EXAMPLE_MODEM_UART_RX_PIN;
|
|
dte_config.uart_config.rts_io_num = CONFIG_EXAMPLE_MODEM_UART_RTS_PIN;
|
|
dte_config.uart_config.cts_io_num = CONFIG_EXAMPLE_MODEM_UART_CTS_PIN;
|
|
dte_config.uart_config.rx_buffer_size = CONFIG_EXAMPLE_MODEM_UART_RX_BUFFER_SIZE;
|
|
dte_config.uart_config.tx_buffer_size = CONFIG_EXAMPLE_MODEM_UART_TX_BUFFER_SIZE;
|
|
dte_config.uart_config.event_queue_size = CONFIG_EXAMPLE_MODEM_UART_EVENT_QUEUE_SIZE;
|
|
dte_config.task_stack_size = CONFIG_EXAMPLE_MODEM_UART_EVENT_TASK_STACK_SIZE;
|
|
dte_config.task_priority = CONFIG_EXAMPLE_MODEM_UART_EVENT_TASK_PRIORITY;
|
|
dte_config.dte_buffer_size = CONFIG_EXAMPLE_MODEM_UART_RX_BUFFER_SIZE / 2;
|
|
|
|
/* Configure the DCE */
|
|
esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(CONFIG_EXAMPLE_MODEM_PPP_APN);
|
|
|
|
/* Configure the PPP netif */
|
|
esp_netif_config_t netif_ppp_config = ESP_NETIF_DEFAULT_PPP();
|
|
|
|
/* Run the modem demo app */
|
|
// Init netif object
|
|
esp_netif_t *esp_netif = esp_netif_new(&netif_ppp_config);
|
|
assert(esp_netif);
|
|
esp_modem_dce_t *dce = esp_modem_new(&dte_config, &dce_config, esp_netif);
|
|
|
|
#if CONFIG_EXAMPLE_NEED_SIM_PIN == 1
|
|
// check if PIN needed
|
|
bool pin_ok = false;
|
|
if (esp_modem_read_pin(dce, &pin_ok) == ESP_OK && pin_ok == false) {
|
|
if (esp_modem_set_pin(dce, "1234") == ESP_OK) {
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
} else {
|
|
abort();
|
|
}
|
|
}
|
|
#endif
|
|
|
|
int rssi, ber;
|
|
esp_err_t err = esp_modem_get_signal_quality(dce, &rssi, &ber);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "esp_modem_get_signal_quality failed with %d", err);
|
|
return;
|
|
}
|
|
ESP_LOGI(TAG, "Signal quality: rssi=%d, ber=%d", rssi, ber);
|
|
|
|
#if CONFIG_EXAMPLE_SEND_MSG
|
|
if (esp_modem_sms_txt_mode(dce, true) != ESP_OK || esp_modem_sms_character_set(dce) != ESP_OK) {
|
|
ESP_LOGE(TAG, "Setting text mode or GSM character set failed");
|
|
return;
|
|
}
|
|
|
|
err = esp_modem_send_sms(dce, CONFIG_EXAMPLE_SEND_MSG_PEER_PHONE_NUMBER, "Text message from esp-modem");
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "esp_modem_send_sms() failed with %d", err);
|
|
return;
|
|
}
|
|
#endif
|
|
|
|
err = esp_modem_set_mode(dce, ESP_MODEM_MODE_DATA);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "esp_modem_set_mode(ESP_MODEM_MODE_DATA) failed with %d", err);
|
|
return;
|
|
}
|
|
/* 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);
|
|
err = esp_modem_set_mode(dce, ESP_MODEM_MODE_COMMAND);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "esp_modem_set_mode(ESP_MODEM_MODE_COMMAND) failed with %d", err);
|
|
return;
|
|
}
|
|
char imsi[32];
|
|
err = esp_modem_get_imsi(dce, imsi);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "esp_modem_get_imsi failed with %d", err);
|
|
return;
|
|
}
|
|
ESP_LOGI(TAG, "IMSI=%s", imsi);
|
|
|
|
esp_modem_destroy(dce);
|
|
esp_netif_destroy(esp_netif);
|
|
}
|