esp_mqtt_client_config_t mqtt_cfg = {
.host = "a1UF4taFJc3.iot-as-mqtt.cn-shanghai.aliyuncs.com",
.port = 1883,
.client_id = "liujiaolong|securemode=3\,signmethod=hmacsha1|",
.username = "mcuiot&a1UF4taFJc3",
.password = "29CF6A505005B36F529E4F505DFAF1270AA6EE07",
};
上面的代码是核心了,如果知道怎么来的请看第一章的阿里云相关内容,这个配置好就能保证设备可以连接阿里云了,下面我们在修改一些订阅与推送的Topic路径就可以在阿里云和串口中看先相关的数据信息,app_main.c全部代码如下(读者感觉情况修改自己的Topic路径),代码中我加了对GPIO的控制为之后上位机通过阿里云控制打下基础:
/* 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
#include
#include
#include
#include "esp_wifi.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "esp_event.h"
#include "tcpip_adapter.h"
#include "protocol_examples_common.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"
#include "lwip/sockets.h"
#include "lwip/dns.h"
#include "lwip/netdb.h"
#include "esp_log.h"
#include "mqtt_client.h"
#include "driver/gpio.h"
static const char *TAG = "MQTT_EXAMPLE";
#define GPIO_OUTPUT_IO_0 22
#define GPIO_OUTPUT_IO_1 19
static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
{
esp_mqtt_client_handle_t client = event->client;
int msg_id;
char Mqtt_data[100];
// your_context_t *context = event->context;
switch (event->event_id) {
//MQTT事件已连接
case MQTT_EVENT_CONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
//推送一个消息
msg_id = esp_mqtt_client_publish(client, "/a1UF4taFJc3/mcuiot/user/tongxin", "{\"Status\":1\,\"Data\":\"Hello\, World!\"}", 0, 1, 0);
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
//订阅
msg_id = esp_mqtt_client_subscribe(client, "/a1UF4taFJc3/mcuiot/user/tongxin", 0);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
break;
//MQTT事件断开
case MQTT_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
break;
//MQTT事件订阅
case MQTT_EVENT_SUBSCRIBED:
ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
//推送一个消息
msg_id = esp_mqtt_client_publish(client, "/a1UF4taFJc3/mcuiot/user/tongxin", "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");
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
//printf("DATA=%.*s\r\n", event->data_len, event->data);
sprintf(Mqtt_data,"%.*s", event->data_len , event->data);
printf("Mqtt_data=%s\r\n", Mqtt_data);
//根据内容控制GPIO
if (strcmp(Mqtt_data, "{\"Status\":1,\"Data\":0}") == 0) {
gpio_set_level(GPIO_OUTPUT_IO_0, 0);
}else if (strcmp(Mqtt_data, "{\"Status\":1,\"Data\":1}") == 0) {
gpio_set_level(GPIO_OUTPUT_IO_0, 1);
}else if (strcmp(Mqtt_data, "{\"Status\":2,\"Data\":0}") == 0) {
gpio_set_level(GPIO_OUTPUT_IO_1, 0);
}else if (strcmp(Mqtt_data, "{\"Status\":2,\"Data\":1}") == 0) {
gpio_set_level(GPIO_OUTPUT_IO_1, 1);
}else{
gpio_set_level(GPIO_OUTPUT_IO_0, 1);
gpio_set_level(GPIO_OUTPUT_IO_1, 1);
}
break;
case MQTT_EVENT_ERROR:
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
break;
default:
ESP_LOGI(TAG, "Other event id:%d", event->event_id);
break;
}
return ESP_OK;
}
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);
mqtt_event_handler_cb(event_data);
}
static void mqtt_app_start(void)
{
esp_mqtt_client_config_t mqtt_cfg = {
.host = "a1UF4taFJc3.iot-as-mqtt.cn-shanghai.aliyuncs.com",
.port = 1883,
.client_id = "liujiaolong|securemode=3\,signmethod=hmacsha1|",
.username = "mcuiot&a1UF4taFJc3",
.password = "29CF6A505005B36F529E4F505DFAF1270AA6EE07",
};
#if CONFIG_BROKER_URL_FROM_STDIN
char line[128];
if (strcmp(mqtt_cfg.uri, "FROM_STDIN") == 0) {
int count = 0;
printf("Please enter url of mqtt broker\n");
while (count 0 && c < 127) {
line[count] = c;
++count;
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
mqtt_cfg.uri = line;
printf("Broker url: %s\n", line);
} else {
ESP_LOGE(TAG, "Configuration mismatch: wrong broker url");
abort();
}
#endif /* CONFIG_BROKER_URL_FROM_STDIN */
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);
esp_mqtt_client_start(client);
}
void app_main(void)
{
//配置GPIO
gpio_pad_select_gpio(GPIO_OUTPUT_IO_0);
/* Set the GPIO as a push/pull output */
gpio_set_direction(GPIO_OUTPUT_IO_0, GPIO_MODE_OUTPUT);
gpio_pad_select_gpio(GPIO_OUTPUT_IO_1);
/* Set the GPIO as a push/pull output */
gpio_set_direction(GPIO_OUTPUT_IO_1, GPIO_MODE_OUTPUT);
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());
tcpip_adapter_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_app_start();
}
上面的代码需要自己理解下我有些简单的备注,下面开始烧写工程,Make Target中点击下载,开始漫长的等待,成功后会有这种提示信息,阿里云会显示在线,打开串口会看见如下信息,如图所示: