Can't connect to MQTT Broker. Connection failed, error code = -2

Hello,

I've been working on connecting my ESP8266 to an MQTT broker (eclipse-mosquitto) running in Docker on Windows. However, I'm encountering constant connection issues with an error code of -2, and I'm unable to establish a connection. Previously, I had successfully connected to Adafruit IO without any problems, so my internet connection appears to be fine. I also attempted to connect using Curl on Termux (Android phone) within the same network, but it failed to connect as well. I'm unsure whether the issue lies with my code, configuration, or possibly my WiFi network. I've tried various solutions found online, but none have resolved my problem.

Could you please provide some guidance on how to further investigate the issue?

Here is the code I use for testing purpose:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>

const char* ssid = "ssid";
const char* password = "password";
const char* mqttServer = "172.17.0.7";
const int mqttPort = 1883;
// const char* mqttUser = "admin";
// const char* mqttPassword = "admin";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  Serial.begin(115200);
  
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  client.setServer(mqttServer, mqttPort);
  client.setCallback(callback);

  while (!client.connected()) {
    Serial.println("Connecting to MQTT Broker...");
    if (client.connect("ESP8266Client")) {
      Serial.println("Connected to MQTT Broker");
    } else {
      Serial.print("Failed, rc=");
      Serial.print(client.state());
      Serial.println(" Retrying in 5 seconds...");
      delay(5000);
    }
  }
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  int lightValue = analogRead(A0);

  StaticJsonDocument<200> jsonDocument;
  jsonDocument["value"] = lightValue;

  char buffer[200];
  serializeJson(jsonDocument, buffer);

  client.publish("LIGHTSENSOR/values", buffer);

  delay(1000);
}

void callback(char* topic, byte* payload, unsigned int length) {
  // Obsługa otrzymanych wiadomości MQTT
}

void reconnect() {
  while (!client.connected()) {
    Serial.println("Connecting to MQTT Broker...");
    if (client.connect("ESP8266Client")) {
      Serial.println("Connected to MQTT Broker");
    } else {
      Serial.print("Failed, rc=");
      Serial.print(client.state());
      Serial.println(" Retrying in 5 seconds...");
      delay(5000);
    }
  }
}

Hello again.

Like I said I have tried various solutions found on the internet, but none of them seemed to work. After thorough investigation, I realized that everything was properly configured on my side, except for one crucial mistake. Instead of using the IPv4 address of my computer where Docker is running, I was attempting to connect to the IP address of Mosquitto within Docker. It was a simple mistake, but it caused a lot of confusion.

Here's how I came to this realization:

I am using the Windows operating system.

To check if port 1883 is open, I used nmap in the Windows Command Prompt with the following command:
nmap -p 1883 -Pn 172.17.0.7

The result showed that the port was filtered for that IP address, leading me to believe that there might be an issue with porting.

I went through the Docker documentation and reviewed the port forwarding settings, but everything seemed fine.

After spending some time investigating the network side of things, I decided to check if the port was open for my IPv4 address instead of , and it turned out to be open with mqtt as a service . That's when I realized my mistake.

I understand that this issue may seem simple, but during my learning process, I had the misconception that I needed to set up the IP of Mosquitto inside Docker, rather than using the IP of the computer where Docker is running. It was a combination of reading the Docker documentation and receiving advice from friend that ultimately helped me resolve this problem.

I hope this post can help someone who might encounter a similar issue.

Here is an old thread about a similar problem. You can check this out: ESP8266 can't connect to MQTT broker - #12 by ducle27

This isn't relevant as the server you connecting to is in the private range so must be a local server. Using a notebook in the same WiFi as the ESP8266 are you able to connect to the Mosquitto instance?

Curl is a HTTP client, so something would have been wrong if it could connect.

Docker container are (by default) not visible outside the host PC. Did you made the configurations to make the container content available on the whole network? Don't ask me how to do that on Windows, I use a more capable OS.

Ok so I migh have not been precise about what happened, but my issue is resolved now. I changed the ip in the code from mqtt container ip, to my IPv4 address of my computer that the Docker is running on. That was the case. If needed, we can close this topic. The process for resolving the case is described above.

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