Arduino Mega MQTT issues - Lost connection to MQTT Broker

Hi Everyone, i have a issue on my Arduino Mega with Ethernet Shield on, i am using this code:

#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, 234);
IPAddress server(192, 168, 0, 100);

void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
}

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

void setup()
{
Ethernet.begin(mac, ip);
// Note - the default maximum packet size is 128 bytes. If the
// combined length of clientId, username and password exceed this,
// you will need to increase the value of MQTT_MAX_PACKET_SIZE in
// PubSubClient.h

if (client.connect(“ArduinoMegaMQTTNODE”, “mqttusername”, “mqttpassword”)) {
client.publish(“outTopic”,“hello world”);
client.subscribe(“inTopic”);
}
}

void loop()
{
client.loop();
}


When its started and connected to MQTT i can see its connected, so its working fine, but after 2-3 minutes of connected on my MQTT Broker its disconnect it self, i have alot of MQTT clients to that broker, i can still ping the Arduino, if i unplug the Arduino and plug it back in, its working again for 2-3 minutes, can anyone try my code on the hardware, and see if its only my boards is not working properly?

You might have sketches with identical MQTT client name?

Jhh

1 Like

Could that really be the reason?

I tried default Arduino MQTT sketch with not fancy stuff in it, and it do the same thing.

Meaby its not working MQTT with Arduino Mega and Ethernet shield.

Multiple Clients with the same ClientID will cause the broker to go unstable and lose connection

My ClientID is: “ArduinoMegaMQTTNODE” and i changed that to some ransom number, i tried that, but its nothing with that.
Can someone confirm that Arduino mega + Ethernet Shield is stable with MQTT?

I know aduino mega over esp8266 serial is stable. Shouldn’t be a problem with Ethernet

Could you confirm or test it if possible?

I don’t have any Ethernet hats

Anyone else?
Could someone try if its working stable?

I’m not sure why you think the ethernet would somehow make it unstable.

Interestingly enough, your sketch doesn’t do anything inside the loop. Is that intentional? You have client.loop(); but looping what? Your publish command is in the setup. Are you just listening for a topic on it, and that’s it? I guess I don’t understand what the sketch is intended to do.

Hi flamingm0e, sure i will tell you.

I have Arduino Mega with Ethernet shield on it.
I got a bunch of light switch around the house i will hook to the board for wired light swithing of the lights.

I watched Superhouse tutorial from here:

The sketch not working when i am using a simple MQTT Authentication example and connect to my MQTT Broker and then leave the connection established.
i can ping the IP, and i can see its connected to my MQTT Broker first time i start it up, but after 2-3 minutes its disconnected my board from the broker.

i can when unplug and plug it in again and its working for 2-3 minutes.

You see it disconnect from the broker?

I can see its connected to my MQTT Broker, i tryed EMQTT Broker and Mosquitto Broker.
Its connected for 2-3 minutes, and after 2-3 minutes it can’t see it in the connection on my MQTT Broker.

Have you thought about logging things in the serial on the arduino too? So you can see any errors on there? Like it hanging and not looping correctly?

From your sketch, it connects, broadcasts hello world, and then does nothing. It probably disconnects because you aren’t telling it to do anything.

In your loop, put a delay (1000) should be fine, and do a client.publish(“I’m HERE!”) to your broker.

Could you give me an example of the delay in the void loop or client loop?

void loop(){
  client.loop();
  delay(1000);
  client.publish("topic/on/broker", "I'm still here!");
}

Thanks for the example, but i can see my broker receive that message on client.publish, but after 10-15 messages its stopped, i can still ping my ethernet shield / Arduino.

Any other things i can try?

DK,

Try mqtt_reconnect_nonblocking sketch in the PubSubClient library. I have 3 megas with W5100 ehternet shield based on that one so I can confirm this works.

One is reading an array of photoresistors looking at LED lights on 3 old domotics controllers each 12 outputs and reporting lights on/off and is switching those outputs on/off over infrared. So those are 36 MQTT lights with status topic and command topic.

Other one is reporting air and water temperature and controlling valves for a pool solar heating installation. This is an MQTT sensor and switches.

Last one is a lawn sprinkler controller - getting status of 4 zones and switching zones on and off, so again status and command topic.

Combined they have sent thousands of messages and this is super stable, only reason to restart them is because I’m extending functionality.

Jhh

This doesn’t indicate anything really except that the device is still connected to the network.

Yearh, so its should not be the board.

Right now i get message more than 3 minutes now, so i will keep testing it.

I will like to see your setup @jhhbe