mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
examples: Add experimental modem example
* esp_modem cxx example to use esp_event_cxx (using only sync event) * Use 0.1.9 version of the esp_modem to support CMake 3.5 * Add supported targets ESP32 and ESP32s2 Closes https://github.com/espressif/esp-idf/issues/1847 Closes https://github.com/espressif/esp-idf/issues/4688 Closes https://github.com/espressif/esp-idf/issues/6637 Closes https://github.com/espressif/esp-idf/issues/5754 Closes https://github.com/espressif/esp-idf/issues/5722
This commit is contained in:
parent
825bbb08e3
commit
6cc07ecd58
8
examples/cxx/experimental/esp_modem_cxx/CMakeLists.txt
Normal file
8
examples/cxx/experimental/esp_modem_cxx/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
# The following lines of boilerplate have to be in your project's CMakeLists
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/cxx/experimental/experimental_cpp_component)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(simple_cxx_ppp_client)
|
18
examples/cxx/experimental/esp_modem_cxx/README.md
Normal file
18
examples/cxx/experimental/esp_modem_cxx/README.md
Normal file
@ -0,0 +1,18 @@
|
||||
| Supported Targets | ESP32 | ESP32-S2 |
|
||||
| ----------------- | ----- | -------- |
|
||||
|
||||
# Simple example of esp_modem component
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
|
||||
## Overview
|
||||
|
||||
This example demonstrates the use of the [esp-modem component](https://components.espressif.com/component/espressif/esp_modem) to connect to a network and send some AT commands.
|
||||
It uses modem CMUX mode so that commands and network could be used at the same time.
|
||||
|
||||
## About the esp_modem
|
||||
|
||||
Please check the component [README](managed_components/espressif__esp_modem/README.md)
|
||||
|
||||
Or refer to the component's [documentation](managed_components/espressif__esp_modem/docs/html/index.html)
|
@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "simple_client.cpp" "simple_mqtt_client.cpp"
|
||||
INCLUDE_DIRS ".")
|
@ -0,0 +1,42 @@
|
||||
menu "Example Configuration"
|
||||
|
||||
choice EXAMPLE_MODEM_DEVICE
|
||||
prompt "Choose supported modem device (DCE)"
|
||||
default EXAMPLE_MODEM_DEVICE_BG96
|
||||
help
|
||||
Select modem device connected to the ESP DTE.
|
||||
config EXAMPLE_MODEM_DEVICE_SIM800
|
||||
bool "SIM800"
|
||||
help
|
||||
SIMCom SIM800L is a GSM/GPRS module.
|
||||
It supports Quad-band 850/900/1800/1900MHz.
|
||||
config EXAMPLE_MODEM_DEVICE_BG96
|
||||
bool "BG96"
|
||||
help
|
||||
Quectel BG96 is a series of LTE Cat M1/Cat NB1/EGPRS module.
|
||||
config EXAMPLE_MODEM_DEVICE_SIM7600
|
||||
bool "SIM7600"
|
||||
help
|
||||
SIM7600 is a Multi-Band LTE-TDD/LTE-FDD/HSPA+ and GSM/GPRS/EDGE module.
|
||||
endchoice
|
||||
|
||||
config EXAMPLE_MODEM_PPP_APN
|
||||
string "Set MODEM APN"
|
||||
default "internet"
|
||||
help
|
||||
Set APN (Access Point Name), a logical name to choose data network
|
||||
|
||||
config EXAMPLE_NEED_SIM_PIN
|
||||
bool "SIM PIN needed"
|
||||
default n
|
||||
help
|
||||
Enable to set SIM PIN before starting the example
|
||||
|
||||
config EXAMPLE_SIM_PIN
|
||||
string "Set SIM PIN"
|
||||
default "1234"
|
||||
depends on EXAMPLE_NEED_SIM_PIN
|
||||
help
|
||||
Pin to unlock the SIM
|
||||
|
||||
endmenu
|
@ -0,0 +1,7 @@
|
||||
targets:
|
||||
- esp32
|
||||
- esp32s2
|
||||
description: cmux example of esp_modem
|
||||
dependencies:
|
||||
espressif/esp_modem:
|
||||
version: "0.1.9"
|
137
examples/cxx/experimental/esp_modem_cxx/main/simple_client.cpp
Normal file
137
examples/cxx/experimental/esp_modem_cxx/main/simple_client.cpp
Normal file
@ -0,0 +1,137 @@
|
||||
/* 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 <cstring>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_log.h"
|
||||
#include "cxx_include/esp_modem_dte.hpp"
|
||||
#include "esp_modem_config.h"
|
||||
#include "cxx_include/esp_modem_api.hpp"
|
||||
#include "esp_event_cxx.hpp"
|
||||
#include "simple_mqtt_client.hpp"
|
||||
|
||||
|
||||
#define BROKER_URL "mqtt://mqtt.eclipseprojects.io"
|
||||
|
||||
using namespace esp_modem;
|
||||
using namespace idf::event;
|
||||
|
||||
constexpr auto TAG = "cmux_example";
|
||||
|
||||
|
||||
extern "C" void app_main(void)
|
||||
{
|
||||
/* Init and register system/core components */
|
||||
auto loop = std::make_shared<ESPEventLoop>();
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
/* Configure the DTE */
|
||||
esp_modem_dte_config_t dte_config = ESP_MODEM_DTE_DEFAULT_CONFIG();
|
||||
|
||||
/* 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();
|
||||
|
||||
/* Create the DTE, PPP and DCE objects */
|
||||
auto uart_dte = create_uart_dte(&dte_config);
|
||||
assert(uart_dte);
|
||||
|
||||
esp_netif_t *esp_netif = esp_netif_new(&netif_ppp_config);
|
||||
assert(esp_netif);
|
||||
|
||||
#if CONFIG_EXAMPLE_MODEM_DEVICE_BG96 == 1
|
||||
std::unique_ptr<DCE> dce = create_BG96_dce(&dce_config, uart_dte, esp_netif);
|
||||
#elif CONFIG_EXAMPLE_MODEM_DEVICE_SIM800 == 1
|
||||
std::unique_ptr<DCE> dce = create_SIM800_dce(&dce_config, uart_dte, esp_netif);
|
||||
#elif CONFIG_EXAMPLE_MODEM_DEVICE_SIM7600 == 1
|
||||
std::unique_ptr<DCE> dce = create_SIM7600_dce(&dce_config, uart_dte, esp_netif);
|
||||
#else
|
||||
#error "Unsupported device"
|
||||
#endif
|
||||
assert(dce);
|
||||
|
||||
/* Setup basic operation mode for the DCE (pin if used, CMUX mode) */
|
||||
#if CONFIG_EXAMPLE_NEED_SIM_PIN == 1
|
||||
bool pin_ok = true;
|
||||
if (dce->read_pin(pin_ok) == command_result::OK && !pin_ok) {
|
||||
if (dce->set_pin(CONFIG_EXAMPLE_SIM_PIN) == command_result::OK) {
|
||||
vTaskDelay(pdMS_TO_TICKS(1000)); // Need to wait for some time after unlocking the SIM
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to set PIN... exiting");
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (dce->set_mode(esp_modem::modem_mode::CMUX_MODE) && dce->set_mode(esp_modem::modem_mode::DATA_MODE)) {
|
||||
ESP_LOGI(TAG, "Modem has correctly entered multiplexed command/data mode");
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to configure desired mode... exiting");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Read some data from the modem */
|
||||
std::string str;
|
||||
while (dce->get_operator_name(str) != esp_modem::command_result::OK) {
|
||||
// Getting operator name could fail... retry after 500 ms
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
}
|
||||
ESP_LOGI(TAG, "Operator name: %s", str.c_str());
|
||||
|
||||
/* Try to connect to the network and publish an mqtt topic */
|
||||
ESPEventHandlerSync event_handler(loop);
|
||||
event_handler.listen_to(ESPEvent(IP_EVENT, ESPEventID(ESP_EVENT_ANY_ID)));
|
||||
auto result = event_handler.wait_event_for(std::chrono::milliseconds(60000));
|
||||
if (result.timeout) {
|
||||
ESP_LOGE(TAG, "Cannot get IP within specified timeout... exiting");
|
||||
return;
|
||||
} else if (result.event.id == ESPEventID(IP_EVENT_PPP_GOT_IP)) {
|
||||
auto *event = (ip_event_got_ip_t *)result.ev_data;
|
||||
ESP_LOGI(TAG, "Got IP address");
|
||||
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));
|
||||
|
||||
/* When connected to network, subscribe and publish some MQTT data */
|
||||
MqttClient mqtt(BROKER_URL);
|
||||
event_handler.listen_to(MqttClient::get_event(MqttClient::Event::CONNECT));
|
||||
event_handler.listen_to(MqttClient::get_event(MqttClient::Event::DATA));
|
||||
|
||||
auto reg = loop->register_event(MqttClient::get_event(MqttClient::Event::DATA),
|
||||
[&mqtt](const ESPEvent &event, void *data) {
|
||||
ESP_LOGI(TAG, " TOPIC: %s", mqtt.get_topic(data).c_str());
|
||||
ESP_LOGI(TAG, " DATA: %s", mqtt.get_data(data).c_str());
|
||||
});
|
||||
mqtt.connect();
|
||||
while (true) {
|
||||
result = event_handler.wait_event_for(std::chrono::milliseconds(60000));
|
||||
if (result.event == MqttClient::get_event(MqttClient::Event::CONNECT)) {
|
||||
mqtt.subscribe("/topic/esp-modem");
|
||||
mqtt.publish("/topic/esp-modem", "Hello modem");
|
||||
continue;
|
||||
} else if (result.event == MqttClient::get_event(MqttClient::Event::DATA)) {
|
||||
ESP_LOGI(TAG, "Data received");
|
||||
break; /* Continue with CMUX example after getting data from MQTT */
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} else if (result.event.id == ESPEventID(IP_EVENT_PPP_LOST_IP)) {
|
||||
ESP_LOGE(TAG, "PPP client has lost connection... exiting");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Again reading some data from the modem */
|
||||
if (dce->get_imsi(str) == esp_modem::command_result::OK) {
|
||||
ESP_LOGI(TAG, "Modem IMSI number: %s", str.c_str());
|
||||
}
|
||||
}
|
@ -0,0 +1,101 @@
|
||||
/* 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 <memory>
|
||||
#include "mqtt_client.h"
|
||||
#include "esp_event_cxx.hpp"
|
||||
#include "simple_mqtt_client.hpp"
|
||||
|
||||
using namespace idf::event;
|
||||
|
||||
/**
|
||||
* Reference to the MQTT event base
|
||||
*/
|
||||
ESP_EVENT_DECLARE_BASE(MQTT_EVENTS);
|
||||
|
||||
/**
|
||||
* Thin wrapper around C mqtt_client
|
||||
*/
|
||||
struct MqttClientHandle
|
||||
{
|
||||
explicit MqttClientHandle(const std::string & uri)
|
||||
{
|
||||
esp_mqtt_client_config_t config = { };
|
||||
config.uri = uri.c_str();
|
||||
client = esp_mqtt_client_init(&config);
|
||||
esp_mqtt_client_register_event(client, MQTT_EVENT_ANY, mqtt_event_handler, this);
|
||||
}
|
||||
|
||||
~MqttClientHandle()
|
||||
{
|
||||
esp_mqtt_client_destroy(client);
|
||||
}
|
||||
|
||||
static void mqtt_event_handler(void *args, esp_event_base_t base, int32_t id, void *data)
|
||||
{
|
||||
// forwards the internal event to the global ESPEvent
|
||||
esp_event_post(base, id, data, sizeof(esp_mqtt_event_t), portMAX_DELAY);
|
||||
}
|
||||
|
||||
esp_mqtt_client_handle_t client;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Definitions of MqttClient methods
|
||||
*/
|
||||
MqttClient::MqttClient(const std::string & uri):
|
||||
h(std::unique_ptr<MqttClientHandle>(new MqttClientHandle(uri)))
|
||||
{}
|
||||
|
||||
void MqttClient::connect()
|
||||
{
|
||||
esp_mqtt_client_start(h->client);
|
||||
}
|
||||
|
||||
idf::event::ESPEvent MqttClient::get_event(MqttClient::Event ev)
|
||||
{
|
||||
switch (ev) {
|
||||
case Event::CONNECT: {
|
||||
return { MQTT_EVENTS, ESPEventID(MQTT_EVENT_CONNECTED) };
|
||||
}
|
||||
case Event::DATA:
|
||||
return { MQTT_EVENTS, ESPEventID(MQTT_EVENT_DATA) };
|
||||
}
|
||||
return { };
|
||||
}
|
||||
|
||||
int MqttClient::publish(const std::string &topic, const std::string &data, int qos)
|
||||
{
|
||||
return esp_mqtt_client_publish(h->client, topic.c_str(), data.c_str(), 0, qos, 0);
|
||||
}
|
||||
|
||||
int MqttClient::subscribe(const std::string &topic, int qos)
|
||||
{
|
||||
return esp_mqtt_client_subscribe(h->client, topic.c_str(), qos);
|
||||
}
|
||||
|
||||
std::string MqttClient::get_topic(void * event_data)
|
||||
{
|
||||
auto event = (esp_mqtt_event_handle_t)event_data;
|
||||
if (event == nullptr || event->client != h->client)
|
||||
return {};
|
||||
|
||||
return std::string(event->topic, event->topic_len);
|
||||
}
|
||||
|
||||
std::string MqttClient::get_data(void * event_data)
|
||||
{
|
||||
auto event = (esp_mqtt_event_handle_t)event_data;
|
||||
if (event == nullptr || event->client != h->client)
|
||||
return {};
|
||||
|
||||
return std::string(event->data, event->data_len);
|
||||
}
|
||||
|
||||
MqttClient::~MqttClient() = default;
|
@ -0,0 +1,77 @@
|
||||
/* 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.
|
||||
*/
|
||||
#ifndef _SIMPLE_MQTT_CLIENT_H_
|
||||
#define _SIMPLE_MQTT_CLIENT_H_
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include "esp_event_cxx.hpp"
|
||||
|
||||
struct MqttClientHandle;
|
||||
|
||||
/**
|
||||
* @brief Simple MQTT client wrapper
|
||||
*/
|
||||
class MqttClient {
|
||||
public:
|
||||
enum class Event {
|
||||
CONNECT,
|
||||
DATA,
|
||||
};
|
||||
|
||||
explicit MqttClient(const std::string & uri);
|
||||
~MqttClient();
|
||||
|
||||
/**
|
||||
* @brief Start the mqtt-client
|
||||
*/
|
||||
void connect();
|
||||
|
||||
/**
|
||||
* @brief Publish to topic
|
||||
* @param topic Topic to publish
|
||||
* @param data Data to publish
|
||||
* @param qos QoS (0 by default)
|
||||
* @return message id
|
||||
*/
|
||||
int publish(const std::string & topic, const std::string & data, int qos = 0);
|
||||
|
||||
/**
|
||||
* @brief Subscribe to a topic
|
||||
* @param topic Topic to subscribe
|
||||
* @param qos QoS (0 by default)
|
||||
* @return message id
|
||||
*/
|
||||
int subscribe(const std::string & topic, int qos = 0);
|
||||
|
||||
/**
|
||||
* @brief Get topic from event data
|
||||
* @return String topic
|
||||
*/
|
||||
std::string get_topic(void *);
|
||||
|
||||
/**
|
||||
* @brief Get published data from event
|
||||
* @return String representation of the data
|
||||
*/
|
||||
std::string get_data(void *);
|
||||
|
||||
/**
|
||||
* @brief Convert internal MQTT event to standard ESPEvent
|
||||
* @param ev internal mqtt event
|
||||
* @return corresponding ESPEvent
|
||||
*/
|
||||
static idf::event::ESPEvent get_event(Event ev);
|
||||
|
||||
private:
|
||||
std::unique_ptr<MqttClientHandle> h;
|
||||
};
|
||||
|
||||
|
||||
#endif //_SIMPLE_MQTT_CLIENT_H_
|
@ -0,0 +1,8 @@
|
||||
# Override some defaults to enable PPP
|
||||
CONFIG_LWIP_PPP_SUPPORT=y
|
||||
CONFIG_LWIP_PPP_NOTIFY_PHASE_SUPPORT=y
|
||||
CONFIG_LWIP_PPP_PAP_SUPPORT=y
|
||||
CONFIG_LWIP_TCPIP_TASK_STACK_SIZE=4096
|
||||
# Do not enable IPV6 in dte<->dce link local
|
||||
CONFIG_LWIP_PPP_ENABLE_IPV6=n
|
||||
CONFIG_COMPILER_CXX_EXCEPTIONS=y
|
Loading…
Reference in New Issue
Block a user