mirror of
https://github.com/espressif/esp-mqtt.git
synced 2025-07-30 10:48:06 +02:00
remove license header and add doc
This commit is contained in:
96
README.md
96
README.md
@ -2,8 +2,104 @@
|
|||||||
|
|
||||||
## How to use
|
## 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
|
## 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
|
## 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
|
## License
|
||||||
|
|
||||||
|
Apache License
|
||||||
|
@ -51,12 +51,11 @@ typedef struct {
|
|||||||
char password[MQTT_MAX_PASSWORD_LEN];
|
char password[MQTT_MAX_PASSWORD_LEN];
|
||||||
char lwt_topic[MQTT_MAX_LWT_TOPIC];
|
char lwt_topic[MQTT_MAX_LWT_TOPIC];
|
||||||
char lwt_msg[MQTT_MAX_LWT_MSG];
|
char lwt_msg[MQTT_MAX_LWT_MSG];
|
||||||
int lwt_msg_len;
|
|
||||||
int lwt_qos;
|
int lwt_qos;
|
||||||
int lwt_retain;
|
int lwt_retain;
|
||||||
int clean_session;
|
int disable_clean_session;
|
||||||
int keepalive;
|
int keepalive;
|
||||||
bool auto_reconnect;
|
bool disable_auto_reconnect;
|
||||||
void *user_context;
|
void *user_context;
|
||||||
int task_prio;
|
int task_prio;
|
||||||
int task_stack;
|
int task_stack;
|
||||||
|
@ -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
|
|
@ -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__
|
#ifndef _PLATFORM_H__
|
||||||
#define _PLATFORM_H__
|
#define _PLATFORM_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__
|
#ifndef _ESP_PLATFORM_H__
|
||||||
#define _ESP_PLATFORM_H__
|
#define _ESP_PLATFORM_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_
|
#ifndef _TRANSPORT_H_
|
||||||
#define _TRANSPORT_H_
|
#define _TRANSPORT_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_
|
#ifndef _TRANSPORT_SSL_H_
|
||||||
#define _TRANSPORT_SSL_H_
|
#define _TRANSPORT_SSL_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_
|
#ifndef _TRANSPORT_TCP_H_
|
||||||
#define _TRANSPORT_TCP_H_
|
#define _TRANSPORT_TCP_H_
|
||||||
|
|
||||||
|
@ -4,17 +4,6 @@
|
|||||||
#include "rom/queue.h"
|
#include "rom/queue.h"
|
||||||
#include "esp_log.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";
|
static const char *TAG = "OUTBOX";
|
||||||
|
|
||||||
outbox_handle_t outbox_init()
|
outbox_handle_t outbox_init()
|
||||||
|
@ -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_qos = config->lwt_qos;
|
||||||
client->connect_info.will_retain = config->lwt_retain;
|
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;
|
client->connect_info.keepalive = config->keepalive;
|
||||||
if (client->connect_info.keepalive == 0) {
|
if (client->connect_info.keepalive == 0) {
|
||||||
client->connect_info.keepalive = MQTT_KEEPALIVE_TICK;
|
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->user_context = config->user_context;
|
||||||
cfg->event_handle = config->event_handle;
|
cfg->event_handle = config->event_handle;
|
||||||
cfg->auto_reconnect = true;
|
cfg->auto_reconnect = true;
|
||||||
|
if (cfg->disable_auto_reconnect) {
|
||||||
|
cfg->auto_reconnect = false;
|
||||||
|
}
|
||||||
|
|
||||||
client->config = cfg;
|
client->config = cfg;
|
||||||
return ESP_OK;
|
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_length = length;
|
||||||
mqtt_data = mqtt_get_publish_data(message, &mqtt_data_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;
|
mqtt_data_offset += mqtt_data_length;
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Get data len= %d, topic len=%d", mqtt_data_length, mqtt_topic_length);
|
ESP_LOGI(TAG, "Get data len= %d, topic len=%d", mqtt_data_length, mqtt_topic_length);
|
||||||
|
Reference in New Issue
Block a user