Just trying to make sense of things...

RFID Lock Prototype: DHCP Enabling - Follow Up

Sunday, 12 April 2009 22:02 by jordan.terrell

I've got a first pass at a DHCP library that works on the Arduino with an Atmega168 MCU.  It's working and I'm able to get an IP address, and then connect to Google.  It is still really memory intensive - I think I have less than 10 bytes of memory remaining while I'm acquiring the IP address.  I've got some ideas on how to lower the memory requirements, but that will require working with the W5100 IC at a lower level.  That's my next step, but in the meantime check out this video!


RFID Lock Prototype: DHCP Enabling

Sunday, 12 April 2009 12:15 by jordan.terrell

Last time I mentioned my RFID Lock prototype, I said that I was going to work on enabling Ethernet connectivity.  I knew that the Arduino Ethernet hardware and supporting library didn't directly support getting an IP address (and other supporting information) via DHCP.  I began to look around to see if someone had developed a library that would handle the DHCP handshake, and unfortunately I could find no such library - only comments of people wishing someone would develop such a library.  Well, I've started just that - a DHCP library for the Arduino Ethernet shield.

I found an DHCP Application Note on the W5100 (Ethernet IC used on the Ethernet shield) manufacture's web site.  It contained an entire application designed for an Atmel ATMega128 that would acquire an IP address via DHCP.  Although much of the code was written for already, I had to do a LOT of refactoring to get it to work as an Arduino library.  Good news is, I just accomplished my first DHCP handshake and acquired and IP address with it. However, it wasn't easy.  I kept experiencing some really strange behavior - random garbage being sent in the DHCP packets (I used Wireshark to monitor the traffic), the MCU would appear to freeze, and it would restart itself.

Finally, I had a hunch that I might be seeing the results of memory corruption or memory boundary violations - after all, I'm coding in the "Arduino language", which is essentially C++ under the covers.  I found a little code snippet that would check for available memory and found that I didn't have enough memory to allocate the structure I used for sending DHCP packets.  The provided Client class in the Ethernet library apparently takes up a lot of memory and I was allocating it right at the start of the Arduino program; I was using a modified version of the sample program that queries Google for sites containing "Arduino".  Once I removed the code that allocated the Client object, I saw that I had ~630 bytes of memory remaining, and I knew that the DHCP packet takes ~550 bytes of memory.  I ran the program again, and it acquired the IP address successfully.

I'm going to try and refactor a few things so that the DHCP packet memory is only used when it is needed - however, since it takes up more than half of the memory available on an Atmega168 chip, I'm thinking that it will be difficult to support on that chip.  I'm going to have to switch to the Atmega328 chip - TWICE the memory!  I plan to test it on an Arduino Duemilanove (both Atmega168 & Atmega328), and Arduino Mini (Atmega168-20AU), and the Arduino MEGA (although I read something that said the Ethernet shield might not work with the MEGA yet).

Once I get a little more polish on the library, I'll submit a patch to Arduino and publish it myself.  I'll be sure to note any memory issues or other concerns that I come across.