Monitor Your Home Remotely Using the Arduino Wifi Shield

Published on December 2016 | Categories: Documents | Downloads: 21 | Comments: 0 | Views: 195
of 6
Download PDF   Embed   Report

Monitor Your Home Remotely Using the Arduino Wifi Shield

Comments

Content

Monitor your home remotely using the Arduino Wifi Shield
A 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:

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:

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:

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

// Push button pin
int pushButton = 2;
// Setup
void setup() {
Serial.begin(9600);
}
// Main loop
void loop() {
// read the pin number 2
int buttonState = digitalRead(pushButton);

15
// print out the state of the button
16
Serial.println(buttonState);
17
delay(1);
18
}
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.

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:
1

attachInterrupt(0, setDoorStatus, RISING);

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:
1

WiFiClient client = server.available();

If this is the case, we start building a very simple HTML page by calling the println() function of client:
1
2
3
4
5
6
7

client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<meta http-equiv=\"refresh\" content=\"5\">");

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

if (door_status == false){
client.print("Everything is ok");
}

4
5
6

else {
client.print("Alert ! The door has been opened");
}

Finally, we finish the HTML page with:
1
2

client.println("<br />");
client.println("</html>");

And then we stop the connection:
1
2

client.stop();
Serial.println("client disonnected");

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:

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

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.

A useful component in home automation : the Arduino Wifi
Shield
Hello, home automation enthusiasts ! Today I want to speak about an exciting Arduino shield that I
recently bought : the official Arduino WiFi shield. If you remember, inside the first article I posted on this
website I spoke about measuring the temperature in your flat using your computer and an Arduino board, and
then sending this temperature data on the web. Well, what if you could do the same but actually without your
computer ? What if the Arduino itself could send the data to the web ? Why is it better than the widely used
Arduino Ethernet shield ? That’s what I will talk about in this article.
Well, first of all, I wanted to order the Arduino Ethernet shield, but then I realized my computer does not have
an Ethernet port anymore, so it would have been a bit complicated. Plus, this avoid another cable lying on my
already messy desk. Here is the shield itself:

First thing I wanted to say, this shield is so easy to use. I won’t get into too much details in this article, but I will
show you how easy it is to remotely measure something (for example a temperature) with just your Arduino
board and this shield. It is very well integrated in the Arduino tools. Loading the library that allows you to use
the shield is super easy, and it really worked out of the box.
I think this shield is great for every electronics hobbyist using Arduino that want to try out the web features of
the Arduino platform, without having to plug yet another cable. Yes, it is more expensive than the Ethernet
shield, but I really think it is worth it.
The first thing I did after plugging the shield into my Arduino UNO board was to hook it up with my local Wifi
network. For that I just followed the official Arduino tutorial for WEP networks , and it worked like a charm :

The next step was to actually do something with the shield. I tried to get tweets and print them on the serial
port, that worked but was not very impressive. Instead I decided to hook up the TMP36 temperature sensor (like
in the first tutorial I’ve written) to my Arduino board and send the data online, without the help of my computer.
I will do another tutorial about how to do this entirely yourself, but in this article I just wanted to show you that
it is possible. For this purpose I used a website named Cosm (www.cosm.com), where you can create a page
that will plot data sent by any source like your Arduino. The official Arduino website also have a nice tutorial
for that.
The Arduino software that was automatically generated by Cosm worked fine, you just have to modify the
Ethernet part to adapt it to the Wifi shield, but that is very easy. After letting my shield sending some data to the
web, that was the result on the Cosm website :

Again, I was amazed by how easy it all was. In a few minutes I had set up a remote temperature sensor in my
flat, with a nice graphical interface ! There is one negative point for this shield : it is a bit expensive. But it is
easily compensated by what it does and how easy it is to use it. So if you are interested in using your Arduino to
interact with the web, this is definitely a shield I recommend. The shield is quite easy to find and you can for
example purchase it on Amazon. As usual, I included links to Amazon in this article so that you can easily find
the products I am talking about. It is also a good way to support this blog by buying products through these
links, so thanks in advance !
That’s all for this article, hope you liked it, there will be a follow-up article where I will show you how to use
this shield and write your own software to upload temperature data on the web !

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close