Want a completely autonomous way to monitor rooms in your home (or even somewhere like your porch)? Want to see both your room's instantaneous status and how it's been doing over the past month? What if your child has been leaving the lights on when they're not in a room while you're at work? What if all you needed was an internet connection to check on your home or office?
Well lucky for you, monitoring your home is not only easy to do, it's cheap too!
In this step-by-step tutorial, you will:
- wire up a light sensor to detect whether or not a light is on in the room
- wire up a temperature/humidity sensor to capture the temperature and humidity levels and calculate heat index inside of your room
- wire up a motion sensor to tell when someone is in the room
- use an Arduino Yun (http://www.arduino.org/products/boards/4-arduino-boards/arduino-yun) to stream this captured information to a cloud service (https://www.initialstate.com)
- turn that information into your personal home dashboard that you can access on your laptop or mobile device
A major requirement of this project is that a WiFi connection is established automatically so that the sensor data can be sent to a remotely accessible visualization. The script that collects and sends sensor data should also start automatically in case it loses power.
An Arduino is perfect for this because it will run whatever sketch was last uploaded onto it when it is powered on. There are WiFi modules that you can add onto smaller Arduinos, but the Yun has an onboard WiFi module that makes establishing an internet connection easy. The Yun can even run Linux, though we don't take advantage of that in this project. The Arduino board also gives us a solder-free way to hook up both analog and digital sensors.
A lot of sensors can be difficult to hook up without a large breadboard, and many of them require extra resistors or capacitors. Grove sensors don't have these issues because they have an easy breakout connector, and any required resistors and capacitors are already attached to the sensor board.
That being said, there are many different ways that you could implement this project, but I'm going to walk through how I set up my sensor box using a Yun and Grove sensors.
The Yun is unique from many other Arduino boards because it uses an SD card to boot from. The Yun uses OpenWRT to run it's Linux side and manage WiFi
Plug your micro SD card into the SD card adapter and plug it into your computer. Make sure that your card is formatted to a FAT (not FAT 32) file system. This can be done from Disk Utility in Mac OSX or from the Computer window on Windows.
While your card is reformatting, you can download the latest OpenWRT image for the Yun on the Arduino Software page. Towards the bottom of the page, under "Other Software", you will see the latest OpenWRT image. Click on it and follow the prompts to download it.
Once your SD card is done formatting and OpenWRT has downloaded, copy the OpenWRT zip file to your SD card and eject the card. Now place the card in the Yun's micro SD slot and plug the Yun into your computer's USB port using the micro USB to USB cord.
Wait for the Yun to boot - it takes about a minute. You'll know it's finished when the white LED on the board comes on. Now check your available WiFi networks (the Yun creates it's own when it boots without a WiFi configuration) and connect to the one that says "Arduino Yun-90**********".
While connected to the Yun's Wifi, go to 192.168.240.1 into your browser address bar. You should see a page that looks like this:
The password is "arduino". On the following page, look for an orange RESET button - click it and wait for the Yun to reboot.
Once your Yun reboots with the updated OpenWRT, you can configure its WiFi settings. Make sure you're still connected to the Yun's WiFi and visit 192.168.240.1 again. In the top right corner you'll see a "Configure" button:
The following page allows you to change your Arduino's name (this will change the name of its default WiFi network too), change the password, and change the Yun's timezone.
WIRELESS PARAMETERS is the section we're most concerned with. This is where you can find the wireless network you want your Yun to automatically connect to on boot. Carefully enter the network's password and click "Configure & Restart".
NOTE: Once the Yun is connected to a wireless network, you can obtain its IP address by entering 192.168.240.1 in your browser address bar or by entering arduino.local, but you must be connected to the same wireless network. This will allow you to SSH into the Linux part of the Yun. We are not going to do that in this tutorial, but you can read more on it here:https://www.arduino.cc/en/Tutorial/LinuxCLI
If you ever need to change the Yun's WiFi settings but don't have access to the wireless network it automatically connects to, you can press the WiFi reset button. You need to hold the button for somewhere between 5-30 seconds and no longer or it will be reset to factory settings.
Communicating with your Arduino is mostly done through the Arduino IDE. This is how we upload sketches to the board that execute every time the board is powered on. Visit the Arduino Software page and find your computer type in the list to the right of the current version of the IDE that is available. Download the zip file and unzip it to install.
Open the IDE with your Yun powered on. Click on Tools and go to Board. Select "Arduino Yun" from the list. Still under Tools, go to Port and select either "Arduino at (IP address)" or the "/dev/tty.usbmodem****" that reads "Arduino Yun" after it.
Now that you have the IDE setup to interact with the Yun, we need to install a library to go with the Grove Temperature/Humidity Pro sensor.
This particular project only requires you to install one library, but there are all sorts out there that can make your life easier and your code shorter. They are also very easy to install.
This library is called "DHT-Sensor-Library". Go here and download the zip by clicking the button in the bottom of the right column. Then go back to the IDE and select Sketch -> Include Library ->Add .ZIP Library....
Select the zip file you just downloaded and then click "Choose". All done!
It's finally time to start adding some sensors to our Yun. Lucky for us, this is as easy as connecting a jumper wire from the Yun to a sensor. The small breadboard lets us share the Yun's 3.3V power supply and ground connection. We'll wire everything up and then test our sensors to make sure they're working.
Unplug power from your Yun before wiring up any circuit!!
We're adding three sensors and trying to keep everything compact, so the wires get a little messy - I'll break down the circuit by sensor.
The first thing we need to do is make sure that 3.3V power and ground are provided for the sensors. Since the Yun only has one 3.3V port, we need to move it to the breadboard where we can open up more connections. Connect a male-male tie line from the 3.3V power to one row on the breadboard and one from GND to the breadboard.
NOTE: In the picture and diagram I have another male-male tie line connecting two rows on the breadboard for both ground and power. This isn't actually necessary since we're only using 3 sensors, but if you decide to add more than 4, you'll need even more spots to access power.
The light sensor is our only analog sensor, so connect SIG to A0 using a female-male tie line. The temperature/humidity sensor is connected to Digital Pin 2, and the motion sensor is connected to Digital Pin 3. Connect VCC and GND on each sensor to the 3.3V Power and GND rows on the breadboard. The NC pins on the sensors mean "No Connection" and do not need to be plugged into anything.
Power on your Yun once the circuits are wired up.
We already installed the temperature/humidity sensor library. It actually contains a test script called DHTtester that you can find under File -> Examples in the IDE. Or you can copy it from here: https://github.com/InitialState/arduinosensorbox/blob/master/DHTtester.ino
Just create a new sketch and upload this to your Yun. Click on the Serial Monitor once it's finished to see the temperature, humidity and heat index print to the screen.
Once you know the sensor is working, you can move on to testing the next one!
Now let's test the motion sensor. It goes HIGH if motion is detected.
You'll find the test script here: https://github.com/InitialState/arduinosensorbox/blob/master/motiontester.ino
Open the serial monitor and start waving your hand in front of the sensor to test if it's working. It claims to have 120 degrees of detection - see if that's true!
The light sensor couldn't be easier to read from. The number it gives us is the ambient light level in Lux. You can test it with the sketch here: https://github.com/InitialState/arduinosensorbox/blob/master/lighttest.ino
Running this script will print the light level to the serial monitor. Try covering and uncovering the sensor to see how the light value changes. A good list of typical light levels for different environments can be found here: http://www.engineeringtoolbox.com/light-level-rooms-d_708.html
Now that we know all three sensors are working, we can move on to reading them all at once!
We have the three components of our project together and working - the temperature and humidity sensor, motion sensor, and light sensor. Remember, you can test each component with the DHTtester.ino, motiontester.ino, and lighttest.ino sketches if you need to troubleshoot a problem. It is time to put everything together. The next step is to find a home for all of our sensor data and turn that data into an easy-to-access dashboard.
We want to stream all of our sensor data to a cloud service and have that service turn our data into a nice dashboard that we can access from our laptop or mobile device. Our data needs a destination. We will use Initial State as that destination.
Step 1: Register for Initial State Account
Go to https://www.initialstate.com/app#/register/ and create a new account.
Step 2: Access Keys
We need a way to associate the data we're going to send with your account. Initial State does this with access keys.
Go to your Initial State account in your web browser, click on your username in the top right, then go to “my account”, you will find your access key at the bottom of the page under “Streaming Access Keys”.
Every time you create a data stream, that access key will direct that data stream to your account (so don’t share your key with anyone).
Copy this key because we need it for our test and our final script.
Step 3: Run the Example
Run the test sketch to make sure we can create a data stream to your Initial State account. Replace Your_Access_Key on line 14 with your access key and run the following: https://github.com/InitialState/arduino_streamers/blob/master/arduinoyunstream.ino
Step 4: Profit
Go back to your Initial State account in your web browser. A new data bucket called “Arduino Stream” should have shown up on the left in your log shelf (you may have to refresh the page). Click on this bucket and then click on the Waves icon to view the test data.
You will want to step through the Waves tutorial to familiarize yourself with how to use this data visualization tool. Next, view the data in Tiles to see this same data in dashboard form.
You are now ready to start streaming real data from your sensor box.
The final sketch that puts everything together is called yunsensorbox.ino and can be found here: https://github.com/InitialState/arduinosensorbox/blob/master/yunsensorbox.ino
Make sure to replace Your_Access_Key on line 26 with your specific access key.
Everything that you might need to change can be found in lines 7-36.
Beneath each respective sensor section you can specify which pin on the Yun you are using. For the temperature sensor, you also need to say which sensor you are using (the Pro is DHT22).
In the Initial State Streamer section the only thing you immediately need to change is accessKey. If you want to stream more or less information or change signal names/emojis, you can set that here too.
Before we run the sketch, let's go through what it is going to do.
NOTE: For initial testing purposes you may want to decrease the delay interval from 41000 to something more like 2-3000 on line 72
Upload the sketch to start your sensor stream!
NOTE: If your sketch only starts once you open the Serial Monitor you can fix it by removing any lines with the word "Serial" from the sketch. Of course, this also takes away the ability to debug the sketch. The Yun's Bridge allows us to access another debugging library, however, called the Console. To use the Console, place #include at the top of your sketch with the other #include statements, where the script says Serial.begin(9600); you will instead say Bridge.begin(); and then Console.begin(); right below it. Then just change "Serial" to "Console" anywhere else. You must be connected to the Port "Arduino at [IPADDRESS]" to see outputs to the Serial Monitor.
Go to your Initial State account and click on the new data bucket with the name corresponding to the BUCKET_NAME parameter (i.e. Yun Sensor Box). Click on Tiles to view your sensor box dashboard. You should see six tiles once you have let your sketch take measurements at least once - Humidity, Temperature, Heat Index, Light, Motion and How Am I Feeling. Wave in front of the motion detector, cover the light sensor, and watch the dashboard update in real-time. This dashboard gives you the ability to see what is happening in your room at-a-glance, very handy when you aren't at home or out-of-town.
If you want to see if there was motion in the room, look for a walking man in the Motion tile. Based on the light level in the Light tile, you can tell whether or not the lights were on. You can also see how Temperature and Humidity changed over time.
Switch to the Waves visualization to better see the history of your sensor data. With Waves you can take measurements and dig through your data to see when things happened. I can see that the lights were still on even though no one was in the room on July 16 from 12:09 pm to 12:18 pm.
This sensor box would also be useful to place in a garage or room where you may keep a pet. With it you can track the heat index to make sure that they aren't in dangerous conditions, especially during the summer.
This final section will show you how I crammed the Yun, breadboard and sensors into a convenient little box.
I have all sorts of cardboard boxes lying around from different projects, so I decided to turn one of them into a home for my Yun and sensors. My main supplies aside from the box were a pair of scissors and electrical tape.
I placed the breadboard directly on top of the Yun - it fit nicely between the pins and ethernet port.
I then taped my sensors together across the bottom and cut holes for them out of the top of the box. I could get the box shut once I cut slots for the tie lines. My box also had a convenient hole where I could access the power port of the Yun.