mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
Merge branch 'feature/update_mqtt_client' into 'master'
[MQTT] - Updates esp_mqtt configuration struct See merge request espressif/esp-idf!18101
This commit is contained in:
commit
e7ccaaf9ca
@ -1 +1 @@
|
||||
Subproject commit 89e5c6014f8dbcfcd98af35fb507ca7b96ac8aee
|
||||
Subproject commit ae53d799da294f03ef65c33e88fa33648e638134
|
@ -95,7 +95,7 @@ TEST_CASE_METHOD(ClientInitializedFixture, "Client Start")
|
||||
{
|
||||
SECTION("Successful start") {
|
||||
esp_mqtt_client_config_t config{};
|
||||
config.uri = "mqtt://1.1.1.1";
|
||||
config.broker.address.uri = "mqtt://1.1.1.1";
|
||||
struct http_parser_url ret_uri = {
|
||||
.field_set = 1 | (1<<1),
|
||||
.port = 0,
|
||||
|
@ -38,7 +38,7 @@ TEST_CASE("mqtt init with invalid url", "[mqtt][leaks=0]")
|
||||
{
|
||||
test_leak_setup(__FILE__, __LINE__);
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = "INVALID",
|
||||
.broker.address.uri = "INVALID",
|
||||
};
|
||||
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
|
||||
TEST_ASSERT_EQUAL(NULL, client );
|
||||
@ -49,7 +49,7 @@ TEST_CASE("mqtt init and deinit", "[mqtt][leaks=0]")
|
||||
test_leak_setup(__FILE__, __LINE__);
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
// no connection takes place, but the uri has to be valid for init() to succeed
|
||||
.uri = "mqtts://localhost:8883",
|
||||
.broker.address.uri = "mqtts://localhost:8883",
|
||||
};
|
||||
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
|
||||
TEST_ASSERT_NOT_EQUAL(NULL, client );
|
||||
@ -73,7 +73,7 @@ TEST_CASE("mqtt enqueue and destroy outbox", "[mqtt][leaks=0]")
|
||||
const int size = 2000;
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
// no connection takes place, but the uri has to be valid for init() to succeed
|
||||
.uri = "mqtts://localhost:8883",
|
||||
.broker.address.uri = "mqtts://localhost:8883",
|
||||
};
|
||||
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
|
||||
TEST_ASSERT_NOT_EQUAL(NULL, client );
|
||||
|
@ -101,8 +101,8 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
|
||||
bool mqtt_connect_disconnect(void)
|
||||
{
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = CONFIG_MQTT_TEST_BROKER_URI,
|
||||
.disable_auto_reconnect = true,
|
||||
.broker.address.uri = CONFIG_MQTT_TEST_BROKER_URI,
|
||||
.network.disable_auto_reconnect = true,
|
||||
};
|
||||
s_event_group = xEventGroupCreate();
|
||||
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
|
||||
@ -122,7 +122,7 @@ bool mqtt_connect_disconnect(void)
|
||||
bool mqtt_subscribe_publish(void)
|
||||
{
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = CONFIG_MQTT_TEST_BROKER_URI,
|
||||
.broker.address.uri = CONFIG_MQTT_TEST_BROKER_URI,
|
||||
};
|
||||
char* topic = append_mac("topic");
|
||||
TEST_ASSERT_TRUE(NULL != topic);
|
||||
@ -152,16 +152,16 @@ bool mqtt_lwt_clean_disconnect(void)
|
||||
char* lwt = append_mac("lwt");
|
||||
TEST_ASSERT_TRUE(lwt);
|
||||
const esp_mqtt_client_config_t mqtt_cfg1 = {
|
||||
.uri = CONFIG_MQTT_TEST_BROKER_URI,
|
||||
.set_null_client_id = true,
|
||||
.lwt_topic = lwt,
|
||||
.lwt_msg = "lwt_msg"
|
||||
.broker.address.uri = CONFIG_MQTT_TEST_BROKER_URI,
|
||||
.credentials.set_null_client_id = true,
|
||||
.session.last_will.topic = lwt,
|
||||
.session.last_will.msg = "lwt_msg"
|
||||
};
|
||||
const esp_mqtt_client_config_t mqtt_cfg2 = {
|
||||
.uri = CONFIG_MQTT_TEST_BROKER_URI,
|
||||
.set_null_client_id = true,
|
||||
.lwt_topic = lwt,
|
||||
.lwt_msg = "lwt_msg"
|
||||
.broker.address.uri = CONFIG_MQTT_TEST_BROKER_URI,
|
||||
.credentials.set_null_client_id = true,
|
||||
.session.last_will.topic = lwt,
|
||||
.session.last_will.msg = "lwt_msg"
|
||||
};
|
||||
s_event_group = xEventGroupCreate();
|
||||
|
||||
@ -201,8 +201,8 @@ bool mqtt_lwt_clean_disconnect(void)
|
||||
bool mqtt_subscribe_payload(void)
|
||||
{
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = CONFIG_MQTT_TEST_BROKER_URI,
|
||||
.disable_auto_reconnect = true,
|
||||
.broker.address.uri = CONFIG_MQTT_TEST_BROKER_URI,
|
||||
.network.disable_auto_reconnect = true,
|
||||
};
|
||||
char* topic = append_mac("topic");
|
||||
TEST_ASSERT_TRUE(NULL != topic);
|
||||
|
@ -4,7 +4,7 @@ ESP-MQTT
|
||||
Overview
|
||||
--------
|
||||
|
||||
ESP-MQTT is an implementation of MQTT protocol client (MQTT is a lightweight publish/subscribe messaging protocol).
|
||||
ESP-MQTT is an implementation of [MQTT](mqtt.org) protocol client (MQTT is a lightweight publish/subscribe messaging protocol).
|
||||
|
||||
|
||||
Features
|
||||
@ -19,17 +19,40 @@ Application Example
|
||||
-------------------
|
||||
|
||||
* :example:`protocols/mqtt/tcp`: MQTT over tcp, default port 1883
|
||||
* :example:`protocols/mqtt/ssl`: MQTT over tcp, default port 8883
|
||||
* :example:`protocols/mqtt/ssl_psk`: MQTT over tcp using pre-shared keys for authentication, default port 8883
|
||||
* :example:`protocols/mqtt/ssl`: MQTT over tls, default port 8883
|
||||
* :example:`protocols/mqtt/ssl_ds`: MQTT over tls using digital signature peripheral for authentication, default port 8883.
|
||||
* :example:`protocols/mqtt/ssl_mutual_auth`: MQTT over tls using certificates for authentication, default port 8883
|
||||
* :example:`protocols/mqtt/ssl_psk`: MQTT over tls using pre-shared keys for authentication, default port 8883.
|
||||
* :example:`protocols/mqtt/ws`: MQTT over Websocket, default port 80
|
||||
* :example:`protocols/mqtt/wss`: MQTT over Websocket Secure, default port 443
|
||||
|
||||
|
||||
Configuration
|
||||
-------------
|
||||
URI
|
||||
^^^
|
||||
|
||||
The configuration is made by setting fields in ``esp_mqtt_client_config_t`` struct. The configuration struct has the following sub structs to configure different aspects of the client operation.
|
||||
|
||||
* :cpp:member:`broker<esp_mqtt_client_config::broker>` - Allow to set address and security verification.
|
||||
* :cpp:member:`credentials<esp_mqtt_client_config::credentials>` - Client credentials for authentication.
|
||||
* :cpp:member:`session<esp_mqtt_client_config::session>` - Configuration for MQTT session aspects.
|
||||
* :cpp:member:`network<esp_mqtt_client_config::network>` - Networking related configuration.
|
||||
* :cpp:member:`task<esp_mqtt_client_config::task>` - Allow to configure FreeRTOS task.
|
||||
* :cpp:member:`buffer<esp_mqtt_client_config::buffer>` - Buffer size for input and output.
|
||||
|
||||
In the following session the most common aspects are detailed.
|
||||
|
||||
Broker
|
||||
^^^^^^^^^^^
|
||||
|
||||
===========
|
||||
Address
|
||||
===========
|
||||
|
||||
Broker address can be set by usage of ``broker.address`` struct. The configuration can be made by usage of ``uri`` field
|
||||
or the combination of ``hostname``, ``transport`` and ``port``. Optionally, `path` could be set, this field is useful in
|
||||
websocket connections.
|
||||
|
||||
The ``uri`` field is used in the following format ``scheme://hostname:port/path``.
|
||||
- Curently support ``mqtt``, ``mqtts``, ``ws``, ``wss`` schemes
|
||||
- MQTT over TCP samples:
|
||||
|
||||
@ -56,8 +79,7 @@ URI
|
||||
.. code:: c
|
||||
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = "mqtt://mqtt.eclipseprojects.io",
|
||||
// .user_context = (void *)your_context
|
||||
.broker.address.uri = "mqtt://mqtt.eclipseprojects.io",
|
||||
};
|
||||
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
|
||||
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
|
||||
@ -65,9 +87,13 @@ URI
|
||||
|
||||
- Note: By default mqtt client uses event loop library to post related mqtt events (connected, subscribed, published, etc.)
|
||||
|
||||
============
|
||||
Verification
|
||||
============
|
||||
|
||||
SSL
|
||||
^^^
|
||||
For secure connections TLS is used, and to guarantee Broker's identity the ``broker.verification`` struct must be set.
|
||||
The broker certificate may be set in PEM or DER format. To select DER the equivalent ``_len`` field must be set,
|
||||
otherwise a NULL terminated string in PEM format should be provided to ``certificate`` field.
|
||||
|
||||
- Get certificate from server, example: ``mqtt.eclipseprojects.io``
|
||||
``openssl s_client -showcerts -connect mqtt.eclipseprojects.io:8883 </dev/null 2>/dev/null|openssl x509 -outform PEM >mqtt_eclipse_org.pem``
|
||||
@ -77,58 +103,54 @@ SSL
|
||||
.. code:: c
|
||||
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = "mqtts://mqtt.eclipseprojects.io:8883",
|
||||
.event_handle = mqtt_event_handler,
|
||||
.cert_pem = (const char *)mqtt_eclipse_org_pem_start,
|
||||
.broker = {
|
||||
.address.uri = "mqtts://mqtt.eclipseprojects.io:8883",
|
||||
.verification.certificate = (const char *)mqtt_eclipse_org_pem_start,
|
||||
},
|
||||
};
|
||||
|
||||
If the certificate is not null-terminated then ``cert_len`` should also be set.
|
||||
Other SSL related configuration parameters are:
|
||||
To details on other fields check the Reference API and :ref:`esp_tls_server_verification`.
|
||||
|
||||
* ``use_global_ca_store``: use the global certificate store to verify server certificate, see :component_file:`esp-tls/esp_tls.h` for more information
|
||||
* ``client_cert_pem``: pointer to certificate data in PEM or DER format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed.
|
||||
* ``client_cert_len``: length of the buffer pointed to by client_cert_pem. May be 0 for null-terminated pem.
|
||||
* ``client_key_pem``: pointer to private key data in PEM or DER format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed.
|
||||
* ``client_key_len``: length of the buffer pointed to by client_key_pem. May be 0 for null-terminated pem.
|
||||
* ``psk_hint_key``: pointer to PSK struct defined in esp_tls.h to enable PSK authentication (as alternative to certificate verification). If not NULL and server/client certificates are NULL, PSK is enabled
|
||||
* ``alpn_protos``: NULL-terminated list of protocols to be used for ALPN.
|
||||
Client Credentials
|
||||
^^^^^^^^^^^^^^^^^^
|
||||
|
||||
Last Will and Testament
|
||||
^^^^^^^^^^^^^^^^^^^^^^^
|
||||
MQTT allows for a last will and testament (LWT) message to notify other clients when a client ungracefully disconnects. This is configured by the following fields
|
||||
in the ``esp_mqtt_client_config_t``-struct.
|
||||
All client related credentials are under the ``credentials`` field.
|
||||
|
||||
* ``lwt_topic``: pointer to the LWT message topic
|
||||
* ``lwt_msg``: pointer to the LWT message
|
||||
* ``lwt_msg_len``: length of the LWT message, required if ``lwt_msg`` is not null-terminated
|
||||
* ``lwt_qos``: quality of service for the LWT message
|
||||
* ``lwt_retain``: specifies the retain flag of the LWT message
|
||||
|
||||
Other Configuration Parameters
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
* ``disable_clean_session``: determines the clean session flag for the connect message, defaults to a clean session
|
||||
* ``keepalive``: determines how many seconds the client will wait for a ping response before disconnecting, default is 120 seconds.
|
||||
* ``disable_auto_reconnect``: enable to stop the client from reconnecting to server after errors or disconnects
|
||||
* ``user_context``: custom context that will be passed to the event handler
|
||||
* ``task_prio``: MQTT task priority, defaults to 5
|
||||
* ``task_stack``: MQTT task stack size, defaults to 6144 bytes, setting this will override setting from menuconfig
|
||||
* ``buffer_size``: size of MQTT send/receive buffer, default is 1024 bytes
|
||||
* ``username``: pointer to the username used for connecting to the broker
|
||||
* ``password``: pointer to the password used for connecting to the broker
|
||||
* ``username``: pointer to the username used for connecting to the broker, can also be set by URI.
|
||||
* ``client_id``: pointer to the client id, defaults to ``ESP32_%CHIPID%`` where %CHIPID% are the last 3 bytes of MAC address in hex format
|
||||
* ``host``: MQTT broker domain (ipv4 as string), setting the uri will override this
|
||||
* ``port``: MQTT broker port, specifying the port in the uri will override this
|
||||
* ``transport``: sets the transport protocol, setting the uri will override this
|
||||
* ``refresh_connection_after_ms``: refresh connection after this value (in milliseconds)
|
||||
* ``event_handle``: handle for MQTT events as a callback in legacy mode
|
||||
* ``event_loop_handle``: handle for MQTT event loop library
|
||||
|
||||
==============
|
||||
Authentication
|
||||
==============
|
||||
|
||||
It's possible to set authentication parameters through the ``authentication`` field. The client supports the following authentication methods:
|
||||
|
||||
For more options on ``esp_mqtt_client_config_t``, please refer to API reference below
|
||||
* Using a password by setting ``authentication.password``.
|
||||
* Muthual authentication with TLS by setting ``authentication.certificate`` and ``authentication.key``, both can be provided in PEM or DER format.
|
||||
* Using secure element available in ESP32-WROOM-32SE, setting ``authentication.use_secure_element``.
|
||||
* Using Digital Signature Peripheral available in some Espressif devices, setting ``authentication.ds_data``.
|
||||
|
||||
Session
|
||||
^^^^^^^^^^^
|
||||
|
||||
For MQTT session related configurations ``section`` fields should be used.
|
||||
|
||||
=======================
|
||||
Last Will and Testament
|
||||
=======================
|
||||
|
||||
MQTT allows for a last will and testament (LWT) message to notify other clients when a client ungracefully disconnects. This is configured by the following fields
|
||||
in the ``esp_mqtt_client_config_t.session.last_will``-struct.
|
||||
|
||||
* ``topic``: pointer to the LWT message topic
|
||||
* ``msg``: pointer to the LWT message
|
||||
* ``msg_len``: length of the LWT message, required if ``msg`` is not null-terminated
|
||||
* ``qos``: quality of service for the LWT message
|
||||
* ``retain``: specifies the retain flag of the LWT message
|
||||
|
||||
Change settings in Project Configuration Menu
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
The settings for MQTT can be found using ``idf.py menuconfig``, under Component config -> ESP-MQTT Configuration
|
||||
|
||||
The following settings are available:
|
||||
@ -153,8 +175,6 @@ The following events may be posted by the MQTT client:
|
||||
* ``MQTT_EVENT_DATA``: The client has received a publish message. The event data contains: message ID, name of the topic it was published to, received data and its length. For data that exceeds the internal buffer multiple `MQTT_EVENT_DATA` will be posted and `current_data_offset` and `total_data_len` from event data updated to keep track of the fragmented message.
|
||||
* ``MQTT_EVENT_ERROR``: The client has encountered an error. `esp_mqtt_error_type_t` from `error_handle` in the event data can be used to further determine the type of the error. The type of error will determine which parts of the `error_handle` struct is filled.
|
||||
|
||||
|
||||
|
||||
API Reference
|
||||
-------------
|
||||
|
||||
|
@ -156,3 +156,18 @@ Breaking Changes (Summary)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
- The function :cpp:func:`esp_transport_read` now returns ``0`` for a connection timeout and ``< 0`` for other errors. Please refer :cpp:enum:`esp_tcp_transport_err_t` for all possible return values.
|
||||
|
||||
|
||||
MQTT Client
|
||||
-----------
|
||||
|
||||
Breaking Changes (Summary)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
- :cpp:type:`esp_mqtt_client_config_t` have all fields grouped in sub structs.
|
||||
|
||||
Most common configurations are listed below:
|
||||
|
||||
- Broker address now is set in :cpp:member:`esp_mqtt_client_config_t::broker::address::uri`
|
||||
- Security related to broker verification in :cpp:member:`esp_mqtt_client_config_t::broker::verification`
|
||||
- Client username is set in :cpp:member:`esp_mqtt_client_config_t::credentials::username`
|
||||
|
@ -27,7 +27,7 @@ struct MqttClientHandle
|
||||
explicit MqttClientHandle(const std::string & uri)
|
||||
{
|
||||
esp_mqtt_client_config_t config = { };
|
||||
config.uri = uri.c_str();
|
||||
config.broker.address.uri = uri.c_str();
|
||||
client = esp_mqtt_client_init(&config);
|
||||
esp_mqtt_client_register_event(client, MQTT_EVENT_ANY, mqtt_event_handler, this);
|
||||
}
|
||||
|
@ -36,13 +36,13 @@ void config_broker(esp_mqtt_client_config_t &mqtt_client_cfg, BrokerConfiguratio
|
||||
std::visit(overloaded{
|
||||
[&mqtt_client_cfg](Host const & host)
|
||||
{
|
||||
mqtt_client_cfg.host = host.address.c_str();
|
||||
mqtt_client_cfg.path = host.path.c_str();
|
||||
mqtt_client_cfg.transport = host.transport;
|
||||
mqtt_client_cfg.broker.address.hostname = host.address.c_str();
|
||||
mqtt_client_cfg.broker.address.path = host.path.c_str();
|
||||
mqtt_client_cfg.broker.address.transport = host.transport;
|
||||
},
|
||||
[&mqtt_client_cfg](URI const & uri)
|
||||
{
|
||||
mqtt_client_cfg.uri = uri.address.c_str();
|
||||
mqtt_client_cfg.broker.address.uri = uri.address.c_str();
|
||||
},
|
||||
[]([[maybe_unused ]]auto & unknown)
|
||||
{
|
||||
@ -55,18 +55,18 @@ void config_broker(esp_mqtt_client_config_t &mqtt_client_cfg, BrokerConfiguratio
|
||||
[]([[maybe_unused]]Insecure const & insecure) {},
|
||||
[&mqtt_client_cfg](GlobalCAStore const & use_global_store)
|
||||
{
|
||||
mqtt_client_cfg.use_global_ca_store = true;
|
||||
mqtt_client_cfg.broker.verification.use_global_ca_store = true;
|
||||
},
|
||||
[&mqtt_client_cfg](CryptographicInformation const & certificates)
|
||||
{
|
||||
std::visit(overloaded{
|
||||
[&mqtt_client_cfg](PEM const & pem)
|
||||
{
|
||||
mqtt_client_cfg.cert_pem = pem.data;
|
||||
mqtt_client_cfg.broker.verification.certificate= pem.data;
|
||||
}, [&mqtt_client_cfg](DER const & der)
|
||||
{
|
||||
mqtt_client_cfg.cert_pem = der.data;
|
||||
mqtt_client_cfg.cert_len = der.len;
|
||||
mqtt_client_cfg.broker.verification.certificate = der.data;
|
||||
mqtt_client_cfg.broker.verification.certificate_len = der.len;
|
||||
}}, certificates);
|
||||
},
|
||||
[]([[maybe_unused]] PSK const & psk) {},
|
||||
@ -76,7 +76,7 @@ void config_broker(esp_mqtt_client_config_t &mqtt_client_cfg, BrokerConfiguratio
|
||||
}
|
||||
},
|
||||
broker.security);
|
||||
mqtt_client_cfg.port = broker.address.port;
|
||||
mqtt_client_cfg.broker.address.port = broker.address.port;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -85,12 +85,12 @@ void config_broker(esp_mqtt_client_config_t &mqtt_client_cfg, BrokerConfiguratio
|
||||
*/
|
||||
void config_client_credentials(esp_mqtt_client_config_t &mqtt_client_cfg, ClientCredentials const &credentials)
|
||||
{
|
||||
mqtt_client_cfg.client_id = credentials.client_id.has_value() ? credentials.client_id.value().c_str() : nullptr ;
|
||||
mqtt_client_cfg.username = credentials.username.has_value() ? credentials.username.value().c_str() : nullptr ;
|
||||
mqtt_client_cfg.credentials.client_id = credentials.client_id.has_value() ? credentials.client_id.value().c_str() : nullptr ;
|
||||
mqtt_client_cfg.credentials.username = credentials.username.has_value() ? credentials.username.value().c_str() : nullptr ;
|
||||
std::visit(overloaded{
|
||||
[&mqtt_client_cfg](Password const & password)
|
||||
{
|
||||
mqtt_client_cfg.password = password.data.c_str();
|
||||
mqtt_client_cfg.credentials.authentication.password = password.data.c_str();
|
||||
},
|
||||
[](ClientCertificate const & certificate) {},
|
||||
[](SecureElement const & enable_secure_element) {},
|
||||
|
@ -71,7 +71,7 @@ void mqtt_app_publish(char* topic, char *publish_string)
|
||||
void mqtt_app_start(void)
|
||||
{
|
||||
esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = "mqtt://mqtt.eclipseprojects.io",
|
||||
.broker.address.uri = "mqtt://mqtt.eclipseprojects.io",
|
||||
};
|
||||
|
||||
s_client = esp_mqtt_client_init(&mqtt_cfg);
|
||||
|
@ -44,7 +44,7 @@ static void send_binary(esp_mqtt_client_handle_t client)
|
||||
const esp_partition_t *partition = esp_ota_get_running_partition();
|
||||
esp_partition_mmap(partition, 0, partition->size, SPI_FLASH_MMAP_DATA, &binary_address, &out_handle);
|
||||
// sending only the configured portion of the partition (if it's less than the partition size)
|
||||
int binary_size = MIN(CONFIG_BROKER_BIN_SIZE_TO_SEND,partition->size);
|
||||
int binary_size = MIN(CONFIG_BROKER_BIN_SIZE_TO_SEND, partition->size);
|
||||
int msg_id = esp_mqtt_client_publish(client, "/topic/binary", binary_address, binary_size, 0, 0);
|
||||
ESP_LOGI(TAG, "binary sent with msg_id=%d", msg_id);
|
||||
}
|
||||
@ -123,8 +123,10 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
|
||||
static void mqtt_app_start(void)
|
||||
{
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = CONFIG_BROKER_URI,
|
||||
.cert_pem = (const char *)mqtt_eclipseprojects_io_pem_start,
|
||||
.broker = {
|
||||
.address.uri = CONFIG_BROKER_URI,
|
||||
.verification.certificate = (const char *)mqtt_eclipseprojects_io_pem_start
|
||||
},
|
||||
};
|
||||
|
||||
ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
|
||||
|
@ -51,12 +51,24 @@ extern const uint8_t client_cert_pem_end[] asm("_binary_client_crt_end");
|
||||
extern const uint8_t server_cert_pem_start[] asm("_binary_mosquitto_org_crt_start");
|
||||
extern const uint8_t server_cert_pem_end[] asm("_binary_mosquitto_org_crt_end");
|
||||
|
||||
static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
|
||||
/*
|
||||
* @brief Event handler registered to receive MQTT events
|
||||
*
|
||||
* This function is called by the MQTT client event loop.
|
||||
*
|
||||
* @param handler_args user data registered to the event.
|
||||
* @param base Event base for the handler(always MQTT Base in this example).
|
||||
* @param event_id The id for the received event.
|
||||
* @param event_data The data for the event, esp_mqtt_event_handle_t.
|
||||
*/
|
||||
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
|
||||
{
|
||||
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
|
||||
esp_mqtt_event_handle_t event = event_data;
|
||||
esp_mqtt_client_handle_t client = event->client;
|
||||
int msg_id;
|
||||
// your_context_t *context = event->context;
|
||||
switch (event->event_id) {
|
||||
switch ((esp_mqtt_event_id_t)event_id) {
|
||||
case MQTT_EVENT_CONNECTED:
|
||||
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
|
||||
msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0);
|
||||
@ -95,7 +107,6 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
|
||||
ESP_LOGI(TAG, "Other event id:%d", event->event_id);
|
||||
break;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void *esp_read_ds_data_from_nvs(void)
|
||||
@ -173,16 +184,22 @@ static void mqtt_app_start(void)
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = "mqtts://test.mosquitto.org:8884",
|
||||
.event_handle = mqtt_event_handler,
|
||||
.cert_pem = (const char *)server_cert_pem_start,
|
||||
.client_cert_pem = (const char *)client_cert_pem_start,
|
||||
.client_key_pem = NULL,
|
||||
.ds_data = ds_data,
|
||||
.broker = {
|
||||
.address.uri = "mqtts://test.mosquitto.org:8884",
|
||||
.verification.certificate = (const char *)server_cert_pem_start,
|
||||
},
|
||||
.credentials = {
|
||||
.authentication = {
|
||||
.certificate = (const char *)client_cert_pem_start,
|
||||
.key = NULL,
|
||||
.ds_data = ds_data
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
|
||||
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
|
||||
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
|
||||
esp_mqtt_client_start(client);
|
||||
}
|
||||
|
||||
|
@ -111,12 +111,16 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
|
||||
|
||||
static void mqtt_app_start(void)
|
||||
{
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = "mqtts://test.mosquitto.org:8884",
|
||||
.client_cert_pem = (const char *)client_cert_pem_start,
|
||||
.client_key_pem = (const char *)client_key_pem_start,
|
||||
.cert_pem = (const char *)server_cert_pem_start,
|
||||
};
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.broker.address.uri = "mqtts://test.mosquitto.org:8884",
|
||||
.broker.verification.certificate = (const char *)server_cert_pem_start,
|
||||
.credentials = {
|
||||
.authentication = {
|
||||
.certificate = (const char *)client_cert_pem_start,
|
||||
.key = (const char *)client_key_pem_start,
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
|
||||
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
|
||||
|
@ -52,12 +52,23 @@ static const psk_hint_key_t psk_hint_key = {
|
||||
.hint = "hint"
|
||||
};
|
||||
|
||||
static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
|
||||
/*
|
||||
* @brief Event handler registered to receive MQTT events
|
||||
*
|
||||
* This function is called by the MQTT client event loop.
|
||||
*
|
||||
* @param handler_args user data registered to the event.
|
||||
* @param base Event base for the handler(always MQTT Base in this example).
|
||||
* @param event_id The id for the received event.
|
||||
* @param event_data The data for the event, esp_mqtt_event_handle_t.
|
||||
*/
|
||||
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
|
||||
{
|
||||
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
|
||||
esp_mqtt_event_handle_t event = event_data;
|
||||
esp_mqtt_client_handle_t client = event->client;
|
||||
int msg_id;
|
||||
// your_context_t *context = event->context;
|
||||
switch (event->event_id) {
|
||||
switch ((esp_mqtt_event_id_t)event_id) {
|
||||
case MQTT_EVENT_CONNECTED:
|
||||
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
|
||||
msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0);
|
||||
@ -96,20 +107,20 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
|
||||
ESP_LOGI(TAG, "Other event id:%d", event->event_id);
|
||||
break;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
static void mqtt_app_start(void)
|
||||
{
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = EXAMPLE_BROKER_URI,
|
||||
.event_handle = mqtt_event_handler,
|
||||
.psk_hint_key = &psk_hint_key,
|
||||
.broker.address.uri = EXAMPLE_BROKER_URI,
|
||||
.broker.verification.psk_hint_key = &psk_hint_key,
|
||||
};
|
||||
|
||||
ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
|
||||
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
|
||||
/* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */
|
||||
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
|
||||
esp_mqtt_client_start(client);
|
||||
}
|
||||
|
||||
|
@ -110,12 +110,12 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
|
||||
static void mqtt_app_start(void)
|
||||
{
|
||||
esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = CONFIG_BROKER_URL,
|
||||
.broker.address.uri = CONFIG_BROKER_URL,
|
||||
};
|
||||
#if CONFIG_BROKER_URL_FROM_STDIN
|
||||
char line[128];
|
||||
|
||||
if (strcmp(mqtt_cfg.uri, "FROM_STDIN") == 0) {
|
||||
if (strcmp(mqtt_cfg.broker.address.uri, "FROM_STDIN") == 0) {
|
||||
int count = 0;
|
||||
printf("Please enter url of mqtt broker\n");
|
||||
while (count < 128) {
|
||||
@ -129,7 +129,7 @@ static void mqtt_app_start(void)
|
||||
}
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
}
|
||||
mqtt_cfg.uri = line;
|
||||
mqtt_cfg.broker.address.uri = line;
|
||||
printf("Broker url: %s\n", line);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Configuration mismatch: wrong broker url");
|
||||
|
@ -107,7 +107,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
|
||||
static void mqtt_app_start(void)
|
||||
{
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = CONFIG_BROKER_URI,
|
||||
.broker.address.uri = CONFIG_BROKER_URI,
|
||||
};
|
||||
|
||||
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
|
||||
|
@ -96,8 +96,8 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
|
||||
static void mqtt_app_start(void)
|
||||
{
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = CONFIG_BROKER_URI,
|
||||
.cert_pem = (const char *)mqtt_eclipseprojects_io_pem_start,
|
||||
.broker.address.uri = CONFIG_BROKER_URI,
|
||||
.broker.verification.certificate = (const char *)mqtt_eclipseprojects_io_pem_start,
|
||||
};
|
||||
|
||||
ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
|
||||
|
@ -355,7 +355,7 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data):
|
||||
return None, None
|
||||
return value.group(1), int(value.group(2))
|
||||
|
||||
publish_cfg['publish_topic'] = dut1.app.get_sdkconfig()['CONFIG_EXAMPLE_SUBSCIBE_TOPIC'].replace('"','')
|
||||
publish_cfg['publish_topic'] = dut1.app.get_sdkconfig()['CONFIG_EXAMPLE_SUBSCRIBE_TOPIC'].replace('"','')
|
||||
publish_cfg['subscribe_topic'] = dut1.app.get_sdkconfig()['CONFIG_EXAMPLE_PUBLISH_TOPIC'].replace('"','')
|
||||
publish_cfg['broker_host_ssl'], publish_cfg['broker_port_ssl'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_SSL_URI')
|
||||
publish_cfg['broker_host_tcp'], publish_cfg['broker_port_tcp'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_TCP_URI')
|
||||
|
@ -30,7 +30,7 @@ menu "Example Configuration"
|
||||
help
|
||||
topic to which esp32 client publishes
|
||||
|
||||
config EXAMPLE_SUBSCIBE_TOPIC
|
||||
config EXAMPLE_SUBSCRIBE_TOPIC
|
||||
string "subscribe topic"
|
||||
default "/topic/subscribe/py2esp"
|
||||
help
|
||||
|
@ -64,7 +64,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
|
||||
static void create_client(void)
|
||||
{
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = "mqtts://127.0.0.1:1234"
|
||||
.broker.address.uri = "mqtts://127.0.0.1:1234"
|
||||
};
|
||||
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
|
||||
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
|
||||
@ -88,7 +88,7 @@ static void connect_no_certs(const char *host, const int port)
|
||||
char uri[64];
|
||||
sprintf(uri, "mqtts://%s:%d", host, port);
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = uri
|
||||
.broker.address.uri = uri
|
||||
};
|
||||
esp_mqtt_set_config(mqtt_client, &mqtt_cfg);
|
||||
esp_mqtt_client_disconnect(mqtt_client);
|
||||
@ -100,12 +100,12 @@ static void connect_with_client_key_password(const char *host, const int port)
|
||||
char uri[64];
|
||||
sprintf(uri, "mqtts://%s:%d", host, port);
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = uri,
|
||||
.cert_pem = (const char *)ca_local_crt,
|
||||
.client_cert_pem = (const char *)client_pwd_crt,
|
||||
.client_key_pem = (const char *)client_pwd_key,
|
||||
.clientkey_password = "esp32",
|
||||
.clientkey_password_len = 5
|
||||
.broker.address.uri = uri,
|
||||
.broker.verification.certificate = (const char *)ca_local_crt,
|
||||
.credentials.authentication.certificate = (const char *)client_pwd_crt,
|
||||
.credentials.authentication.key = (const char *)client_pwd_key,
|
||||
.credentials.authentication.key_password = "esp32",
|
||||
.credentials.authentication.key_password_len = 5
|
||||
};
|
||||
esp_mqtt_set_config(mqtt_client, &mqtt_cfg);
|
||||
esp_mqtt_client_disconnect(mqtt_client);
|
||||
@ -117,11 +117,11 @@ static void connect_with_server_der_cert(const char *host, const int port)
|
||||
char uri[64];
|
||||
sprintf(uri, "mqtts://%s:%d", host, port);
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = uri,
|
||||
.cert_pem = (const char *)ca_der_start,
|
||||
.cert_len = ca_der_end - ca_der_start,
|
||||
.client_cert_pem = "NULL",
|
||||
.client_key_pem = "NULL"
|
||||
.broker.address.uri = uri,
|
||||
.broker.verification.certificate = (const char *)ca_der_start,
|
||||
.broker.verification.certificate_len = ca_der_end - ca_der_start,
|
||||
.credentials.authentication.certificate = "NULL",
|
||||
.credentials.authentication.key = "NULL"
|
||||
};
|
||||
esp_mqtt_set_config(mqtt_client, &mqtt_cfg);
|
||||
esp_mqtt_client_disconnect(mqtt_client);
|
||||
@ -133,10 +133,10 @@ static void connect_with_wrong_server_cert(const char *host, const int port)
|
||||
char uri[64];
|
||||
sprintf(uri, "mqtts://%s:%d", host, port);
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = uri,
|
||||
.cert_pem = (const char *)client_pwd_crt,
|
||||
.client_cert_pem = "NULL",
|
||||
.client_key_pem = "NULL"
|
||||
.broker.address.uri = uri,
|
||||
.broker.verification.certificate = (const char *)client_pwd_crt,
|
||||
.credentials.authentication.certificate = "NULL",
|
||||
.credentials.authentication.key = "NULL"
|
||||
};
|
||||
esp_mqtt_set_config(mqtt_client, &mqtt_cfg);
|
||||
esp_mqtt_client_disconnect(mqtt_client);
|
||||
@ -148,8 +148,8 @@ static void connect_with_server_cert(const char *host, const int port)
|
||||
char uri[64];
|
||||
sprintf(uri, "mqtts://%s:%d", host, port);
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = uri,
|
||||
.cert_pem = (const char *)ca_local_crt,
|
||||
.broker.address.uri = uri,
|
||||
.broker.verification.certificate = (const char *)ca_local_crt,
|
||||
};
|
||||
esp_mqtt_set_config(mqtt_client, &mqtt_cfg);
|
||||
esp_mqtt_client_disconnect(mqtt_client);
|
||||
@ -161,10 +161,10 @@ static void connect_with_server_client_certs(const char *host, const int port)
|
||||
char uri[64];
|
||||
sprintf(uri, "mqtts://%s:%d", host, port);
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = uri,
|
||||
.cert_pem = (const char *)ca_local_crt,
|
||||
.client_cert_pem = (const char *)client_pwd_crt,
|
||||
.client_key_pem = (const char *)client_no_pwd_key
|
||||
.broker.address.uri = uri,
|
||||
.broker.verification.certificate = (const char *)ca_local_crt,
|
||||
.credentials.authentication.certificate = (const char *)client_pwd_crt,
|
||||
.credentials.authentication.key = (const char *)client_no_pwd_key
|
||||
};
|
||||
esp_mqtt_set_config(mqtt_client, &mqtt_cfg);
|
||||
esp_mqtt_client_disconnect(mqtt_client);
|
||||
@ -176,10 +176,10 @@ static void connect_with_invalid_client_certs(const char *host, const int port)
|
||||
char uri[64];
|
||||
sprintf(uri, "mqtts://%s:%d", host, port);
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = uri,
|
||||
.cert_pem = (const char *)ca_local_crt,
|
||||
.client_cert_pem = (const char *)client_inv_crt,
|
||||
.client_key_pem = (const char *)client_no_pwd_key
|
||||
.broker.address.uri = uri,
|
||||
.broker.verification.certificate = (const char *)ca_local_crt,
|
||||
.credentials.authentication.certificate = (const char *)client_inv_crt,
|
||||
.credentials.authentication.key = (const char *)client_no_pwd_key
|
||||
};
|
||||
esp_mqtt_set_config(mqtt_client, &mqtt_cfg);
|
||||
esp_mqtt_client_disconnect(mqtt_client);
|
||||
@ -192,8 +192,8 @@ static void connect_with_alpn(const char *host, const int port)
|
||||
const char *alpn_protos[] = { "mymqtt", NULL };
|
||||
sprintf(uri, "mqtts://%s:%d", host, port);
|
||||
const esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = uri,
|
||||
.alpn_protos = alpn_protos
|
||||
.broker.address.uri = uri,
|
||||
.broker.verification.alpn_protos = alpn_protos
|
||||
};
|
||||
esp_mqtt_set_config(mqtt_client, &mqtt_cfg);
|
||||
esp_mqtt_client_disconnect(mqtt_client);
|
||||
|
@ -52,8 +52,8 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
|
||||
case MQTT_EVENT_CONNECTED:
|
||||
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
|
||||
xEventGroupSetBits(mqtt_event_group, CONNECTED_BIT);
|
||||
msg_id = esp_mqtt_client_subscribe(client, CONFIG_EXAMPLE_SUBSCIBE_TOPIC, qos_test);
|
||||
ESP_LOGI(TAG, "sent subscribe successful %s , msg_id=%d", CONFIG_EXAMPLE_SUBSCIBE_TOPIC, msg_id);
|
||||
msg_id = esp_mqtt_client_subscribe(client, CONFIG_EXAMPLE_SUBSCRIBE_TOPIC, qos_test);
|
||||
ESP_LOGI(TAG, "sent subscribe successful %s , msg_id=%d", CONFIG_EXAMPLE_SUBSCRIBE_TOPIC, msg_id);
|
||||
|
||||
break;
|
||||
case MQTT_EVENT_DISCONNECTED:
|
||||
@ -164,24 +164,24 @@ static void configure_client(char *transport)
|
||||
break;
|
||||
case TCP:
|
||||
ESP_LOGI(TAG, "[TCP transport] Startup..");
|
||||
config.uri = CONFIG_EXAMPLE_BROKER_TCP_URI;
|
||||
config.broker.address.uri = CONFIG_EXAMPLE_BROKER_TCP_URI;
|
||||
break;
|
||||
case SSL:
|
||||
ESP_LOGI(TAG, "[SSL transport] Startup..");
|
||||
config.uri = CONFIG_EXAMPLE_BROKER_SSL_URI;
|
||||
config.broker.address.uri = CONFIG_EXAMPLE_BROKER_SSL_URI;
|
||||
break;
|
||||
case WS:
|
||||
ESP_LOGI(TAG, "[WS transport] Startup..");
|
||||
config.uri = CONFIG_EXAMPLE_BROKER_WS_URI;
|
||||
config.broker.address.uri = CONFIG_EXAMPLE_BROKER_WS_URI;
|
||||
break;
|
||||
case WSS:
|
||||
ESP_LOGI(TAG, "[WSS transport] Startup..");
|
||||
config.uri = CONFIG_EXAMPLE_BROKER_WSS_URI;
|
||||
config.broker.address.uri = CONFIG_EXAMPLE_BROKER_WSS_URI;
|
||||
break;
|
||||
}
|
||||
if (selected_transport == SSL || selected_transport == WSS) {
|
||||
ESP_LOGI(TAG, "Set certificate");
|
||||
config.cert_pem = (const char *)mqtt_eclipseprojects_io_pem_start;
|
||||
config.broker.verification.certificate = (const char *)mqtt_eclipseprojects_io_pem_start;
|
||||
}
|
||||
esp_mqtt_set_config(mqtt_client, &config);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user