experimental/mqtt_cxx: Adds a C++ Mqtt client wrapper

- Base class with separated event handlers for each Mqtt client event.
- Topic matcher added to support data events.
- Filter class to allow only mqtt valid topic filters.
- Initial code for unit test on the host.
This commit is contained in:
Euripedes Rocha Filho
2020-12-15 13:10:10 +00:00
committed by Euripedes Rocha
parent 9fb01ca534
commit 231392d0c6
21 changed files with 1084 additions and 0 deletions

View File

@ -0,0 +1,12 @@
# The following four 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)
# (Not part of the boilerplate)
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common
$ENV{IDF_PATH}/examples/cxx/experimental/experimental_cpp_component
$ENV{IDF_PATH}/examples/cxx/experimental/esp_mqtt_cxx/components)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(mqtt_tcp_cxx)

View File

@ -0,0 +1,12 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#
PROJECT_NAME := mqtt_tcp_cxx
EXTRA_COMPONENT_DIRS := $(IDF_PATH)/examples/common_components/protocol_examples_common
EXTRA_COMPONENT_DIRS += $(IDF_PATH)/examples/cxx/experimental/esp_mqtt_cxx/components
EXTRA_COMPONENT_DIRS += $(IDF_PATH)/examples/cxx/experimental/experimental_cpp_component
CXXFLAGS += -std=gnu++17
include $(IDF_PATH)/make/project.mk

View File

@ -0,0 +1,3 @@
idf_component_register(SRCS "mqtt_tcp_example.cpp"
INCLUDE_DIRS ".")
target_compile_options(${COMPONENT_LIB} PRIVATE "-std=gnu++17")

View File

@ -0,0 +1,9 @@
menu "Example Configuration"
config BROKER_URL
string "Broker URL"
default "mqtt://mqtt.eclipse.org"
help
URL of the broker to connect to
endmenu

View File

@ -0,0 +1,81 @@
/* C++ MQTT (over TCP) 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 "nvs_flash.h"
#include "protocol_examples_common.h"
#include "esp_log.h"
#include "esp_mqtt.hpp"
#include "esp_mqtt_client_config.hpp"
namespace mqtt = idf::mqtt;
namespace {
constexpr auto *TAG = "MQTT_EXAMPLE";
class MyClient final : public mqtt::Client {
public:
using mqtt::Client::Client;
private:
void on_connected(esp_mqtt_event_handle_t const event) override
{
using mqtt::QoS;
subscribe(messages.get());
subscribe(sent_load.get(), QoS::AtMostOnce);
}
void on_data(esp_mqtt_event_handle_t const event) override
{
if (messages.match(event->topic, event->topic_len)) {
ESP_LOGI(TAG, "Received in the messages topic");
}
}
mqtt::Filter messages{"$SYS/broker/messages/received"};
mqtt::Filter sent_load{"$SYS/broker/load/+/sent"};
};
}
extern "C" void app_main(void)
{
ESP_LOGI(TAG, "[APP] Startup..");
ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
esp_log_level_set("*", ESP_LOG_INFO);
esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE);
esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE);
esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE);
esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE);
esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE);
esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE);
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
* Read "Establishing Wi-Fi or Ethernet Connection" section in
* examples/protocols/README.md for more information about this function.
*/
ESP_ERROR_CHECK(example_connect());
mqtt::BrokerConfiguration broker{
.address = {mqtt::URI{std::string{CONFIG_BROKER_URL}}},
.security = mqtt::Insecure{}
};
mqtt::ClientCredentials credentials{};
mqtt::Configuration config{};
MyClient client{broker, credentials, config};
while (true) {
constexpr TickType_t xDelay = 500 / portTICK_PERIOD_MS;
vTaskDelay(xDelay);
}
}

View File

@ -0,0 +1,3 @@
# Enable C++ exceptions and set emergency pool size for exception objects
CONFIG_COMPILER_CXX_EXCEPTIONS=y
CONFIG_COMPILER_CXX_EXCEPTIONS_EMG_POOL_SIZE=1024