Friday, January 14, 2011

LAN Webserver with Arduino

Just to make things complete, I wanted to show you the alternative to the WiFly shield, the Ethernet shield. It is a heck of a lot easier to work with. There are already some example ethernet sketches available in the Arduino IDE and the board comes preassembled, so you really only have to stack it on top of your Arduino, connect it to your network and define some settings in the code. You could even log some data with the onboard micro SD card slot.

Here is a sample image of the Webserver in action.

The code is pretty much the same as in the example which is provided by the IDE and only differs from my WiFly example in the setup part. You only have to define your network settings accordingly.

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte ip[] = { 127, 0, 0, 1 };

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);

void setup()
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

void loop()
  // listen for incoming clients
  Client client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c =;
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          // print something, in HTML format:
          client.print("LAN Webserver Running!");
          client.println("<br />");
          client.print("Visit ");
          client.print("<a href=\"\"></a>");
          client.print(" for more experiments!");
          client.println("<br />"); 
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
    // give the web browser time to receive the data
    // close the connection:

1 comment:

  1. Hello, It is lawrence who work in WIZnet designed W5100 chip in Ethernet shield. Your projects are so great and useful for many other makers, So, if you do not mind, could I share your projects in out web site?
    Thank you!