/* 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 <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#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 "event_groups.h"
#define CONNECTED_BITS (GOT_IPV4_BIT)
#define GOT_IPV4_BIT BIT(0)
static const char *TAG = "MQTT_EXAMPLE";
static EventGroupHandle_t s_connect_event_group;
static const char *s_connection_name;
static ip4_addr_t s_ip_addr;
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;
// your_context_t *context = event->context;
switch (event->event_id) {
// 建立连接成功
case MQTT_EVENT_CONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
// 发布主题/topic/qos1,主题消息为“data_3”,qos=1
// msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data_3", 0, 1, 0);
msg_id = esp_mqtt_client_publish(client, "Server_subscribe", "I'am_client",
Esp32-ETHERNET-Kit mqtt与wifi自由选择功能整合
最新推荐文章于 2025-10-30 14:54:11 发布
本文介绍了一个使用ESP32进行MQTT通信的例子,包括连接到Wi-Fi网络、启动MQTT客户端并实现消息发布与订阅等功能。

最低0.47元/天 解锁文章
753

被折叠的 条评论
为什么被折叠?



