MQTT, Arduino MEGA client gets disconnected after some time

Hi, my name is Josep Mencion and I’m doing a research school project on home automation.
I have an Arduino MEGA 2560 with an Ethernet Shield which connects to the Raspberry Pi 1883 MQTT broker and publishes sensors data and subscribes to some topics to actuate.

I send temperature, humidity and luminosity data to the broker.

All seems to work, but the Arduino MEGA doesn’t send the data for more than 2 days. I explain: without any explanation, the Arduino disconnects from the broker and stops sending the data.

I attach the code on the next message because if not, I cannot create such a large message here.

.- I have attempted changing my Arduino MEGA board and my Ethernet Shield and the behaviour is the same.
.- The Arduino MEGA is an original board.

Probably there's a code error I can't find (for example, on the DHT.22 read?
I would be very happy if someone could find me the mistake.

I have thought that also could be an electrical problem on the power supply, even thought that I have changed my USB supply and the behaviour is the same. But it comes to my mind that I currently have my Arduino MEGA connected to the USB 5V to the computer and it also has de circular connector and I could connect the Arduino MEGA to the two supplies (12 V and 5 V computer USB).

Thank you very much,

Best,
Josep Mencion

The code is too large and I cannot send it by here. So I wrote it on the openHAB forum and it's here: MQTT, Arduino MEGA client gets disconnected after some time - Beginners - openHAB Community

Thank you very much,

Best,

Josep

The arduino, unlike the rpi, does not have lots of memory. So you shouldn't make use of String class. Because eventually, the String class (unless being use with extra caution) makes holes in the memory causing the arduino to crash.

Iepa!

I found the mistake in the code, and I paste it here to who it may serve. It was because when I defined the variable I only reserved 3 decimals.

So, in the Arduino code I have now defined these three variables with 13 significant figures reserved. It’s a really strange number, but it’s too big to have reserved decimal proms.

The problem was that when a variable extended from these 3 significant figures which were reserved before, the Arduino code crashed.

char lumStr[13];
char humStr[13];
char tempStr[13];

The unique modification was that 13 you see there, before was a 3. Now it's a 13.

Thanks a lot and best,
Josep