#include
#include
#include
#include "nvs_flash.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "wifi.h"
#include "mqtt.h"
static const char *TAG = "MAIN";
void app_main(void)
{
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_ERROR_CHECK(nvs_flash_init());//初始化nvs flash
wifi_init();
while(1)
{
vTaskDelay(pdMS_TO_TICKS(2000));
mqtt_data_publish();
}
}
#include "wifi.h"
#include "mqtt.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#define WIFI_SSID "********" // 你的WiFi名称
#define WIFI_PASS "********" // 你的WiFi密码
static const char *TAG = "WIFI";
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
switch (event_id) {
case WIFI_EVENT_STA_START:
ESP_LOGI(TAG, "Connecting to %s", WIFI_SSID);
esp_wifi_connect();
break;
case WIFI_EVENT_STA_CONNECTED:
ESP_LOGI(TAG, "Connected to %s", WIFI_SSID);
break;
case WIFI_EVENT_STA_DISCONNECTED:
ESP_LOGI(TAG, "Disconnected from %s", WIFI_SSID);
esp_wifi_connect();
break;
case IP_EVENT_STA_GOT_IP:
ESP_LOGI(TAG, "Got IP address");
mqtt_app_init();
break;
default:
break;
}
}
void wifi_init(void)
{
tcpip_adapter_init();// 初始化TCP/IP协议栈
ESP_ERROR_CHECK(esp_event_loop_create_default());// 初始化WiFi事件处理程序
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();// 配置WiFi连接参数
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));// 注册WiFi事件处理程序
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));// 设置WiFi工作模式为Station模式
wifi_config_t wifi_config = {// 配置WiFi连接信息
.sta = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
},
};
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));// 设置WiFi连接信息
ESP_ERROR_CHECK(esp_wifi_start());// 启动WiFi
}
#ifndef __WIFI_H
#define __WIFI_H
void wifi_init(void);
#endif
#include "mqtt.h"
#include "mqtt_client.h"
#include "esp_log.h"
#define ESP_MQTT_HOST "183.230.40.39"
#define ESP_MQTT_URI "mqtt://mqtt.heclouds.com"
#define ESP_MQTT_PORT 6002
#define ESP_CLIENT_ID "********"//设备id
#define ESP_PRODUCT_ID "********"//产品id
#define ESP_CLIENT_KEY "********"//API KEY
static const char *TAG = "MQTT";
esp_mqtt_client_handle_t client;
bool mqtt_connect_flag=false;
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;
int msg_id;
client = event->client;
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, "heart_report_topic", 0);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
mqtt_connect_flag=true;
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);
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);
break;
case MQTT_EVENT_ERROR:
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno));
}
break;
default:
ESP_LOGI(TAG, "Other event id:%d", event->event_id);
break;
}
}
void mqtt_app_init(void)
{
esp_mqtt_client_config_t mqtt_cfg = {
.uri = ESP_MQTT_URI,
.port = ESP_MQTT_PORT,
.client_id = ESP_CLIENT_ID,//设备id
.username = ESP_PRODUCT_ID,//产品id
.password = ESP_CLIENT_KEY,//鉴权信息
};
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);
}
void mqtt_data_publish(void)
{
char data_str[50];
int msg_id=0;
if(mqtt_connect_flag==true)
{
snprintf(data_str,sizeof(data_str),"/{%s}/{%s}/heartbeat/report",ESP_PRODUCT_ID,ESP_CLIENT_ID);
msg_id = esp_mqtt_client_publish(client, "heart_report_topic",data_str, 0, 0, 0);
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
}
}
#ifndef __MQTT_H
#define __MQTT_H
void mqtt_app_init(void);
void mqtt_data_publish(void);
#endif
如遇到找不到头文件报错,需注意CMakeLists.txt配置。
吾辈必当勤勉,持书仗剑耀中华。