diff --git a/README.md b/README.md index a9126ea..b8e0c27 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,104 @@ ## How to use +Clone this component to [ESP-IDF](https://github.com/espressif/esp-idf) project (as submodule): +``` +git submodule add https://github.com/tuanpmt/espmqtt.git components/espmqtt +``` + ## Documentation +### URI + +- MQTT over HTTP, default port `1883`: `mqtt://iot.eclipse.org` +- MQTT over HTTP, port `1884`: `mqtt://iot.eclipse.org:1884` +- MQTT over HTTP, port `1884`, username and password: `mqtt://username:password@iot.eclipse.org:1884` +- MQTT over HTTPS, default port `8883`: `mqtts://iot.eclipse.org` +- Minimal configurations: + +```cpp +const esp_mqtt_client_config_t mqtt_cfg = { + .uri = "mqtt://iot.eclipse.org", + .event_handle = mqtt_event_handler, + // .user_context = (void *)your_context +}; +``` + +### More options for `esp_mqtt_client_config_t` + +- `event_handle` for MQTT events +- `host`: replace `uri` host +- `port`: replace `uri` port +- `client_id`: replace default client id is `ESP32_%CHIPID%` +- `lwt_topic, lwt_msg, lwt_qos, lwt_retain`: are mqtt lwt options, default NULL +- `disable_clean_session`: mqtt clean session, default clean_session is true +- `keepalive`: (value in seconds) mqtt keepalive, default is 120 seconds +- `disable_auto_reconnect`: this mqtt client will reconnect to server (when errors/disconnect). Set `disable_auto_reconnect=true` to disable +- `user_context` pass user context to this option, then can receive that context in `event->user_context` +- `task_prio, task_stack` for MQTT task, default priority is 5, and task_stack = 4096 bytes +- `buffer_size` for MQTT send/receive buffer, default is 1024 +- `cert_pem` pointer to CERT file for server verify (with SSL), default is NULL, not required to verify the server + ## Example +Check `examples/mqtt_tcp` and `examples/mqtt_ssl` project. In Short: + +```cpp + +static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) +{ + esp_mqtt_client_handle_t client = event->client; + int msg_id; + // your_context_t *context = event->context; + switch (event->event_id) { + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); + ESP_LOGI(TAG, "sent unsubscribe 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/qos0", "data", 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"); + vTaskDelay(500/portTICK_RATE_MS); + msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + break; + } + return ESP_OK; +} +const esp_mqtt_client_config_t mqtt_cfg = { + .uri = "mqtt://iot.eclipse.org", + .event_handle = mqtt_event_handler, + // .user_context = (void *)your_context +}; + +esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); +esp_mqtt_client_start(client); +``` + ## License + +Apache License diff --git a/include/mqtt_client.h b/include/mqtt_client.h index 6572958..d784f90 100755 --- a/include/mqtt_client.h +++ b/include/mqtt_client.h @@ -51,12 +51,11 @@ typedef struct { char password[MQTT_MAX_PASSWORD_LEN]; char lwt_topic[MQTT_MAX_LWT_TOPIC]; char lwt_msg[MQTT_MAX_LWT_MSG]; - int lwt_msg_len; int lwt_qos; int lwt_retain; - int clean_session; + int disable_clean_session; int keepalive; - bool auto_reconnect; + bool disable_auto_reconnect; void *user_context; int task_prio; int task_stack; diff --git a/lib/README.md b/lib/README.md deleted file mode 100644 index be5a1bb..0000000 --- a/lib/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# ESP32 MQTT Library - -This is component based on ESP-IDF for ESP32 - -Full documentation and sample project: https://github.com/tuanpmt/esp32-mqtt diff --git a/lib/include/platform.h b/lib/include/platform.h index 7121609..e311503 100644 --- a/lib/include/platform.h +++ b/lib/include/platform.h @@ -1,18 +1,3 @@ - -// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - #ifndef _PLATFORM_H__ #define _PLATFORM_H__ diff --git a/lib/include/platform_esp32_idf.h b/lib/include/platform_esp32_idf.h index 0368544..ea7bcda 100644 --- a/lib/include/platform_esp32_idf.h +++ b/lib/include/platform_esp32_idf.h @@ -1,18 +1,4 @@ -// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - #ifndef _ESP_PLATFORM_H__ #define _ESP_PLATFORM_H__ diff --git a/lib/include/transport.h b/lib/include/transport.h index 7bbe07b..c1b5395 100644 --- a/lib/include/transport.h +++ b/lib/include/transport.h @@ -1,17 +1,3 @@ -// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - #ifndef _TRANSPORT_H_ #define _TRANSPORT_H_ diff --git a/lib/include/transport_ssl.h b/lib/include/transport_ssl.h index 91cce7e..2f6fd9f 100644 --- a/lib/include/transport_ssl.h +++ b/lib/include/transport_ssl.h @@ -1,16 +1,3 @@ -// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. #ifndef _TRANSPORT_SSL_H_ #define _TRANSPORT_SSL_H_ diff --git a/lib/include/transport_tcp.h b/lib/include/transport_tcp.h index c3d555d..dc8a86f 100644 --- a/lib/include/transport_tcp.h +++ b/lib/include/transport_tcp.h @@ -1,17 +1,3 @@ -// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - #ifndef _TRANSPORT_TCP_H_ #define _TRANSPORT_TCP_H_ diff --git a/lib/mqtt_outbox.c b/lib/mqtt_outbox.c index c337579..d56e533 100644 --- a/lib/mqtt_outbox.c +++ b/lib/mqtt_outbox.c @@ -4,17 +4,6 @@ #include "rom/queue.h" #include "esp_log.h" -// typedef struct outbox_item { -// char *buffer; -// int len; -// int msg_id; -// int msg_type; -// int tick_created; -// int retry_count; -// bool pending; -// STAILQ_ENTRY(outbox) next; -// } outbox_item; - static const char *TAG = "OUTBOX"; outbox_handle_t outbox_init() diff --git a/mqtt_client.c b/mqtt_client.c index e68c1b9..e7a484d 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -128,8 +128,11 @@ static esp_err_t esp_mqtt_set_config(esp_mqtt_client_handle_t client, const esp_ client->connect_info.will_qos = config->lwt_qos; client->connect_info.will_retain = config->lwt_retain; - client->connect_info.clean_session = config->clean_session; //TODO: default is CLEAN + client->connect_info.clean_session = true//TODO: default is CLEAN + if (config->disable_clean_session) { + client->connect_info.clean_session = false; + } client->connect_info.keepalive = config->keepalive; if (client->connect_info.keepalive == 0) { client->connect_info.keepalive = MQTT_KEEPALIVE_TICK; @@ -138,6 +141,10 @@ static esp_err_t esp_mqtt_set_config(esp_mqtt_client_handle_t client, const esp_ cfg->user_context = config->user_context; cfg->event_handle = config->event_handle; cfg->auto_reconnect = true; + if (cfg->disable_auto_reconnect) { + cfg->auto_reconnect = false; + } + client->config = cfg; return ESP_OK; } @@ -404,9 +411,6 @@ static void deliver_publish(esp_mqtt_client_handle_t client, uint8_t *message, i mqtt_data_length = length; mqtt_data = mqtt_get_publish_data(message, &mqtt_data_length); - if (client->config->event_handle) { - // client->config->event_handle(client, &event_data); - } mqtt_data_offset += mqtt_data_length; ESP_LOGI(TAG, "Get data len= %d, topic len=%d", mqtt_data_length, mqtt_topic_length);