Thursday 17 January 2013

Arduino Web Server with ENC28J60 + JY-MCU AVR

It took some time and several attempts to find an Ethernet library for ENC28J60 that works on the JY-MCU AVR board using ATmega32.

Hurray, my web server is responding!


The formal Arduino Ethernet Shield and the library to use it are built around the WIZnet W5100 Ethernet controller. The main advantage of the WIZnet W5100 over the Microchip ENC28J60 is the built in TCP/IP stack, that it, it has built in support for 4 sockets and protocols like TCP, UDP, ICMP, IPv4 ARP. While the ENC28J60 only support the physical and link (MAC) layers and the rest need to be implemented in software.

I first started with this library: https://github.com/turicas/Ethernet_ENC28J60
This library goal is to expose the same API as the standard Ethernet library that work with the W5100 and allow developers to choose between W5100 and ENC28J60 without any change to their sketch/code.
After trying the web server example and adding some debug code, I took a deeper look into the library code   to asses the list of limitations it has and how this affect the functionality.
I believe the main challenge for such library is to manage the 4 sockets and their data buffers with the limited memory available on the ATmega32 (2KB) compared to the 8KB buffer on the Ethernet chip.
Even if we split the Ethernet buffer in half (4KB RX + 4KB TX), we might get 4 different 1K packets, one for each socket, and we have no way to store them all in memory for processing. I think this is why the library is limited to one socket at a time, and in that case, what is the point in that library.

I then switched to a different library - EtherShild that is used by Nuelectronics, Andy's Life, and others.
Apparently, I was not the only one that went this road: http://arduino.cc/forum/index.php/topic,22635.0.html
At least now I have a library that works.

To make it work, I had to modify it a bit to use the SPI pin definitions from the pins_arduino.h file so the IDE will take the variant I wrote for the JY-MCU board. For some reason, both libraries assume only the Arduino standard boards instead of using the definitions in the pins_arduino.h or the just use the SPI library.

This library even comes with example to take temperature measurement from a 1-wire DS18B20 digital thermometer and show it on a web page.
I gave it a go, but had to modify it a bit to make it work with DS18S20 that use parasite power and provide readings differently:

  1. Add a strong pull-up  for 750ms between the convert temp instruction to the read instruction by setting the pin to output/high for 750ms.
  2. Change the conversion of the read to match the device resolution.


However, this sample can't handle more than one device on the bus and perform the reading during the respond to the page request and it takes about 750ms to return the page.

Next: use a proper one-wire library that can handle multiple devices on the bus and perform the reading asynchronously to web page requests.

Update: later on, when I continued my 1-wire temperature server development, I switched to another Ethernet library - EtherCard. It has a better API and support DHCP, NTP, UDP, as well as TCP client and server.

1 comment:

  1. Just found your work, thanks, I assume this will be a huge help since I too bought the JY-MCU shield (c:

    BR
    Andy

    ReplyDelete