首页 > 解决方案 > ESP32 MQTT 事件处理

问题描述

我正在尝试将文件 mqtt_client.h 中定义的 arduino-esp32 方法用于我的ESP32/Arduino IDE项目:

我正在努力为此制作一个事件处理程序。

我目前有:

#include <mqtt_client.h>

void MQTTEvent(esp_mqtt_event_t event) {
  // process event
}

void setup() {
  esp_mqtt_client_config_t mqtt_cfg;
  mqtt_cfg.host = "192.168.0.126";
  mqtt_cfg.port = 1883;
  mqtt_cfg.client_id = "ESP32";
  mqtt_cfg.uri = "HOMEPC";
  mqtt_cfg.event_handle = MQTTEvent;

  esp_mqtt_client_handle_t mqtt_client = esp_mqtt_client_init(&mqtt_cfg);
  esp_mqtt_client_start(mqtt_client);
}

void loop() {

}

...但它失败了:

*invalid conversion from 'void (*)(esp_mqtt_event_t)' to 'mqtt_event_callback_t {aka int (*)(esp_mqtt_event_t*)}' [-fpermissive]*

on line mqtt_cfg.event_handle = MQTTEvent;

我找不到任何例子。有人可以帮我吗?

标签: cmqttesp32

解决方案


你的事件句柄的类型是错误的,它应该返回一个esp_err_t所以而不是:

esp_err_t MQTTEvent(esp_mqtt_event_t *event) {
  ...
  return ESP_OK; // you will need to return esp error codes
}

请参阅此处的定义:https ://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/protocols/mqtt.html#_CPPv419esp_mqtt_event_id_t


推荐阅读