mirror of
https://github.com/espressif/ESP8266_RTOS_SDK.git
synced 2025-05-21 17:16:29 +08:00
feat(mqtt): remove ibm-mqtt component and example
This commit is contained in:
@ -1,3 +1,2 @@
|
||||
CONFIG_BROKER_URI="mqtts://${EXAMPLE_MQTT_BROKER_SSL}"
|
||||
CONFIG_BROKER_CERTIFICATE_OVERRIDE="${EXAMPLE_MQTT_BROKER_CERTIFICATE}"
|
||||
CONFIG_MQTT_USING_ESP=y
|
||||
|
@ -1 +0,0 @@
|
||||
CONFIG_MQTT_USING_ESP=y
|
@ -1 +0,0 @@
|
||||
CONFIG_MQTT_USING_ESP=y
|
@ -1,3 +1,2 @@
|
||||
CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y
|
||||
CONFIG_BROKER_URL="FROM_STDIN"
|
||||
CONFIG_MQTT_USING_ESP=y
|
||||
|
@ -1 +0,0 @@
|
||||
CONFIG_MQTT_USING_ESP=y
|
@ -1,2 +1 @@
|
||||
CONFIG_BROKER_URI="ws://${EXAMPLE_MQTT_BROKER_WS}/ws"
|
||||
CONFIG_MQTT_USING_ESP=y
|
||||
|
@ -1 +0,0 @@
|
||||
CONFIG_MQTT_USING_ESP=y
|
@ -1,3 +1,2 @@
|
||||
CONFIG_BROKER_URI="wss://${EXAMPLE_MQTT_BROKER_WSS}/ws"
|
||||
CONFIG_BROKER_CERTIFICATE_OVERRIDE="${EXAMPLE_MQTT_BROKER_CERTIFICATE}"
|
||||
CONFIG_MQTT_USING_ESP=y
|
||||
|
@ -1 +0,0 @@
|
||||
CONFIG_MQTT_USING_ESP=y
|
@ -1,6 +0,0 @@
|
||||
# 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)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(ibm-mqtt)
|
@ -1,9 +0,0 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := mqtt_demo
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
@ -1,21 +0,0 @@
|
||||
# ESP8266 MQTT Client Demo
|
||||
|
||||
## 1. Introduction
|
||||
|
||||
This MQTT demo is based on the Eclipse Paho MQTT library, and demonstrates a working MQTT client actions(subscribe, publish, ping). Using this MQTT demo, you can connect to the MQTT broker, subscribe to a topic and publish messages to the predefined topic.
|
||||
|
||||
Also, this demo will ping the MQTT broker in the defined interval if no sending or receiving action happens. And we add some APIs to realize the SSL functionality, these SSL APIs provide the one-way certification and two-way certification.
|
||||
|
||||
## 2. Configuration
|
||||
* export `IDF_PATH`
|
||||
* `make menuconfig` -> `Example Configuration` to config your example
|
||||
* `make menuconfig` -> `Component config` -> `MQTT(Paho)` -> to config your MQTT parameters
|
||||
|
||||
## 3. Compiling & Execution
|
||||
|
||||
Once all the aforementioned works are done, the MQTT client demo will:
|
||||
|
||||
* Connect to the MQTT Broker
|
||||
* Subscribe to the topic "/espressif/sub"
|
||||
* Publish messages to the topic "/espressif/pub"
|
||||
* MQTT keep alive interval is 30s, so if no sending and receiving actions happended during this interval, ping request will be sent and ping response is expected to be received.
|
@ -1,4 +0,0 @@
|
||||
set(COMPONENT_SRCS "MQTTEcho.c")
|
||||
set(COMPONENT_ADD_INCLUDEDIRS ".")
|
||||
|
||||
register_component()
|
@ -1,93 +0,0 @@
|
||||
menu "Example Configuration"
|
||||
|
||||
config WIFI_SSID
|
||||
string "WiFi SSID"
|
||||
default "myssid"
|
||||
help
|
||||
SSID (network name) for the example to connect to.
|
||||
|
||||
config WIFI_PASSWORD
|
||||
string "WiFi Password"
|
||||
default "mypassword"
|
||||
help
|
||||
WiFi password (WPA or WPA2) for the example to use.
|
||||
Can be left blank if the network has no security set.
|
||||
|
||||
config MQTT_BROKER
|
||||
string "MQTT broker"
|
||||
default "mosquitto.org"
|
||||
help
|
||||
MQTT broker which you want to login, either IP address or domain name is OK.
|
||||
|
||||
config MQTT_PORT
|
||||
int "Default MQTT port"
|
||||
default 1883
|
||||
help
|
||||
MQTT port.
|
||||
|
||||
config MQTT_SUB_TOPIC
|
||||
string "MQTT subscribe topic"
|
||||
default "/espressif/sub"
|
||||
help
|
||||
MQTT subscribe topic to MQTT broker.
|
||||
|
||||
choice MQTT_SUB_QOS
|
||||
prompt "MQTT Subscribe QoS"
|
||||
default SUB_QOS1
|
||||
help
|
||||
MQTT subcribe QoS level.
|
||||
|
||||
config SUB_QOS0
|
||||
bool "QOS0"
|
||||
config SUB_QOS1
|
||||
bool "QOS1"
|
||||
config SUB_QOS2
|
||||
bool "QOS2"
|
||||
endchoice
|
||||
|
||||
config DEFAULT_MQTT_SUB_QOS
|
||||
int
|
||||
default 0 if SUB_QOS0
|
||||
default 1 if SUB_QOS1
|
||||
default 2 if SUB_QOS2
|
||||
|
||||
config MQTT_PUB_TOPIC
|
||||
string "MQTT publish topic"
|
||||
default "/espressif/pub"
|
||||
help
|
||||
MQTT publish topic to MQTT broker.
|
||||
|
||||
choice MQTT_PUB_QOS
|
||||
prompt "MQTT publish QoS"
|
||||
default PUB_QOS1
|
||||
help
|
||||
MQTT publish QoS level.
|
||||
|
||||
config PUB_QOS0
|
||||
bool "QOS0"
|
||||
config PUB_QOS1
|
||||
bool "QOS1"
|
||||
config PUB_QOS2
|
||||
bool "QOS2"
|
||||
endchoice
|
||||
|
||||
config DEFAULT_MQTT_PUB_QOS
|
||||
int
|
||||
default 0 if PUB_QOS0
|
||||
default 1 if PUB_QOS1
|
||||
default 2 if PUB_QOS2
|
||||
|
||||
config MQTT_PUBLISH_INTERVAL
|
||||
int "MQTT publish interval(ms)"
|
||||
default 0
|
||||
help
|
||||
Default MQTT publish message interval.
|
||||
|
||||
config MQTT_PAYLOAD_BUFFER
|
||||
int "MQTT payload size(Bytes)"
|
||||
default 1460
|
||||
help
|
||||
1460~2048 is recommended.
|
||||
MQTT payload size.
|
||||
|
||||
endmenu
|
@ -1,249 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "esp_system.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event_loop.h"
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
|
||||
#include "MQTTClient.h"
|
||||
|
||||
/* FreeRTOS event group to signal when we are connected & ready to make a request */
|
||||
static EventGroupHandle_t wifi_event_group;
|
||||
|
||||
/* The event group allows multiple bits for each event,
|
||||
but we only care about one event - are we connected
|
||||
to the AP with an IP? */
|
||||
const int CONNECTED_BIT = BIT0;
|
||||
|
||||
#define MQTT_CLIENT_THREAD_NAME "mqtt_client_thread"
|
||||
#define MQTT_CLIENT_THREAD_STACK_WORDS 4096
|
||||
#define MQTT_CLIENT_THREAD_PRIO 8
|
||||
|
||||
static const char *TAG = "example";
|
||||
|
||||
static esp_err_t event_handler(void *ctx, system_event_t *event)
|
||||
{
|
||||
/* For accessing reason codes in case of disconnection */
|
||||
system_event_info_t *info = &event->event_info;
|
||||
|
||||
switch (event->event_id) {
|
||||
case SYSTEM_EVENT_STA_START:
|
||||
esp_wifi_connect();
|
||||
break;
|
||||
|
||||
case SYSTEM_EVENT_STA_GOT_IP:
|
||||
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
|
||||
break;
|
||||
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||
ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason);
|
||||
if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) {
|
||||
/*Switch to 802.11 bgn mode */
|
||||
esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N);
|
||||
}
|
||||
esp_wifi_connect();
|
||||
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void initialise_wifi(void)
|
||||
{
|
||||
tcpip_adapter_init();
|
||||
wifi_event_group = xEventGroupCreate();
|
||||
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
|
||||
wifi_config_t wifi_config = {
|
||||
.sta = {
|
||||
.ssid = CONFIG_WIFI_SSID,
|
||||
.password = CONFIG_WIFI_PASSWORD,
|
||||
},
|
||||
};
|
||||
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
}
|
||||
|
||||
static void messageArrived(MessageData *data)
|
||||
{
|
||||
ESP_LOGI(TAG, "Message arrived[len:%u]: %.*s", \
|
||||
data->message->payloadlen, data->message->payloadlen, (char *)data->message->payload);
|
||||
}
|
||||
|
||||
static void mqtt_client_thread(void *pvParameters)
|
||||
{
|
||||
char *payload = NULL;
|
||||
MQTTClient client;
|
||||
Network network;
|
||||
int rc = 0;
|
||||
char clientID[32] = {0};
|
||||
uint32_t count = 0;
|
||||
|
||||
ESP_LOGI(TAG, "ssid:%s passwd:%s sub:%s qos:%u pub:%s qos:%u pubinterval:%u payloadsize:%u",
|
||||
CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD, CONFIG_MQTT_SUB_TOPIC,
|
||||
CONFIG_DEFAULT_MQTT_SUB_QOS, CONFIG_MQTT_PUB_TOPIC, CONFIG_DEFAULT_MQTT_PUB_QOS,
|
||||
CONFIG_MQTT_PUBLISH_INTERVAL, CONFIG_MQTT_PAYLOAD_BUFFER);
|
||||
|
||||
ESP_LOGI(TAG, "ver:%u clientID:%s keepalive:%d username:%s passwd:%s session:%d level:%u",
|
||||
CONFIG_DEFAULT_MQTT_VERSION, CONFIG_MQTT_CLIENT_ID,
|
||||
CONFIG_MQTT_KEEP_ALIVE, CONFIG_MQTT_USERNAME, CONFIG_MQTT_PASSWORD,
|
||||
CONFIG_DEFAULT_MQTT_SESSION, CONFIG_DEFAULT_MQTT_SECURITY);
|
||||
|
||||
ESP_LOGI(TAG, "broker:%s port:%u", CONFIG_MQTT_BROKER, CONFIG_MQTT_PORT);
|
||||
|
||||
ESP_LOGI(TAG, "sendbuf:%u recvbuf:%u sendcycle:%u recvcycle:%u",
|
||||
CONFIG_MQTT_SEND_BUFFER, CONFIG_MQTT_RECV_BUFFER,
|
||||
CONFIG_MQTT_SEND_CYCLE, CONFIG_MQTT_RECV_CYCLE);
|
||||
|
||||
MQTTPacket_connectData connectData = MQTTPacket_connectData_initializer;
|
||||
|
||||
NetworkInit(&network);
|
||||
|
||||
if (MQTTClientInit(&client, &network, 0, NULL, 0, NULL, 0) == false) {
|
||||
ESP_LOGE(TAG, "mqtt init err");
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
payload = malloc(CONFIG_MQTT_PAYLOAD_BUFFER);
|
||||
|
||||
if (!payload) {
|
||||
ESP_LOGE(TAG, "mqtt malloc err");
|
||||
} else {
|
||||
memset(payload, 0x0, CONFIG_MQTT_PAYLOAD_BUFFER);
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
ESP_LOGI(TAG, "wait wifi connect...");
|
||||
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY);
|
||||
|
||||
if ((rc = NetworkConnect(&network, CONFIG_MQTT_BROKER, CONFIG_MQTT_PORT)) != 0) {
|
||||
ESP_LOGE(TAG, "Return code from network connect is %d", rc);
|
||||
continue;
|
||||
}
|
||||
|
||||
connectData.MQTTVersion = CONFIG_DEFAULT_MQTT_VERSION;
|
||||
|
||||
sprintf(clientID, "%s_%u", CONFIG_MQTT_CLIENT_ID, esp_random());
|
||||
|
||||
connectData.clientID.cstring = clientID;
|
||||
connectData.keepAliveInterval = CONFIG_MQTT_KEEP_ALIVE;
|
||||
|
||||
connectData.username.cstring = CONFIG_MQTT_USERNAME;
|
||||
connectData.password.cstring = CONFIG_MQTT_PASSWORD;
|
||||
|
||||
connectData.cleansession = CONFIG_DEFAULT_MQTT_SESSION;
|
||||
|
||||
ESP_LOGI(TAG, "MQTT Connecting");
|
||||
|
||||
if ((rc = MQTTConnect(&client, &connectData)) != 0) {
|
||||
ESP_LOGE(TAG, "Return code from MQTT connect is %d", rc);
|
||||
network.disconnect(&network);
|
||||
continue;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "MQTT Connected");
|
||||
|
||||
#if defined(MQTT_TASK)
|
||||
|
||||
if ((rc = MQTTStartTask(&client)) != pdPASS) {
|
||||
ESP_LOGE(TAG, "Return code from start tasks is %d", rc);
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Use MQTTStartTask");
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
if ((rc = MQTTSubscribe(&client, CONFIG_MQTT_SUB_TOPIC, CONFIG_DEFAULT_MQTT_SUB_QOS, messageArrived)) != 0) {
|
||||
ESP_LOGE(TAG, "Return code from MQTT subscribe is %d", rc);
|
||||
network.disconnect(&network);
|
||||
continue;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "MQTT subscribe to topic %s OK", CONFIG_MQTT_SUB_TOPIC);
|
||||
|
||||
for (;;) {
|
||||
MQTTMessage message;
|
||||
|
||||
message.qos = CONFIG_DEFAULT_MQTT_PUB_QOS;
|
||||
message.retained = 0;
|
||||
message.payload = payload;
|
||||
sprintf(payload, "message number %d", ++count);
|
||||
message.payloadlen = strlen(payload);
|
||||
|
||||
if ((rc = MQTTPublish(&client, CONFIG_MQTT_PUB_TOPIC, &message)) != 0) {
|
||||
ESP_LOGE(TAG, "Return code from MQTT publish is %d", rc);
|
||||
} else {
|
||||
ESP_LOGI(TAG, "MQTT published topic %s, len:%u heap:%u", CONFIG_MQTT_PUB_TOPIC, message.payloadlen, esp_get_free_heap_size());
|
||||
}
|
||||
|
||||
if (rc != 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
vTaskDelay(CONFIG_MQTT_PUBLISH_INTERVAL / portTICK_RATE_MS);
|
||||
}
|
||||
|
||||
network.disconnect(&network);
|
||||
}
|
||||
|
||||
ESP_LOGW(TAG, "mqtt_client_thread going to be deleted");
|
||||
vTaskDelete(NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
// Initialize NVS
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(ret);
|
||||
|
||||
initialise_wifi();
|
||||
|
||||
ret = xTaskCreate(&mqtt_client_thread,
|
||||
MQTT_CLIENT_THREAD_NAME,
|
||||
MQTT_CLIENT_THREAD_STACK_WORDS,
|
||||
NULL,
|
||||
MQTT_CLIENT_THREAD_PRIO,
|
||||
NULL);
|
||||
|
||||
if (ret != pdPASS) {
|
||||
ESP_LOGE(TAG, "mqtt create client thread %s failed", MQTT_CLIENT_THREAD_NAME);
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
#
|
||||
# "main" pseudo-component makefile.
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
||||
|
Reference in New Issue
Block a user