Arduino/ESP32 无法连接到 MQTT 代理

问题描述 投票:0回答:1

我正在努力让我的 ESP32 板(还有 Arduino R3 和 Nano)连接到 HiveMQ 代理,但它无法连接。我尝试过各种端口、不同的经纪人、禁用网络上的广告拦截等,但似乎没有什么区别。

#include <Arduino.h>
#include <ArduinoJson.h>
#include <WiFi.h>
#include <PubSubClient.h>

void MQTT_callback(char* topic, byte* message, unsigned int length);

void setup() {
    Serial.begin(115200);
    while (!Serial);
    delay(250);

    Serial.println("setup beginning");

    
    Serial.print("Connecting to WiFi...");
    WiFi.begin("...", "...");
    for (int i=0; i<30 && WiFi.status() != WL_CONNECTED; ++i) {
        Serial.print(".");
        delay(250);
    }

    if (WiFi.status() != WL_CONNECTED) {
        Serial.println(" failed");
        Serial.println("Oh no, no Wifi!? You're back in 1980! That's horrible...");
        return;
    } else {
        Serial.println(" connected!");
    }
    

    WiFiClient espClient;
    PubSubClient client(espClient);

    const char* MQTT_URL = "somereallylongstring.s2.eu.hivemq.cloud";
    const int MQTT_PORT = 8883;
    const char* MQTT_uid = "...";
    const char* MQTT_pwd = "...";
    const String MQTT_client = "esp32-client-" + WiFi.macAddress();

    client.setServer(MQTT_URL, MQTT_PORT);
    client.setCallback(MQTT_callback);
    
    if (!client.connected()) {
        Serial.println("Connecting to MQTT broker with client '" + MQTT_client + "'...");
        for (int i=0; i<10 && !client.connected(); ++i) {

            if (client.connect(MQTT_client.c_str(), MQTT_uid, MQTT_pwd)) {
                Serial.println("Connected to MQTT broker");
            } else {
                Serial.print("Connection to MQTT broker failed: ");

                #define C(x) case (x): Serial.println(#x); break;
                switch (client.state()) {
                    C(MQTT_CONNECTION_TIMEOUT)
                    C(MQTT_CONNECTION_LOST)
                    C(MQTT_CONNECT_FAILED)
                    C(MQTT_DISCONNECTED)
                    C(MQTT_CONNECTED)
                    C(MQTT_CONNECT_BAD_PROTOCOL)
                    C(MQTT_CONNECT_BAD_CLIENT_ID)
                    C(MQTT_CONNECT_UNAVAILABLE)
                    C(MQTT_CONNECT_BAD_CREDENTIALS)
                    C(MQTT_CONNECT_UNAUTHORIZED)
                }
                #undef C
            }
        }
    }

    if (client.connected()) {
        const char* topic = "/sensors/sean/salt_tank";

        StaticJsonDocument<MQTT_MAX_PACKET_SIZE> doc;
        doc["ts"] = "2023-01-15 20:24:00";
        doc["temp"] = 82.1;
        doc["tds"] = 821;
        doc["ph"] = 7.6;

        String output;
        serializeJson(doc, output);

        Serial.println("Publishing message to " + String(topic) + output);
        client.publish(topic, output.c_str());
    }


    Serial.println("setup complete!");
}


void loop() {

}


void MQTT_callback(char* topic, byte* message, unsigned int length) {
    String msg = "";

    for (int i=0; i<length; ++i)
        msg += (char)message[i];

    Serial.println("Message arrived on topic " + String(topic) + ":");
    Serial.println(msg);
}

这是输出:

22:11:00.629 > setup beginning
22:11:00.631 > Connecting to WiFi........ connected!
22:11:02.001 > Connecting to MQTT broker with client 'esp32-client-08:3A:F2:B8:8B:CC'...
22:11:17.512 > Connection to MQTT broker failed: MQTT_CONNECTION_TIMEOUT
22:11:32.670 > Connection to MQTT broker failed: MQTT_CONNECTION_TIMEOUT

我不知道这是否有什么不同,但我使用的是 ARM MacBook。我不知道库的差异是否会导致问题。谢谢!

c++ arduino mqtt esp32 hivemq
1个回答
0
投票

我在尝试使用用户和密码连接代理时遇到了类似的问题,但我使用的是 micropython。我已经连接到免费的 hivemq 经纪人,即“经纪人客户端仪表板”,当我尝试使用我在 acc 中创建的经纪人时购买,只是我无法意识到连接。你的问题怎么样?可以解决吗?

© www.soinside.com 2019 - 2024. All rights reserved.