Monitor your home remotely using the Arduino WiFi Shield

hardware_configA few days ago, I posted an article about the Arduino WiFi shield, and I promised I will make another article showing a practical application, and of course using open-source software & hardware. So here it is ! In this article, I will show you how to monitor some data in your home using precisely this Arduino WiFi shield. Along with the Arduino Uno board, the final system will form an autonomous solution to monitor one or several sensors in your home. You could for example be interested in monitoring some contact sensors that are installed on your doors, and you want to know if the door has been opened or not. To emulate one of these sensors, I will simply use a small push button in this tutorial, but the principle is exactly the same.

Hardware requirements

The first thing you will need is an Arduino board. As usual, I used the Arduino UNO board for this tutorial, but any other official Arduino board should be fine as well. The second most important element in this tutorial is the Arduino WiFi shield. You will also need a small push button to emulate the sensor you want to measure, and a 10K ohms resistor.

Because we are using the WiFi shield, you will also need to have a wireless network running in your home, with WEP or WPA2 personal authentification. Don’t worry, I will show you how to do it in both cases.

Software requirements

Appart from the latest version of the Arduino IDE, you don’t need much software for this tutorial.

Hardware configuration

The hardware configuration is pretty simple. First, you have to plug the WiFi shield into your Arduino board. Then, you have to connect the resistor and the push button to the Arduino shield using a pull-down configuration: this means that if the button is not pressed, the input will be “pulled” to the ground. At the end, the button will be connected on pin number 2. Why number 2 ? This is because this is an interrupt pin, we will why we need that later. This is the schematic of this configuration:

hardware_config_bb

Without this, the input could actually have a random state and we would trigger a “door has been opened” action even if nothing actually happened. The result should look like this:

hardware_config

Testing individual parts

Let’s now really dive into this project and test every part separately. First, let’s see if your Arduino board can connect to your local wireless network. For this, it is really easy, you can just use the official Arduino sketch for WPA networks which you can find here. If you have a network with WEP protection, you can find the corresponding tutorial here. Just modify your wireless network name, set the right password, and then upload the sketch to the shield with the Arduino IDE. Open the serial monitor, and you should see this:

Screen Shot 2013-03-15 at 9.59.28 PM

See this IP address ? This is the address of your shield. For now, just write it down, we’ll use it later. By the way, the “link” LED should turn green when the shield is connected. Sometimes it takes a while, so be patient :)

Finally, let’s test the push button. We want to detect the state of the button, so we will use the function digitalRead(). Here is the sketch to test the button:

Now upload the sketch, and open the serial monitor. You should see a lot of zeros, which is logical because the pin number 2 is connected to the ground. Then, just push the button, and if you see ones like on the following screenshot, it is all good.

Screen Shot 2013-03-15 at 10.01.36 PM

Putting it all together

It’s now time to combine everything we’ve learned so far, and read out remotely the value of of this push button. Actually, we will do something slightly different: we will check if the button changed it’s state (which mean for example that the front door of your flat has been opened), and put a message on a server if this happened. And the server of course, it will be the Arduino board with the WiFi shield.

First of all, we need to check if the button changed state. We could do as we did before, and use the digitalRead() function. But there is a problem with that function: what we will do is check the status of the button, which is fast, and then if something connects to the server running on our Arduino board, it will answer to this connection and reply with the state of the button. Sounds good to you ? Well … imagine now if the door is actually opened and closed again during the time the board is answering to the incoming connection: the board will completely miss it, and a thief for example is entering your home without you knowing it !

That’s why I spoke about using pin 2 before: it is actually one of the interrupt pin of the Arduino board. What this means is that on this pin, you can attach an interrupt with the attachInterrupt() function. This will call a special function if at any time the state of this pin change, in our case going from low to high:

Here, the function setDoorStatus will set a variable to indicate that the door has been opened. That  was for the sensor part, now we have to actually make our board answer with the status of the sensor if something (in this case, your computer) access the board. After connecting the shield to the wireless network, the next step is to see if there is some incoming connection. This is done by doing:

If this is the case, we start building a very simple HTML page by calling the println() function of client:

Then, we check the status of the variable door_opened, and print some information accordingly:

Finally, we finish the HTML page with:

And then we stop the connection:

