Arduino based Webserver (ATMEGA328 + ENC28j60)

Updated on 09-01-2017 to correct issue with MAC address: (Identifed by Julien as per in the comment section)

Replaced : static byte mymac[] = {0xDD,0xDD,0xDD,0x00,0x00,0x01}; with new value.

As I built a Arduino Pro clone based development board running with 3.3V, I thought to give it a try with my Ethernet module for testing. This is an example for 3.3V LVTTL system. If you use this ENC28J60 on a 5V system,you may have to use logic level convertors.
Ethernet module:
Based on Microchip’s ENC28J60 chip. I’m using a board made by Sure electronics (really good manufacture) called MB-CM14111. I got this 2 years back from ebay for $10. You can now find cheap boards on ebay and do an ebay search as “enc28j60”. ENC28J60 is a SPI device and needed to be connected to ATMEGA328’s SPI module.
Arduino Board:
I’m using my Techduino Pro board which I build on an earlier post along with the my modified CP2102 USB to UART board.
Connections:
I connected the two boards as follows to establish a SPI link between two;
Ethernet Module Techduino
3.3V 3.3V
GND GND
DI Pin 11
DO Pin 12
CLK Pin 13
CS Pin 10
DSCF5688_640x480
DSCF5693_480x640
DSCF5697_640x480
Software Library:
I used EtherCard Arduino library from JCW’s GIT repository. See here for downloads and installation instructions. You just have to download the zip file and extract it to your Arduino’s library folder(in my case arduino-1.0.3\libraries) and rename it to EtherCard.
Code:
I used a very basic code just to serve a static page from the webserver. You can find many more detailed information on using this library from LUCA’s ENC28J60 tutorials.This is the code I used. You just have to copy this code to Arduino IDE and compile it and upload.Modify myip[] ass you wish.This is the device’s IP address.The library also supports DHCP but here I’m using a static IP for the simplicity.I also omitted the standard HTTP requests/replies on this code for the simplicity.
#include <EtherCard.h>
static byte mymac[] = {0xDE,0xDD,0xDD,0x00,0x00,0x01};
static byte myip[] = {192,168,1,10};
byte Ethernet::buffer[700];
 
void setup () {
  ether.begin(sizeof Ethernet::buffer, mymac,10);
  ether.staticSetup(myip);
}
 
void loop() {
 
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);
 
  if(pos) {   
    
    BufferFiller bfill = ether.tcpOffset();
    bfill.emit_p(PSTR("<h1>hello</h1>"));
    ether.httpServerReply(bfill.position());
  }
}
Testing:
I connected the Ethernet module to my gigabit Ethernet card using a straight cable (my card auto detects straight or crossover connection).I set my PC’s address to be 192.168.1.1 manually. Then I tried a ping test to the Ethernet module by  “ping 192.168.1.10” on command prompt. It was a success in first round.
Ping
Then I opened my browser and visited http://192.168.1.10 , TaDa!!!! The web page is working Open-mouthed smile
Web
I did a simple packet tracking using WireShark too. Just to see what’s happening there. You can see how  the ping requests and replies going through the network.













Wireshark 



Share on Google Plus
    Blogger Comment

12 comments:

  1. could you tell me how to handle client requests ??

    ReplyDelete
    Replies
    1. here it's just checking for any request and send the same file.you need to check incoming http header to handle client requests

      Delete
  2. Hi buddika, can we replace Pin 13 with another pin.

    ReplyDelete
    Replies
    1. We can't do that as we use Hardware SPI in here. But you can share CLK of SPI with any number of SPI devices you use :)

      Delete
  3. Hi Buddika,thank you reply.
    buddi how we send to data to c# tcp from this?

    ReplyDelete
  4. Maybe a silly question but could you plug it into a spare port on a router or switch and have it work the same way?

    ReplyDelete
  5. Your code looks good! Ping and web server can be tested!

    But is there a way to know if you can use a patch cable from the pc to the enc28j60, or if a crossover cable is needed?

    I have an enc board that has header pins for 5v and 3.3v.. it needs 2 power supplies?! it doesn't say but I assume the spi signals from the arduino can be 3.3v The reset line is unconnected but maybe it should be pulled high?

    The reason I ask so many dumb questions is that I can't seem to get it to work! Do you know what SPI clock speed and mode it's supposed to be?

    ReplyDelete
  6. Hi!
    Thanks for this tutorial, was the most straight-forward to connecting my arduino for a prototype of a project i have in mind.

    I'd like to point a small error in the code about the MAC address:
    At first it worked out of the box while connecting it directly to my laptop ethernet, but couldn't even ping when i plugged it into a Netgear DGS105 switch (5port gigabit), still the connect LED of the module was on and traffic light was blinking (albeit not in rythm with the pings) so PHY link was on and seeing (at least some broadcast) traffic.

    After various hardware swaps which solved nothing, i had a doubt about the MAC address and read the Wikipedia article which mentions two bits on the first byte of the MAC for unicast/multicast and global/local.

    You're using 0xDD, which is 11011101, so global(manufacturer hardware ID) and multicast.

    I suspect this SOHO switch might be filtering out the multicast MAC so i changed it to 11011110 (local and unicast):
    static byte mymac[] = {0xDE,0xDD,0xDD,0x00,0x00,0x01};
    And then it worked.

    By the way i also put the "local"(software defined) flag so the MAC couldn't conflict with some manufacturer range.

    ReplyDelete
    Replies
    1. Hi Julien,

      Thanks for the valuable information, I haven't used this setup on a router or a switch, I just used point to point connection with PC. I will add your information to the post also

      ~Buddika

      Delete