It’s been a while since I had the idea of an Arduino receiving and sending data trough the Internet. Long time ago I picked up a simple RGB led, an arduino Ethernet shield, put some pieces of code together and I was able to receive information from a website and convert into light and colour.
I built a custom XML parser for Arduino ethernet and some simple functions for changing the color and fade the leds. Just enough freedom to decide what information gather and how to show it. Colors, blinks, fades. All I had was just an RGB led, not even a button. So the interface was pretty simple.
My first experiment was to connect Arduino ethernet to the database of my webshop (Doctor Bit) and show information in real time on how many new orders we have. All this is accomplished part in Arduino and another part with a small PHP script on a remote server. Arduino triggers the script on the server and the script gathers and returns all the info we want from the database, wrapping it up with nice XML tags.
Arduino finds these tags in the web response and displays the information gathered in a form of light/colour pulses that I can can understand.
This light codes are pretty straight forward. From the beginning… red indicates device is starting. A nice colour fade trough all possible colour mixes indicates that we are discovering DHCP servers and asking for an IP. When connected we get a blue colour, and when we receive the information from the website we get green which is the indicator that everything went ok. After green we will get blue or led pulses if a client placed a new order. Blue if the order has been paid, red if has been not. A green colour back indicates all orders have been cleared.
With this device we can catch data from any other website or network (trough XML).And it is pretty easy to adapt the code to whatever our function is.
After some days of thoroughly testing the device I dicovered that at a certain point (2-3days) the device would stuck and stop refreshing the information from the website. Only a manual reset would fix it. I did try some variations in the code, looked in internet for possible bugs, but besides a problem with the hardware the reset in version 1 of the Ethernet shield I couldn’t find any clue on what was happening inside Arduino.
That was probably 1 year ago, I left the project on hold because I couldn’t figure out what was the problem. Since then I’v been working in many other projects. Some really simple and others more complex. Recently, one of this projects that I’ve already commented in this blog, had to use an Ethernet shield to print a document trough a java web server in a host computer so I went back to this project to rescue some pieces of code. And of course I couldn’t resist to give it another try and see if finally could have the problem solved.
I decided to change the way the code would get an IP from a DHCP server and the way of creating connections. I’m using a DHCP library from www.gkaindl.com. In the upcoming version of Arduino IDE, the UNO version some sort of previous adaptation of this code is already included. Haven’t tried yet but for now the libray from gkaindl looks really good.
My major change on the code was to go from a blocking routine to a non-blocking routine. So I can always monitor what was going on in there. Some how I always had the idea that the problem was due to a difficulties obtaining a new lease from the DHCP server. Now, with this modifications in the code, I finally see trough serial all what Arduino ethernet is doing and hoping to determine what was the real problem.
It turn out that when doing this modifications I had to change also the way of refreshing all data. This particular piece of code was based on a simple algorithm using millis() in order to determine time lapses. Reading a little bit more about millis() I found that it should be able to return a full unsigned long variable before it would overflow and start over again from 0. I obseved that instead of an unsigned long variable to contain the millis() reference I was using a signed long. Would that be the problem? Let’s check doing a little bit of maths:
If the greater number we can have in an unsigned long variable is : 4,294,967,295 according to the C standard.
We transform this into milliseconds and gives us (in days) approx: 4,294,967,295 / 1000 / 60 / 60 /24 = 49,71 days witch is approximately what the Arduino millis() documentation tells us that an overflow will happens.
Doing the maths for the greater number of a signed long : 2,147,483,647 / 1000 / 60 / 60 / 24 = 24.8 days. It’s obviously half of the unsigned variable, as one bit is just used to mark negative and positive values. So that wasn’t the source of my problem. As my code would never pass 2 – 3 days of running time.
After this two changes and without knowing why my code has been running for more than 3 days, and so far so good. I will be monitoring closely and post any other observation I found along the way. Most likely was something referring the DHCP library.