我正在尝试将使用ESP-IDF框架运行的ESP32连接到MQTT。我已成功导入此MQTT library并已将配置设置为如下所示:

static void mqtt_app_start(void)
{
    const esp_mqtt_client_config_t mqtt_cfg = {
        // .host = "m15.cloudmqtt.com",
        .uri = "mqtt://rxarkckf:[email protected]:10793", // uri in the format (username:password@domain:port)
        // .host = "m15.cloudmqtt.com", // config with host, port, user, password seperated
        // .port = 10793,
        // .username = "rxarkckf",
        // .password = "smNb81Ppfe7T",

        .event_handle = mqtt_event_handler,
        // .user_context = (void *)your_context
    };

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


我叫mqtt_app_start();在我的app_main函数中。上载代码后,我的ESP-32无法连接到MQTT代理,并输出以下内容:

␛[0;32mI (12633410) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000␛[0m
␛[0;31mE (12633710) MQTT_CLIENT: Error network response␛[0m
␛[0;32mI (12633710) MQTT_CLIENT: Error MQTT Connected␛[0m
␛[0;32mI (12633710) MQTT_CLIENT: Reconnect after 10000 ms␛[0m
␛[0;32mI (12633710) MQTT_SAMPLE: MQTT_EVENT_DISCONNECTED␛[0m


我仔细检查了主机,用户名,密码和端口的值是否正确。当我查看cloudmqtt.com托管的Web界面上的日志时,可以看到以下输出:

2018-11-17 03:50:53: New connection from 73.94.66.49 on port 10793.
2018-11-17 03:50:53: Invalid protocol "MQIs�" in CONNECT from 73.94.66.49.
2018-11-17 03:50:53: Socket error on client <unknown>, disconnecting.
2018-11-17 03:51:20: New connection from 73.94.66.49 on port 10793.

最佳答案

我使用过蚊子也有类似的经验。

将此行添加到mqtt_config.h使我的mqtt工作。

#define CONFIG_MQTT_PROTOCOL_311


我认为设置此配置的更正确方法是在sdkconfig.h中,可以手动或使用“ make menuconfig”

关于c - ESP-32无法连接到MQTT代理:mqtt_client:网络响应错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53355096/

10-11 01:18