ArduinoMqttClient cannot connect to broker

Is this supposed to work?

#include <SoftwareSerial.h>
#include <WiFiEsp.h>
#include <ArduinoMqttClient.h>

// ESP8266 wifi board things.
#define RX 6 // blue
#define TX 7 // black
SoftwareSerial Esp8266(RX, TX);
const char AP[] = "ap";
const char PASS[] = "secret";
WiFiEspClient wifi_client;

// mqtt things.
const char mqtt_broker[] = "192.168.0.113";
const int mqtt_port = 1883;
const char mqtt_topic[] = "zigbee2mqtt/lux";
MqttClient mqtt_client(wifi_client);



void setup() {
   pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  Esp8266.begin(9600);
  WiFi.init(&Esp8266);
  
  // Check for the presence of the shield.
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true);
  }

  // Connect to WiFi network.
  while ( WiFi.status() != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(AP);
    WiFi.begin(AP, PASS);
  }
  Serial.println("WIFI connected.");

  while (!mqtt_client.connect(mqtt_broker, mqtt_port)) {
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqtt_client.connectError());

    delay (3000);
  }

  mqtt_client.subscribe("zigbee2mqtt/lux");
}

void loop() {
  int messageSize = mqtt_client.parseMessage();
  if (messageSize) {
    // we received a message, print out the topic and contents
    Serial.print("Received a message with topic '");
    Serial.print(mqtt_client.messageTopic());
    Serial.print("', length ");
    Serial.print(messageSize);
    Serial.println(" bytes:");

    // use the Stream interface to print the contents
    while (mqtt_client.available()) {
      Serial.print((char)mqtt_client.read());
    }
    Serial.println();

    Serial.println();
  }
}

It gets stuck in the connection loop saying

MQTT connection failed! Error code = -1

Same problem using test.mosquitto.org and arduino/simple.

(Tried PubSubClient which can connect but doesn't receive any messages.)

Is there a known working MQTT subscriber for Arduino?

const char mqtt_broker[] = "192.168.0.113";

Are you sure that is the correct format for a broker address when using its IP address ?

I tried ArduinoMqttClient's WiFiEcho example, and test.mosquitto.org 's IP address 91.121.93.94 also worked fine.

The mqttClient.connect is attempted once with an if, not in a while loop. If it doesn't work the first time, it's likely not going to.

Looks like you're using a different WiFi client

than the ones in the example. Try the appropriate one; maybe they are more compatible

#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_AVR_UNO_WIFI_REV2)
  #include <WiFiNINA.h>
#elif defined(ARDUINO_SAMD_MKR1000)
  #include <WiFi101.h>
#elif defined(ARDUINO_ARCH_ESP8266)
  #include <ESP8266WiFi.h>
#elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_GIGA) || defined(ARDUINO_OPTA)
  #include <WiFi.h>
#elif defined(ARDUINO_PORTENTA_C33)
  #include <WiFiC3.h>
#elif defined(ARDUINO_UNOR4_WIFI)
  #include <WiFiS3.h>
#endif

BTW, with ESP8266, you also should be able use WiFi.h, since it has been added as an alias to ESP8266WiFi.h

If you actually want to try the WiFiEcho example, I did have to tweak the WiFi connect loop. You don't repeatedly begin, you only do it once; then check the status

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

No; but I don't see why it wouldn't be.
Are you?

And "test.mosquitto.org" also doesn't work.

I have WiFi working in other sketches so I don't see why it would be a problem here.

Tried

#include <WiFi.h>

but that doesn't even compile

mqtt-a.ino:2:10: fatal error: WiFi.h: No such file or directory
 #include <WiFi.h>
          ^~~~~~~~
compilation terminated.
exit status 1

Compilation error: WiFi.h: No such file or directory

That doesn't do anything different: in both cases if the first call to WiFi.begin is successful then it is not called again and the code proceeds to attempt to connect to the mqtt broker.

We can see from looking at the library source code that the connect function has multiple overloads:

If you want to specify a host name, you pass it a C string, as is done in the library examples:

But if you want to specify an IP address, then pass the function an IPAddress.

You can learn about the IPAddress type here:

IPAddress is actually part of the standard Arduino core API, but for some reason (perhaps because it was not in the core API at that time?) the people who wrote the documentation put it in the individual documentation of a library that happened to use the type instead of in the Arduino Language Reference where it belongs.

Sorry, turns out that's in the master branch, but not in the latest release, 3.1.2, which is almost 18 months old.

Did you try ESP8266WiFi.h ?

Could be some library interaction, or some change in the API. If in the "get things to work" mode, best to use the library the ArduinoMqttClient folks (presumably) tested with their own code.

True if begin is always a blocking call that waits until success or failure. If instead it initiates the connection and then you have to wait while the WiFi stack works in the background, then it matters whether calling begin will re-initialize the connection attempt.

More to the point, on the ESP32 I have handy, this works

  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

after printing a few dots; while this doesn't

  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    // failed, retry
    Serial.print(".");
    delay(500);
  }

And to verify

  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    Serial.print("begin ");
    Serial.println(WiFi.status());
    delay(5000);
    Serial.print("waited ");
    Serial.println(WiFi.status());
  }

prints

10:51:35.433 -> begin 6
10:51:40.413 -> waited 3
10:51:40.446 -> begin 6
10:51:45.434 -> waited 3

which shows it actually connects. The API -- while it has the same function signatures -- has changed so that the library has to be used in a different way.

Having said all that, is that related to your specific problem? Who knows; just going through the process of elimination.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.