Arduino hangs up after some time

I have an Arduino Mega connected to an aquaponics system monitoring pH, water temp, and a few other sensors. This was working great for months at a time with no problems at all. I then added the capability for it to write to a csv file. No errors for months again. Now, Ive added, using POST or GET commands to write to a MySQL database on my webserver. This works. I can then view the information online. However, after a non-regular amount of time, Something becomes unstable and my database server stops having new information sent to it. I have scoured forums and have driven myself half crazy here trying to get this to work reliably. I travel for work and have until the end of August to try to figure this out… Then I wont be home again for a couple months. Any help would be appreciated. Attached is my entire sketch.

m3allc.com/aquaponics.txt

Without looking at the sketch, it sounds like it runs out of RAM at some point.

I dont discount this theory. However, the entire sketch uses only 29% of the memory. Is there a way to detect this and reset the board?

Post your code (in code tags) and ensure that it has been auto formatted BEFORE posting.

Mark

I did. But the forum limits the amount of characters I can post. This is why I uploaded it as a text file.

Hi,

If your sketch is too big, use REPLY rather than QUICK REPLY and it has an attachment facility, so you can post your sketch as an attachment.

Tom.... :)

Good to know. Thanks TomGeorge

You are using lots of ints where you could use bytes and save half the memory. Anywhere there is no requirement for a number bigger than 255 use a byte.

You are using Strings (capital S) which are unsuitable in the small memory of an Arduino. My guess is they are the main problem. Use strings (small s) which are arrays of char with a 0 terminator.

...R

The string that was created to write to the csv file has been removed since I posted the sketch. I have replaced it with several lines of file.print lines. This is my first sketch so I know there are bound to be some mistakes.. although to this point things have worked. I appreciate the help. If there is anything else, I can make those changes as well.

adamlwvdc36: The string that was created to write to the csv file has been removed since I posted the sketch.

Post the latest version so we are all in step with you. And please make sure you post the new version after this Post. Don't amend the code in an earlier post.

...R

I have attached the updated code. I dont want anyone to actually modify the code for me. But, looking at it and telling me where to look and what to look for and maybe a small explanation of why would allow me to learn better. This is only my first sketch and still have a lot of learning to do. Thank you for all of the help so far.

You still have several Strings (capital S) such as astatus

They are probably what is causing the problem by gradually consuming memory.

...R

Ok. So just change the capital S to a lowercase s? or is there something else? What is the difference?

Example:

String astatus = "";

translates to

string astatus="";

???

Or is there something else that needs to happen.

BTW, I have this topic set to send me an email anytime there is another reply sent. You seem to be the primary responder and for that, Thank You for the help.

Or, should it be declared as a char?

I AM trying to research this at the same time.

char astatus[] = "";

Will this change as the program needs it to change as well? I am totally believing this since these strings were only added for the database part of the code. Which, after implementing, the program started messing up.

"No errors for months again. Now, Ive added, using POST or GET commands to write to a MySQL database on my webserver. This works. I can then view the information online. However, after a non-regular amount of time, Something becomes unstable and my database server stops having new information sent to it."

  1. Have you tried backing out (comment out) the new POST/GET section? Did it start working again?
  2. What is "after a non-regular amount of time"? 3-40 seconds, 1 hour-6 hours etc.?
  3. The database server stops having new information sent to it... Is the arduino still running? Do you have a blinking led to let you know?

adamlwvdc36: Or, should it be declared as a char?

Yes.

Unfortunately it is not a simple change as there are different library functions to manipulate strings rather than Strings. This cstring reference link may be useful.

...R

Non regular time is anywhere between 5 to 9 hours. The arduino acts like it is still working but pH stops printing to the serial monitor. Yes, when this happens SOME functions of the sketch still work. Learning what I've learned it is pretty clear (or at least it's a damn good explanation that makes a lot of sense) it would seem that things that are dependent on strings are the things that are not working after the failure.

Ok, it’s been a few days and I’ve done some tinkering. I have removed the Strings I added right before the system started locking up. I also added a line to serial print the amount of free memory every time the system sends to the server. This is what I know.

The free memory stays at 5860 the entire time.

When the system freezes, the RX LED on the ethernet shield stays on.

I did some researching and it seems that the W5100 can… I guess… timeout? after a while. I have attached a photo. I didn’t happen to catch the RX while it happened to be on. Either it is blinking SO FAST it appears to be steady or it is truly steady. This light is mostly off and of course flashes only during the times it is sending and receiving data.

Any suggestions?

adamlwvdc36: Ok, it's been a few days and I've done some tinkering.

...SNIP....

Any suggestions?

YES - post the latest code. I can't see your PC screen from here. (It should not be necessary to ask this)

...R

OK. I have done a TON of research into this (including reading an ENTIRE 18 page thread in this forum). I am 99% sure I know what the problem is, and have implemented changes to the code to fix the issue. I am currently running (as of 10 minutes ago) a test to see if I can go 24 hours without a hang up again. Robin, you should be able to understand when I say that I believe the issue is caused by the Ethernet shield... W5100 and SD being on the same bus... My code is so cluttered with test code that I have commented out. WHEN this succeeds I will clean up the code to make it look better to me (not the Arduino), and then call it version 4.