Go Down

Topic: Arduino as MQTT Client Disconnecting Problem (Read 465 times) previous topic - next topic


Hi Everybody,

I am trying to use Arduino's MQTT client library(pubsub) to send input states of an arduino to the broker.

I am using knollery's pubsub library but there are some problems. I am trying to use examples of library for first test and to understand the library better, but the examples not working! My arduino is connecting again and again and again to the broker. So there are no reason to disconnect from the broker I think but I couldnt found what am I missing. Because of a unknown reason Arduino is disconnecting and trying to connect again.

I am trying to use mqtt_basic example and I just changed few things in code. Like broker ip, topics and ethernet.begin algorithm. I prefer to use DHCP thats why I add something about ethernet.begin.

I add the code below and I add some "******" signs to show you where I change.

So my questions is;

1-Why my arduino is connecting once in a second, actually why it is disconnecting?

2-Where is the connect to the broker as a client command inside of reconnect() function?

3-How the connection works between the Ethernet.client and mqttclient so there are some lines to connect ethernet client and pubsub client. How is that works?

4-As I read, the callback function as it is, to check if our published message from arduino is arrived or not arrived to the broker isn't it? I couldnt understand it clearly.

5-My arduino calling the reconnect function so many time but I am not receivng the messages from another device. But arduino printing "connected" to serial port. If arduino publish a 100 messages to broker few of them are reaching. I am checking the reached messages from two different subscribed clients. And that numbers are not same all the time, some times %2 of messages are reaching and sometimes %50 of messages is reaching its unstable.

I couldnt understand it why are that examples not working, what am I missing even thought I read the documents?

I attached the codes. Thanks in advance...
Notice: Sorry because of my bad english :(

Code: [Select]
 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, 0, 5);//IP adress to use if DHCP failed
IPAddress server(85, 119, 83, 194);//IP adress of the mosquitto public test broker

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

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")) {
      // Once connected, publish an announcement...
      client.publish("2177800/1206/arduino","I connected again!");
      // ... and resubscribe
    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying

void setup()

  client.setServer(server, 1883);

  ////////////////////////////////////*****************I add here!!! Because I have some problems about connecting to router. But its ok by this way.
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);

  // Allow the hardware to sort itself out

void loop()
  if (!client.connected()) {
//  client.loop();

Go Up