Just trying to make sense of things...

Arduino DHCP Library: Version 0.3

Saturday, 25 April 2009 08:45 by jordan.terrell

I just finished implementing some improvements to the Arduino DHCP Library as suggested by members of the Arduino forums.  Special thanks to Arduino forum members Nebster, smartperson, and thomsen for their feedback and suggestions.

      Download here: Arduino DHCP Library v0.3
      Previous Versions: v0.2, v0.1

Additional instructions can be found on the original DHCP library post.

Here is a list of the improvements in the v0.3 release:

  • Transaction ID is correctly incremented on DHCP discovery retries
  • Elapsed Seconds is now calculated and transmitted, instead of always being zero
  • You can now retrieve the following data elements:
    • Local MAC Address
    • Local IP Address
    • Subnet Mask
    • Gateway IP Address
    • DHCP Server IP Address
    • DNS Server IP Address
  • The example Arduino sketch has been updated to show:
    • How to determine if an IP address was successfully acquired
    • How to retrieve data elements acquired from the DHCP server (e.g. Local IP Address)

Arduino DHCP Library: Version 0.2

Wednesday, 22 April 2009 22:19 by jordan.terrell

I just finished adding retry capability to the DHCP library.  It will, by default, resend DHCP requests every 2 seconds, and timeout after 60 seconds.  I've also removed the need to declare a Dhcp variable. Take a look at the included example to see how to use the new library.

       Download here: Arduino DHCP Library v0.2

I hope this release solves the outstanding issues I know about.  I'm also looking for feedback on additional enhancements - ARP conflict detection, lease renewal, etc.  If you know of something that doesn't work or you would like to see added, please let me know.


RFID Lock Prototype: DHCP Library v0.1

Sunday, 19 April 2009 22:11 by jordan.terrell

Update: This has been blogged about on the Makezine Blog – excellent!  Thanks to John Park for making this happen.

Well, I now have a version of the Arduino DHCP library that I feel moderately comfortable releasing to the wild. If you're interested in the background story, check out these posts.

       Download here: Arduino DHCP Library v0.1

I've tested it with an Arduino Duemilanove (ATMega168), but I'm positive that there will be bugs in the code and that for some (hopefully not many) people it will just plain not work.  If you do try to use it and find that it doesn't work, try using Wireshark to see what packets (if any) are flying across the wire.  Even if you can't figure it out, sending Wireshark capture logs to me will be helpful for me to debug.

In order to use the library, you need to drop the contents of the zip file into your "Arduino\hardware\libraries\Ethernet" folder.  There is a sample in the zip file called "WebClientWithDHCP" that is a modification of the standard Ethernet "WebClient" example.  There are the basic steps to get it working.

1) Add "#include "Dhcp.h" to the beginning of your Arduino sketch
2) Declare a variable of type Dhcp (e.g. "Dhcp dhcp;")
3) Call the beginWithDHCP() method, while providing it a MAC address

It has been a challenge writing this library, and I've just scratched the surface.  I don't have IP lease renewal, retry, cable detection, or even much error handling/reporting.  I'm just interested in getting feedback from the Arduino community to see if they find this useful and where to take the library next.


RFID Lock Prototype: DHCP Enabling - Follow Up #2

Wednesday, 15 April 2009 09:35 by jordan.terrell

Continuing on my work to build a DHCP client for my RFID Lock Prototype, I’ve read through the datasheet for the W5100 Ethernet chip (in use on the Arduino Ethernet shield) and I’ve studied the source code for the existing Arduino Ethernet library, and now I know how to work more directly with the transmit and receive buffers (16K shared across 4 sockets) on the W5100 chip.  Hopefully over the next few days I can carve out some time to refactor the DHCP library I’ve built so far to make use of this technique.  Once I do, I will release it publicly – hopefully I will find some willing beta testers.  If all goes well, perhaps it might be included in a future Arduino software release!

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.