Arduino program hang in less than 24 hours

Hi all,
I have an arduino mega that monitor 18 sensors ds18b20 and also control 18 relays.
The mega is network wired and sends / receive to an mqtt server.
For some unknown reason to me it is hang after around 18-20 hours. I can see that looking on the db where node red server insert data received from mega.
I will put here the code maybe some of you experienced can spot some issue in coding...I don t se other reason for that so I guess maybe the way I wrote the code is not good...

Seems I am not able to write the code here so I will upload as ino file

When compiling I get this

Sketch uses 22836 bytes (8%) of program storage space. Maximum is 253952 bytes.
Global variables use 1535 bytes (18%) of dynamic memory, leaving 6657 bytes for local variables. Maximum is 8192 bytes.

so I assume is enough free memory

Thank you

heating.ino (9.21 KB)

Hi,
Does the on board LED blink indicating that the temperatures are being read?

Have you checked your code for variables and their values that may try to be bigger than the type they are assigned.
For example int variables can only be -32,768 to 32,767 before over flowing.

Tom... :slight_smile:

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

When you find yourself writing code like this

  pinMode(pinC01, OUTPUT);
  pinMode(pinC02, OUTPUT);
  pinMode(pinC03, OUTPUT);
  pinMode(pinC04, OUTPUT);
  pinMode(pinC05, OUTPUT);
  pinMode(pinC06, OUTPUT);
  pinMode(pinC07, OUTPUT);
  pinMode(pinC08, OUTPUT);
  pinMode(pinC09, OUTPUT);
  pinMode(pinC10, OUTPUT);
  pinMode(pinC11, OUTPUT);
  pinMode(pinC12, OUTPUT);
  pinMode(pinC13, OUTPUT);
  pinMode(pinC14, OUTPUT);
  pinMode(pinC15, OUTPUT);
  pinMode(pinC16, OUTPUT);

it is time to learn about arrays. All that could be done in 3 lines of code similar to this

for (byte n = 0; n < numPins; n++) {
  pinMode(arrayOfPinNumbers[n], OUTPUT);
}

...R

"Arduino program hang in less than 24 hours"

Automatic answer: You are trying to use a "String" object. :astonished:

Don't!. As per Robin2's reply. :roll_eyes:

lucian_v:
so I assume is enough free memory

Don't assume, find out how much you have. Grab a freemem function from somewhere (Adafruit has one) and monitor it. Your extensive String object use is likely the cause and you'll be able to see your heap growing as Strings fragment.

If that's not the issue, I've had trouble in the past with wifi libraries. My ISP occasionally sends me humungous ethernet packets - no idea why. My wifi library had a 400 byte buffer and no bounds checking, which led to daily crashes. I doubt that the ethernet library is so defective, but it's something to check if removing Strings doesn't get your system stable.

And, what are all those relays switching?

TomGeorge:
Hi,
Does the on board LED blink indicating that the temperatures are being read?

Have you checked your code for variables and their values that may try to be bigger than the type they are assigned.
For example int variables can only be -32,768 to 32,767 before over flowing.

Tom... :slight_smile:

I don t think this is the case...I push values in db each 90 seconds and all values looks fine.

Robin2:
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

When you find yourself writing code like this

  pinMode(pinC01, OUTPUT);

pinMode(pinC02, OUTPUT);
  pinMode(pinC03, OUTPUT);
  pinMode(pinC04, OUTPUT);
  pinMode(pinC05, OUTPUT);
  pinMode(pinC06, OUTPUT);
  pinMode(pinC07, OUTPUT);
  pinMode(pinC08, OUTPUT);
  pinMode(pinC09, OUTPUT);
  pinMode(pinC10, OUTPUT);
  pinMode(pinC11, OUTPUT);
  pinMode(pinC12, OUTPUT);
  pinMode(pinC13, OUTPUT);
  pinMode(pinC14, OUTPUT);
  pinMode(pinC15, OUTPUT);
  pinMode(pinC16, OUTPUT);



it is time to learn about arrays. All that could be done in 3 lines of code similar to this


for (byte n = 0; n < numPins; n++) {
  pinMode(arrayOfPinNumbers[n], OUTPUT);
}




...R

I will try this, thank you.

Paul__B:
"Arduino program hang in less than 24 hours"

Automatic answer: You are trying to use a "String" object. :astonished:

Don't!. As per Robin2's reply. :roll_eyes:

will change it, thanks

wildbill:
Don't assume, find out how much you have. Grab a freemem function from somewhere (Adafruit has one) and monitor it. Your extensive String object use is likely the cause and you'll be able to see your heap growing as Strings fragment.

If that's not the issue, I've had trouble in the past with wifi libraries. My ISP occasionally sends me humungous ethernet packets - no idea why. My wifi library had a 400 byte buffer and no bounds checking, which led to daily crashes. I doubt that the ethernet library is so defective, but it's something to check if removing Strings doesn't get your system stable.

Will search for such functions as you suggest. thanks a lot

JCA34F:
And, what are all those relays switching?

There are 15 heating circuits (actuators on 230V and 2W each) and the pump .

Thanks a lot for your suggestions, I will apply them asap.

If there is a function you can put in the code that prints out the amount of free/used memory on a periodic basis, that might get some insight as to memory usage as an issue.

Hi,

Does the on board LED blink indicating that the temperatures are being read?
Tom.... :slight_smile:

Hi,
no, the led not blink every 90 seconds..also i don t have either ping with it ..