DHCP working! (code now attached)
Roy Eltham
Posts: 3,000
DHCP code for the Spinneret Web Server
I've updated this to be more of a module you call from your code instead of being stand alone. Although, I did leave in a commented out section at the top that you can put back in to run it stand alone and test.
Usage info:
1. Call Start
2. Immediately call SetMAC_Address() passing in the address of an array of 6 bytes containing your MAC address from the sticker on the bottom of your Spinneret.
3. Now call DoDHCP(). The first parameter is the socket(0-3), and second is an ID which can be anything you want.
4. If DoDHCP() returns SUCCESS, then you can call the various GetXXX() functions to get the IP, Gateway, SubnetMask, etc.
5. Call Stop
Applied fixes for bugs reported by jstjohnz.
I've removed the file from this message, the latest version is available in the google repository here: http://code.google.com/p/spinneret-web-server/source/browse/trunk/DHCP.spin
I've updated this to be more of a module you call from your code instead of being stand alone. Although, I did leave in a commented out section at the top that you can put back in to run it stand alone and test.
Usage info:
1. Call Start
2. Immediately call SetMAC_Address() passing in the address of an array of 6 bytes containing your MAC address from the sticker on the bottom of your Spinneret.
3. Now call DoDHCP(). The first parameter is the socket(0-3), and second is an ID which can be anything you want.
4. If DoDHCP() returns SUCCESS, then you can call the various GetXXX() functions to get the IP, Gateway, SubnetMask, etc.
5. Call Stop
Applied fixes for bugs reported by jstjohnz.
I've removed the file from this message, the latest version is available in the google repository here: http://code.google.com/p/spinneret-web-server/source/browse/trunk/DHCP.spin
Comments
OBC
If you get Error 2, that is a timeout waiting for the server. The demo code in main just tries once, but normally you would call DoDHCP() a few times if the result is a timeout. I plan to implement the proper timeout/retry protocol from the DHCP spec, but until then what's there will get you going in most cases.
Also, it's up to you to redo the DHCP just before the Lease runs out. I'll be adding a proper Renew function soon, but I plan to leave it up to you to call it at the appropriate time.
Hello Mr.Eltham
'
Thanks for posting the code DHCP.spin
'
I can't seem to find the Object "Brilldea_W5100_Indirect_Driver_Ver006.spin".
'
All I can find is Brilldea_W5100_UDP_Echo_Demo_indirect_Ver005.spin
'
I don't feel too stupid since google can't seem to find it either.
'
Could You post the Driver or a link to it?
'
I look forward to trying out Your code.
'
Thanks again.
Thanks Mr.Eltham
'
They changed the name in the OBEX pointer to "Wiznet W5100 Indirect/Parallel Driver". This is why I couldn't find the file.
'
http://obex.parallax.com/objects/615/
'
Thanks again
'
Your code works great!
'
My router See's the Spinneret with no problems.
'
Here is a screen shot of the PST and the cmd window that I "ping"ed the Spinneret with.
'
You are the Man
'
Many Thanks
PST object
http://obex.parallax.com/objects/457/
Your code works out of the box with Timothy Swieter's driver.
Thank you for providing a starting place with which to start developing for the higher OSI layers.
I would like to concentrate above the application layer on top of HTTP(GET and maybe POST) and URL mapping with the eventual goal of implementing some part of a dynamic application server stack supporting (JSP/Servlet or ASP - both use <% %> html ignored tags) - after I scope out what not to implement.
Any work at the future application server level will build upon all the layers below like your DHCP code, as well as community submitted standard HTTP, TCP/UDP, IP implementations.
thank you
/michael
I noticed in your screenshot image that your MAC is still set to 00:00:00:00:00:00. You should edit the MAC_ADDRESS_# constants at the top of the DHCP.spin file to match your MAC address from the sticker on the bottom your Spinneret.
Also, I plan to keep extending this "lower level" stuff to include better DHCP handling, DNS, basic HTTP support, code to get date and time from time servers to set the RTC, and so on. I need all of these things for my project.
Good one, I was wondering why my MAC was not auto-detecting in SPIN. I should actually read your code.
My MAC address shows now, I guess I can simulate any other mac as well. I'll need to store this in EEPROM or SD.
00:08:dc:16:ef:06 192.168.0.189 SpinServer0 23 Hours 59 Minutes Revoke Reserve
thank you
/michael
Excellent!! Great job!!
It works perfectly here as well.
OBC
So after modifying the pin definitions in your code to match my board, I tried your program. Unfortunately, I still have the same results. About 40% of the time I get correct responses. The rest of the time either the Subnet Mask,Gateway IP, or DNS Server or a combination of all three will be wrong. The IP address is always correct and I have yet to see any issues with any bytes prior to the Magic Cookie. I am beginning to suspect that my W5100 has problems with flaky RAM but I just wanted to see if anybody else has had similar issues. As part of a troubleshooting exercise I might try starting the RAM at a higher location and see if that works.
Regards,
Shawn
*Just for grins, here is a dump from the serial terminal. I did tweak your code to allow the DHCP process to be restarted via serial commands so I could retry if I got bogus results. Here is an example of what I'm seeing. The most common erroneous result is to have the subnet mask read 255.255.0.3 or 255.255.3.4. Incidentally, this response was taken after switching to Socket 1 instead of Socket 0. (Which BTW, to make your code work with any socket, in your DoDHCP procedure you need to change the line that reads the socket status register to:
' get the status of the socket
W5100.readIND((W5100#_S0_SR + (socket * $0100)), @status, 1)
)
Here is the Serial Port Dump from my Prop/WIZ811MJ combo.
DHCP Demo. Press 1 to start DHCP
IP : 192.168.0.104
Subnet Mask: 0.0.66.66
Gateway IP : 192.168.0.1
DNS Server : 0.0.84.4
DHCP Server: 192.168.0.1
Domain Name: KRWSMS
DHCP Demo. Press 1 to start DHCP
IP : 192.168.0.104
Subnet Mask: 255.255.3.4
Gateway IP : 192.168.0.1
DNS Server : 0.0.84.4
DHCP Server: 192.168.0.1
Domain Name: KRWSMS
DHCP Demo. Press 1 to start DHCP
IP : 192.168.0.104
Subnet Mask: 255.255.255.0
Gateway IP : 192.168.0.1
DNS Server : 192.168.0.1
DHCP Server: 192.168.0.1
Domain Name: KRWSMS
DHCP Demo. Press 1 to start DHCP
I have not had any issues with DHCP packets being messed up like you describe. It's could be your W5100 chip, but it could also be your router being flaky? My DHCP code doesn't do any validation of the packet and doesn't retry either, but most things will, so you might not notice DHCP issues with other devices.
Anyway, I will be uploading a new version soon, and it'll have more stuff configurable, like the socket to use. I moved the status reading to a function also.
would you mind posting a link to your code in the Sticky that now resides at the very top of the Spinneret Forum? I'm encouraging everyone to help keep that list up to date.
thanks,
Mark
It's not the router. I have verified the packets being sent to the W5100 from the router are correct via Wireshark. I have tried all four sockets with the same sporadic results. I have started reading both your's and Tim's code trying to understand what is going on and I can't see any obvious issues. I really don't know where to look at this point. I've used Tim's UDP Echo Demo with the UDP Test Tool by Simple Com Tools and have not yet seen a problem. I've even modified the code so that it dumps the entire contents of the received array to the serial port and everything looks ok with it. I'll keep poking around and let you know what I find.
Thanks for the help,
Shawn
Noob here. Please forgive me if I am stating the obvious.
The ethernet controller should verify the CRC as it is being received. If you have a good CRC after completely receiving the frame, you *should* have what the DHCP server sent. I haven't seen the code for the Spinneret yet so I can't look to comment.
Of course, that begs the question: is there any working code that implements a reasonable IP stack up through at least UDP? I am not so worried about DHCP (services should live at static addresses) but would like to get a Spinneret to play with knowing that there is something that works to try out initially.
Brian
Welcome, I myself use reserved static IP's so I am able to use Timothy's TCP Server demo with a couple minor modifications (only the TCP port and IP are required at this point - the MAC address is not required at this layer). There are now ~?10+ developers here with working and more advanced demos involving evolving modules for DHCP, SD I/O, GET URI path/program/querystring_parameter parsing for the upper OSI layers.
As for the TCP layer transport (ports) driver - as an upper layer developer I would expect that the hardware would provide this. However checksum should be checked by one or all of the transport layers above IP - (TCP, UDP and ICMP-(ping) in sequence) - this would be good to verify. This would be either an enhancement request or bug report for this project.
Check the sticky posts for collaborative works in progress.
http://forums.parallax.com/showthread.php?p=955671#post955671
thank you
/michael
Still, this will work for my project -- a ham-radio station controller for stations using software-defined radios. I need a peripheral to control various functions, e.g. antenna switching relays, rotator, amplifier control, external device bandswitching, analog in/out, and BCD in/out.
Been using the BS2 and Propeller in my classes at school (http://www.gbmontessori.com). We have been extremely well supported by Parallax (Andy and Stephanie are wonderful!) and by Hanno Sander (12blocks). Love the chip. The only thing I miss is analog I/O on the chip.
Hmm, I think I feel a GPS-disciplined reference oscillator and NTP server coming on (even NTP broadcast would be great).
Oh! Does the 5100 do IPv6?
Brian
I definitely think it is a hardware problem either with my layout (I am using point-to-point wiring between a WIZ811MJ and the Propeller Proto USB board) or with the W5100 itself because I modified your DHCP driver to use the SPI interface and it now works flawlessly. I'm still not exactly sure what the problem is because I only have issues with reading all three 0xFF's of the subnet mask. I only get two out of the three which throws off the rest packet since there is one byte missing. It could be a problem with a noise spike coupling to the Read line during one of the transitions that tricks the W5100 into thinking a Read has occurred which then causes the read pointer to Auto-Increment. Since I don't have the equipment to diagnose this effectively (I only have a 10MHz single channel O'Scope), I think I am just going to switch to SPI mode until I get to the point where I am confident enough to have a prototype board spun-up.
Shawn
Nice piece of software, and will come in very handy. One issue I was seeing was that sometimes the routine would return with an error timeout well before 10 seconds had elapsed. I think the problem is that in your timeout test code you use ">=" when comparing currentcount and timeoutcount. That is an assignment operator. I think you need to use "=>". At least, it seemed to work for me.
The byte comparison is backwards. Either change [index] to [3-index] or change [DHCP XID+index] to [DHCP XID+3-index].
As written it will only work if the xid is 4 identical bytes..
I am using the Verizon advantec router and under the advanced tab you have IP distribution (the DHCP server) where I can tell it what hostname and IP to assign to what MAC address.
By having a fixed IP you can then find the web server more easily and map it through the router for outside access.
@obrienm
by setting up the network dhcp server to return the fixed address you have better control over the network addresses and once you configure the MAC address in the spinneret you don't need to configure the IP/gateway/netmask/hostname in the hardware so moving to another network is then trivial.