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);
  server.begin();
}

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 = client.read();
        // 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");
          client.println();
          // print something, in HTML format:
          client.print("LAN Webserver Running!");
          client.println("<br />");
          client.print("Visit ");
          client.print("<a href=\"http://marioboehmer.blogspot.com\">marioboehmer.blogspot.com</a>");
          client.print(" for more experiments!");
          client.println("<br />"); 
          break;
        }
        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
    delay(1);
    // close the connection:
    client.stop();
  }
}

WiFi Webserver with Arduino

Today I want to show you, how to use your Arduino as a webserver or how to transmit data wirelessly. I used the WiFly shield from sparkfun. The only thing I had to do was to solder on some connector pins, so I could stack the module on the Arduino. The module itself works out of the box. You only need some configurations for your network connection. The first thing to do is to connect the board with your Arduino and run a first example sketch to see if works properly. You can find an excellent tutorial for some basic first steps here.

After uploading the first example sketch to the Arduino, open the Serial Monitor and explore some of the commands from the datasheet. One of the most important things you might want to know is the MAC address of your WiFly module. After you switched to the command mode, just enter "get mac". Now you can configure your router to assign a DHCP address for example. For details about all the commands and how the module works look into the datasheet.

One thing to remember is to introduce the module to your network at startup time. So in your setup method you have to send all the necessary information like passphrase, listenport and stuff like that to your module. To communicate easily with the module, I used a high level WiFly Shield library found here.

Source (just insert your configurations where marked with <insert> :
#include "WiFly.h"

Server server(80);

void setup() {
  Serial.begin(9600);
  SpiSerial.begin();
 
  //exit CMD mode if not already done
  SpiSerial.println("");
  SpiSerial.println("exit");
  delay(1000);
 
  //set into CMD mode
  SpiSerial.print("$$$");
  delay(1000);

  //set authorization level
  SpiSerial.println("set w a <insert>");
  delay(1000);

  //set passphrase
  SpiSerial.println("set w p <insert>");
  delay(1000);
 
  //set localport
  SpiSerial.println("set i l 80");
  delay(1000);
 
  //disable *HELLO* default message on connect
  SpiSerial.println("set comm remote 0");
  delay(1000);

  //join wifi network <ssid>
  SpiSerial.println("join <insert>"); 
  delay(5000);
 
  //exit CMD mode
  SpiSerial.println("exit"); 
  delay(1000);  
}


void loop() {
  listenForClients();
}

void listenForClients() {
  // 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 = client.read();
        // 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");
          client.println();
          // print something, in HTML format:
          client.print("WiFly Webserver Running!");
          client.println("<br />");
          client.print("Visit ");
          client.print("<a href=\"http://marioboehmer.blogspot.com\">marioboehmer.blogspot.com</a>");
          client.print(" for more experiments!");
          client.println("<br />"); 
          break;
        }
        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
    delay(1);
    // close the connection:
    client.stop();
  }
}
You can see in the video that the WiFly board is blinking when I send a request to its address.

video