2

I'm working on getting my ESP32 board (also Arduino R3 & Nano) to connect to a HiveMQ broker, but it will not connect. I've tried various ports, different brokers, disabling ad blocking on my network, etc., but nothing seems to make a difference.

#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);
}

Here's the output:

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

I don't know if this makes a difference, but I'm using an ARM MacBook. I don't know if the difference in libraries could be causing an issue or not. Thanks!

Dan Champagne
  • 890
  • 2
  • 17
  • 37
  • 3
    Port 8883 is MQTT over TLS. Start with port 1883 so you don't have to worry about the certs while debugging the rest. Also have you tested you can connect with another client? – hardillb Jan 17 '23 at 14:44
  • I don't know what I was thinking when I posted this, because it didn't occur to me to try another service. Switching to a different broker and changing the port did the trick! If you want to post this as an answer, I'll upvote it. – Dan Champagne Jan 18 '23 at 00:56

0 Answers0