These are the important parts of the code, as usual you can find the full code in our GitHub repository. Now, just upload the final code into the board, and wait until the “link” LED turns green on the board. Now, remember when I told you to write down the shield’s IP address ? This is where you will need it. Just open your favorite browser, and type down this IP into the URL field. This is what you should see:

Screen Shot 2013-03-15 at 10.10.00 PM

Now, press the button, and return to the page. The message should have changed to this:

Screen Shot 2013-03-15 at 10.10.45 PM

Which means our systems works ! You can now monitor what’s going on in your home directly from your computer. Of course, there would be some more adjustments to do so you can monitor it from anywhere in the world (right now it is limited to your own network), but you get the idea.

You can also find the corresponding video on Youtube:

Here is a summary of all the components you will need for this project:

Have fun with this projects, as always, don’t hesitate to comment, modify it, and even more important share !

Receive the latest news about home automation

Over 1000 home automation enthusiasts already joined our list !

Do you want to receive the latest news about open-source hardware and home automation? Then join our mailing list today by entering your email below. It is 100% free and you can unsubscribe at any time if you're not satisfied.

  • Pingback: London Arduino #3 | Londuino

  • Frans van Enschot

    Hi Marco,
    i have used your program, and it workt.
    but i have still a few questions.
    when i load the program below then it works only a short time, and also the lines somtimes missing characters.
    what do i wrong?
    hopefully can you help me.

    grtz frans

    /*
    WiFi Web Server

    A simple web server that shows the value of the analog input pins.
    using a WiFi shield.

    This example is written for a network using WPA encryption. For
    WEP or WPA, change the Wifi.begin() call accordingly.

    Circuit:
    * WiFi shield attached
    * Analog inputs attached to pins A0 through A5 (optional)

    created 13 July 2010
    by dlf (Metodo2 srl)
    modified 31 May 2012
    by Tom Igoe

    */

    #include
    #include

    char ssid[] = “Ziggo446F8″; // your network SSID (name)
    char pass[] = “KCk7KuczEW76″; // your network password
    int keyIndex = 0; // your network key Index number (needed only for WEP)

    int status = WL_IDLE_STATUS;

    WiFiServer server(80);

    void setup() {
    //Initialize serial and wait for port to open:
    Serial.begin(9600);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
    }

    // check for the presence of the shield:
    if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println(“WiFi shield not present”);
    // don’t continue:
    while(true);
    }

    // attempt to connect to Wifi network:
    while ( status != WL_CONNECTED) {
    Serial.print(“Attempting to connect to SSID: “);
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
    }
    server.begin();
    // you’re connected now, so print out the status:
    printWifiStatus();
    }

    void loop() {
    // listen for incoming clients
    WiFiClient client = server.available();
    if (client) {
    Serial.println(“new client”);
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
    if (client.available()) {
    char c = client.read();
    Serial.write(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”);
    client.println(“Connection: close”); // the connection will be closed after completion of the response
    client.println(“Refresh: 5″); // refresh the page automatically every 5 sec
    client.println();
    client.println(“”);
    client.println(“”);
    // output the value of each analog input pin
    for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
    int sensorReading = analogRead(analogChannel);
    client.print("analog input ");
    client.print(analogChannel);
    client.print(" is ");
    client.print(sensorReading);
    client.println("”);
    }
    client.println(“”);
    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(1000);

    // close the connection:
    client.stop();
    Serial.println(“client disonnected”);
    }
    }

    void printWifiStatus() {
    // print the SSID of the network you’re attached to:
    Serial.print(“SSID: “);
    Serial.println(WiFi.SSID());

    // print your WiFi shield’s IP address:
    IPAddress ip = WiFi.localIP();
    Serial.print(“IP Address: “);
    Serial.println(ip);

    // print the received signal strength:
    long rssi = WiFi.RSSI();
    Serial.print(“signal strength (RSSI):”);
    Serial.print(rssi);
    Serial.println(” dBm”);
    }

    • Marco Schwartz

      Hi Frans, thanks for using my code ! I don’t know for sure if this is the reason, but it seems you don’t create the right HTML tags when the server answer, like :

      client.println(“< !DOCTYPE HTML>“);
      client.println(““);
      client.println(““);

      and :

      client.println(“
      “);
      client.println(““);

      Try by really taking the original code and just changing the part related to the printing of the analog inputs values (and the WiFi SSID/pass of course)

      Let me know if you need additional help !
      Marco

  • http://puiucristian.com Puiu

    Hey,

    Do you know if the wifi shield can act as a client and server at the same time?

    I mean that it listen for new clients in the loop, but if something triggers it (like the door being open) it opens a webpage to send some data .

  • Hoyt

    Hi,
    can you suggest a way to monitor your home from your office instead of monitor your home within your home?
    Thanks,
    Hoyt

    • http://www.marcoschwartz.com Marco Schwartz

      Hi Hoyt, thanks for your comment. This will be the subject of an upcoming article, as there are some tweaks to do on the router in your home. I will keep you updated !

      • Husni

        Great work and thanks for the info.. I’m also interested in how to do all that outside my home. I would greatly appreciate if you can keep me updated as well.. great work!!

  • http://www.sequoia.fi/blog/2012/06/04/sequoia-and-tecnotree cosmetic surgery prices abroad

    Hello! I just would like to give an enormous thumbs up for the great info you might have here on this post.

    I will likely be coming again to your blog for extra soon.

    • http://www.marcoschwartz.com Marco Schwartz

      Thanks for your comment Oliver ! More articles related to connected open home automation systems are coming !

  • edwards

    I am having trouble with the html part i think everything works up until i try to open my ip address in the web browser. The search doesn’t find the page. The board is connected to my network, I confirmed this by pinging it. I’m not sure how to fix this. Any help is appreciated.
    Thank You

    • http://www.marcoschwartz.com Marco Schwartz

      Hi Edwards,

      I think you configured your Arduino board correctly (as the ping works), check in the preferences of your router if the port of the Arduino WiFi server (80) is not blocked for the IP of the Arduino Wifi shield.

      • edwards

        I can’t access the preferences, I don’t know the password and my boss that does is out of town. Couldn’t I just change the port and fix the problem if port 80 is blocked?

      • pxj

        I am also having the same problem.. I couldn’t find anything like you have mentioned above.

        • http://www.marcoschwartz.com Marco Schwartz

          It mainly depends on your router (and unfortunately there are nearly as many router interfaces as router models …) but if you are having problems to access your system from the outside world you can definitely check out http://www.yaler.org/ that might solve your problem.

      • Alessandro

        I have the same problem and both my arduino and browser are within the same network….anyone has a solution for this problem?

  • http://yusufokto.com yusuf

    hi marco,
    your code inspire my project :) , can wifi shield transfer file .jpg from sdcard arduino to leptop via wifi? are you have some sketch like my project?

    thanks for advice :)

  • Lucas

    Hey Marco

    First of all, thanks for the tut.
    I have a request. I wanner do something similer for my project. Its very simple. I wanner be able to connect to a webserver with the wifi shield, and send 0 & 1 bits. I dont need all the hardware and so on.
    What should i do??

    • http://www.marcoschwartz.com Marco Schwartz

      Hey Lucas,

      You can just use HTTP to send 0 & 1’s to a distant webserver. You can have a look at the official WiFi client tutorial of Arduino:

      http://arduino.cc/de/Reference/WiFiClient

  • Arne

    I just would like to thank you for your code and video. It was really helpfull for my project :)

    Arne

  • Pingback: Monitor your home remotely using the Arduino WiFi Shield - Arduino collector blog

  • Alex

    Hello i used your code and loaded it to my arduino wifi shield but when i put in the browse my ip direction there only appears “page not available” because it cannot be connected with the server can you help me please thank you

    • http://www.marcoschwartz.com Marco Schwartz

      Hello Alex,

      Make sure first that your shield respond to a simple ping ip_of_the_shield in your terminal.

  • Arvin

    Hi, I’m interested with controlling the arduino in any part of world. I just want to ask if it is possible and can you give some insights on how to do it? thanks

  • omvaghela

    i used your program but if i want to use this without my local ip(i want to use my static ip) what should i do?
    (i dont want to use teleduino or nearbus.net or etc. sites)
    Can i do it with my arduino and ethernet shield or i have to use raspberry pi?
    THANKS!

  • Eugene

    Hi ,Marco.I am fun of your prolific job that you enjoy to do.I count that I can ask you to assist me with next.I tried to install an Ethernet shield from Arduino on Windows you use Mac right.I browsed the internet and could not find a tutorial how to install it on Windows to use wifi capabilities with the Arduino.Thank you in advance,Eugene.

    • http://www.marcoschwartz.com Marco Schwartz

      Hello Eugene, I’ll have a new book about the Arduino Ethernet shield soon!