Local Broker connection failed (rc=-2)

Hello,

I installed mosquitto as a Broker on my lokal Windows (because I dont have a Pi atm). Now I connected my Arduino with Ethernetshield 2 via an Adapter to my pc. I changed the mosquitto.conf with following lines “allow_anonymous true”, “per_listener_settings true”, “listener 1884” and “bind_address 192.168.xx.x”. Everything works fine with the localhost. With the MQTT Explorer its visable that I can creat a new topic via cmd. I created a pw and username for my mosquitto broker.

Now to my Problem: The Arduino cant get a connection to the Broker. (Attempting MQTT connection…failed, rc=-2) Trying to change the Ports didnt help me out. Looking to Wireshark it says:

189	42.349355	192.168.xx.3	192.168.xx.2	TCP	60	[TCP Retransmission] [TCP Port numbers reused] 49247 → 1883 [SYN] Seq=0 Win=2048 Len=0 MSS=1460

but no more data to this port or from this ip.
Is it possible that my Windows blocks more data or that I need to configure the port?

My Arduino Code:
/*
Basic MQTT example

This sketch demonstrates the basic capabilities of the library.
It connects to an MQTT server then:

  • publishes “hello world” to the topic “outTopic”
  • subscribes to the topic “inTopic”, printing out any messages
    it receives. NB - it assumes the received payloads are strings not binary

It will reconnect to the server if the connection is lost using a blocking
reconnect function. See the ‘mqtt_reconnect_nonblocking’ example for how to
achieve the same result without blocking the main loop.

*/

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, xx, 3);
IPAddress server(192, 168, xx, 2);

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}

EthernetClient ethClient;
PubSubClient client(ethClient);

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("arduinoClient", "client", "test")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outTopic","hello world");
      // ... and resubscribe
      client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup()
{
  Serial.begin(9600);

  client.setServer(server, 1883);
  client.setCallback(callback);

  Ethernet.begin(mac, ip);
  // Allow the hardware to sort itself out
  delay(1500);
}

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

I am new to mosquitto and MQTT so I am thankfull for everyone who can help me.
Thank you!

Hello!

It sounds like you’re doing a reasonable job of moving from a simple case on your local computer to check things are working before moving to the more complicated case of using the arduino. That’s a good plan. It’s possible there are still problems on both ends though, so let me make some suggestions.

If you have access to a second computer, try using MQTT Explorer on that computer to connect to the computer with the broker on - then you will have confirmed that the broker can accept remote connections, and that the password configuration is correct.

Another step would be to try connecting your arduino to a public test broker like test.mosquitto.org port 1883 - this is accessible to anyone and has no authentication, so you can be reasonably confident that if your arduino does not connect then it is something on the arduino not working correctly. Doing this removes the need to get authentication correct.

If you get both of those steps working, then you’ll be back to trying your arduino with your own broker and hopefully it will go smoothly.

Cheers,

Roger

Hello,

to verify, if the Windows firewall is blocking your incoming connection you may temporarely disable the firewall (in the system security setting) and test again. If this fixes the problem you might need to add/adapt the firewall allowing incoming connections in the specified port.
Normally you should see a response packet in the wireshark.

Best,
Norbert

Hey there,

Thanks for the fast replies. Unfortunately the project is for my studies at work so I am a little restricted. I cant disable my Firwall nor does my arduino have access to the internet.

I can try to install a virtuell machine and install the MQTT Explorer there or send some data to the broker.