Search code examples
raspberry-pimqttarduino-unomosquitto

mqtt between arduino and raspberry pi


I'm trying to prepare for my master project in my last year by getting my head around mqtt. I've successfully installed mosquitto on my RPi, ran the test publish and subscribe (hello/world). I also managed to connect to the broker using the android app myMQTT and everything seams to run just fine. The problems start when i try to connect to the broker with an arduino with ethernet shield using the PubSubClient library by knolleary. Everything is connected to one switch (router-Rpi-arduino). I've made sure that the arduino has an unique ip-address an mac-address (I checked it in the router). The server ip-address is also correct as i'm using it to ssh into the Rpi.. The code running on the arduino keeps returning:

Attempting MQTT connection...failed, rc=-4 try again in 5 seconds

The returncode means connection timeout..

I'm running a fresh installed mosquitto on the pi so there's no username or password required to connect to the broker. Does anyone knows what i'm doing wrong here? I've been looking over the internet for a while now and i can't seem to figure it out.

Code running on the arduino:

   /*
 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, 0xAA };
IPAddress ip(192, 168, 1, 41);
IPAddress server(192, 168, 1, 26);

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")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      //client.publish("outTopic","hello world");
      // ... and resubscribe
      client.subscribe("hello/world");
    } 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(57600);
  client.setServer(server, 1883);
  client.setCallback(callback);
  Ethernet.begin(mac, ip);
  // Allow the hardware to sort itself out
  delay(3000);
}

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

Solution

  • From the docs for the PubSubClient, the rc=-4 means the connection attempt has timed out. It has established a TCP connection, but the server has not responded to the MQTT connection attempt.

    What version of mosquitto are you using? It is possible you are using an older version that doesn't support MQTT 3.1.1 that the PubSubClient defaults to now. If that is the case, you can change the MQTT_VERSION value in PubSubClient.h to revert back to MQTT 3.1.