Friday, January 14, 2011

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() {
  //exit CMD mode if not already done
  //set into CMD mode

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

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

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

void loop() {

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 =;
        // 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("WiFly 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:
You can see in the video that the WiFly board is blinking when I send a request to its address.


  1. Thanks, man! Was struggling to get the wifly working, and this was the example that I think just set us free!

  2. I'm glad to hear that I could help out :).

  3. Hello! This was really helpful for me. When I try your code, however, I get the "WiFly Webserver Running!" message when I try port 2000, not 80. (Have never been able to get 80 to work). Can you think of any reasons why that could be?

  4. I can only think of your router being the problem. Port 80 is a default port for webservers. Maybe your router has certain filters configured or even blocks the port for certain IPs in your network.

  5. Hello! This was helpful :)
    I need little help with a project...I am trying to do something like a SCADA system, and I am wondering if the WiFI shield can communicate directly with PC wireless module, without using a router wireless... If you can give me some ideas it will be great :)

  6. Hey Mario Bohmer
    Thank you for the tutorial. I am trying to post sensor values(SHT15 temperature and humidity sensor) using Wifly shield, I am struggling. Do you have any tips for me.Please help.

  7. Will this work with Roving Networks RN XV+Wireless SD Shield

    1. I don't have the RN XV module here but according to some other posts I read you can also use the WiFly library to communicate with the module. Since it is controlled via RX TX rather than SPI you will have to change the code though. My code like displayed above will not work.

      You can find an example on how to use it here:

  8. Hi, I have gotten the sketch to compile and currently the WiFly module is showing a slow blink on its green LED. However, I am confused on what is supposed to happen next. Nothing appears in the serial monitor and when I try to enter the ip address of the WiFly module that I had set in command mode (""), the browser just keeps trying to load it, but to no avail. Any help would be appreciated, thank you.

  9. Nevermind, it is working now, for some reason, the sketch would not work with google chrome

  10. Hello,

    I am using a WiFly 2.21 RN-131C shield with an Arduino UNO board. I tried to run your program but it seems that Client client = server.available(); is not recognized. When i compile, here is what i get: cannot allocate an object of abstract type 'Client'. I don't know if it has something to do with my libraries but i have the WiFly.h library on my Arduino. Can you please help me in this matter asap? Thanks in advance

    1. Try changing it to WiFlyClient and WiFlyServer

  11. Hi,

    I use the user manual for the Wifly, I am not using arduino,
    But I like this example.
    However it uses arduino libraries that make no sense.
    I cant see what Client.stop() does.
    Does it enter command mode $$$ then send "exit".
    How can the exit command be sent to the wifly without first entering command mode ?
    I think client.stop() does nothing,
    please explain

  12. Sketch Arduino WiFi for Wifly modem configuration without library and
    with front-end:

  13. I learn some new stuff from it too, thanks for sharing your information.