E10 and strange problems

The EtherTen combines an Uno-equivalent Arduino-compatible board and Wiznet-based Ethernet support, along with a microSD card slot and Power-over-Ethernet support. [Product page]
Post Reply
JollyJohn
Posts: 10
Joined: Sun Dec 04, 2011 6:00 am

E10 and strange problems

Post by JollyJohn » Mon Dec 19, 2011 10:52 am

Hi All,
I'm having a problem I can't get my head around and I'm hoping someone has some advice. I have a sketch for a solar pool heater controllers I'm building and I've hit a weird problem. I've isolated it down to a Serial.print statement which is in the following code (I'm testing the pachube strings):

// send the HTTP PUT request.
Serial.print("PUT /api/41617.csv HTTP/1.0\n");

Serial.print("Host: www.pachube.com\n"); <=== Troublesome code

If I comments out the second Serial.print all works. If I uncomment it the system crashes in a totally unrelated piece of code - but always at the same spot.

I'm using Arduino IDE v1.0 and the sketch size that works is 25620 and that doesn't work is 25654 bytes. Have I hit a limit of some sort? Is there a bug in the compiler? Have I done something stupid that I can't see.

BTW - If I uncomment the second line, but then comment out the first line it still work! One or the other but not both. I've tried putting delays between to allow the buffer to drain but to no avail.

Any ideas?

Thanks,
John

User avatar
jonoxer
Freetronics Staff
Freetronics Staff
Posts: 298
Joined: Sat Oct 15, 2011 11:31 am
Location: Melbourne, Australia
Contact:

Re: E10 and strange problems

Post by jonoxer » Wed Dec 28, 2011 4:22 am

Perhaps you've hit a RAM limit? The ATmega328 only has 2K of RAM, and every time you define a string like that it uses up some of the precious RAM space. If there are many other Serial.print commands elsewhere in the sketch you'll hit that 2K limit very quickly. Combining that with Ethernet (which uses gobs of RAM for things like the TX and RX buffers) and it can become tricky to store everything you need in RAM.

In Arduino v1.0 there were some convenience features added to make it easy to store unchanging strings like that in flash memory instead, freeing up some RAM. Have a look at the F() function, which takes care of transparently storing the string in flash and then fetching it again when required. There's mention of it here:

http://www.arduino.cc/en/Serial/Print
--
Jon

JollyJohn
Posts: 10
Joined: Sun Dec 04, 2011 6:00 am

Re: E10 and strange problems

Post by JollyJohn » Fri Dec 30, 2011 10:02 am

And the man wins the prize!!

Thanks Jon, exactly right. I stripped all the diagnostic and warning messages out and everything came good. I hadn't found the F() function, but now I can put them back in. What is the easiest way to get an estimate of the RAM that's going to be consumed?

I'm happy with the Pachube reporting. My family now gets a twitter message from the house when the pool is warm enough to swim in :D

User avatar
jonoxer
Freetronics Staff
Freetronics Staff
Posts: 298
Joined: Sat Oct 15, 2011 11:31 am
Location: Melbourne, Australia
Contact:

Re: E10 and strange problems

Post by jonoxer » Sun Jan 01, 2012 11:01 am

Reporting RAM usage on small micros can be a pain, because it's easy to use up memory just by trying to read it! One of those situations where measuring something changes it.

I did a talk about Arduino memory usage / architecture a couple of years ago at the very first Arduino Miniconf, so perhaps the slides will be of some use. Slides from my talks don't generally make a lot of sense on their own because I use them more as visual props than as reference material, but near the end is some example code for sending RAM usage out the serial port.

http://jon.oxer.com.au/talks/id/120
--
Jon

EpicTinker
Posts: 9
Joined: Thu Nov 03, 2011 4:36 pm

Re: E10 and strange problems

Post by EpicTinker » Mon Jan 02, 2012 6:54 am

Besides Jon's informative slides, you may want to check this link out, which may give you some additional info on how to best optimize your code to fit in the limited RAM available. I happened to run into this issue a few days ago, too :)

http://www.designer2k2.at/home/arduino/ ... rflow.html

JollyJohn
Posts: 10
Joined: Sun Dec 04, 2011 6:00 am

Re: E10 and strange problems

Post by JollyJohn » Thu Jan 05, 2012 11:22 am

Thanks guys, much appreciated. I'm onto my second project now with many more planned, so I'm bound to strike this agin (and again, and again ..... :-) ) The references will be very useful.

Post Reply