Have you ever sat at your desk and wondered what was happening back at home? Maybe you wanted to see how your baby was? Maybe you wanted to make sure that the dog wasn't sitting on the couch or maybe just enjoy the view outside your living room!
If only there was a way! Ever wished you had eyes everywhere? Well, good news! Now you can! And even better news! You don't have to spend a cartload of money!
You can build your very own IoT Sentry Ward using an Intel Edison, an old Android phone, a few household materials, and pure elbow grease!
Your Sentry Ward allows you to sneak a peek into what's happening around your house!
All you have to do, is tell it what you want on Twitter, and it will send you a photo of what it can see!
So what are you waiting for? Let's get started!
You very own IoT Twitter Sentry Ward can be made using just a few items you can find lying around the house. you will need;
- An Intel Edison
- An Arduino Breakout Board for the Intel Edison
- An old Android Phone (requires android 2.3.4+)
- A working Internet connection
In terms of software, you'll need to download the following, all of them are free of cost;
- PuTTY Terminal
- Python 2.7
- FileZilla FTP Client
- Sublime Text 2 or a similar text editor software (Notepad will do as well, but Sublime Text looks much cooler!)
You can also customize the behaviour of your sentry ward to suit your needs. In this tutorial, I'll be showing you an example of how you can do that. For that you'll be needing;
- A photoresistor
- A 10 KOhm resistor
- A general purpose PCB
Step 2: Setting Up The Edison
The first thing you'll need to do is set up the Intel Edison. There are a number of very comprehensive guides scattered around the Internet on how to do that. This one in particular is my favourite if you plan to develop many more applications on your Edison.
However, in the interests of thoroughness, I'll go over some basic procedures here as well.
- Download the Intel Edison Drivers
- Connect your Intel Edison to your PC using the J3 and J15 micro USB ports
- Start the Driver setup and follow the instructions given
Now you've completed the basic setup for your Edison. Now we'll get to configuring your Edison to your WiFi at home.
- Go to Start Menu >> Device Manager >> COM (Ports and LPT)
- There will be a device labelled "USB Serial Port". Note the COM number written in brackets in front of it.
- Open PuTTY and start a session with the following parameters
- COM Port = (Number you noted down)
- Baud Rate = 115200
- Click "Open"
- Hit Enter twice and type in "root" when prompted
- Type in "configure_edison --setup"
- Follow the instructions to set your password and WiFi. It will show you the IP address of your Edison when you're done.
- To test the connection type in "ping www.google.com". If you see that Google is returning your pings, your Edison is connected to the Internet!
For every sentry, you're going to need a watchful eye that is capable of capturing images.
Good news is, you don't need to buy an expensive camera or camcorder! You can make one using an old Android phone lying around the house! Just make sure its running an Android version post 2.3.4 Gingerbread.
I'll be using an old Sony Xperia Play that was catching dust in my drawer. It shipped with a snazzy little dock onto which it sits pretty comfortably and so I don't have to worry about propping it up!
What you're going to want to do is install an app called IP Webcam on your old Android device from the play store from the following link;
Once you're done, make sure you're connected to your home wifi network and open up IP Webcam and scroll to the bottom.Select "Start Server" and note the IP address that appears near the bottom of the screen.It should look something like "http://192.168.x.xxx:8080"
To check if your camera is connected to the network, open up a browser on your computer and type in the IP address you saw on your phone including the 8080 at the end.The tab will be named "IP Webcam", and you should see a number of options regarding the video recorder.Select the "Browser" button in front of the "Video Renderer" option and you should see a live stream of the video on your Android device!
Alright! Now that you've configured your sentry, let's see how we can get it to send image data to the Internet!
In order to communicate with a Twitter account, we will need some credentials. These credentials are stored by Twitter in keys which it allows developers to use for their apps. To get your keys, do the following;
- Go to https://apps.twitter.com and select "Create New App"
- Fill in your app details like its name and description. In the Website field, you may put in a dummy webpage. Agree to the conditions and hit "Create your Twitter Application"
- Once your application has been created, go to the "Permissions" tab and select "Read,Write and Accessdirect messages" and hit "Update Settings"
- You can check your keys in the "Keys and Access Tokens" tab. Scroll down and click on "Create my Access Token"
- Once the tokens have been created, make note of;
- Consumer Key
- Consumer Secret
- Access Token
- Access Token Secret
Great work! Now you've configured Twitter to send and receive data from your account!
Now, what we essentially want the Intel Edison to do is;
- To keep checking your Twitter feed for any command
- Once it gets a command, to get a photograph from your Android Ward
- To upload that photograph as a reply to the command.
To talk to our Edison, we are going to use Python. It's an open source language with lots of resources and a growing pool of users. It is also very easy to learn and understand. So let's get started!
Python uses files known as libraries to pick and choose between the tools it needs to execute a certain code. The aim of our code is to upload a photograph taken by our sentry Twitter every time we tweet "Click!" to our handle.
To do that, we are going to use a library called Tweepy. Tweepy is basically a collection of files which makes sending and receiving data from Twitter to Python much easier. So in order to install Tweepy on your Edison, you'll have to do the following steps;
- Open PuTTY and SSH into your Edison as described in the Tutorial
- Type in "sudo pip install tweepy"
- Wait for the package to install, and you're good to go!
In order to get the image from the Android Ward, we also need to have a Python library which can handle images. A very popular library which does that is called OpenCV.
To install OpenCV, you need to follow the same procedure as for Tweepy, except that instead of "sudo pip install tweepy", you type in "opkg install opencv"
Awesome! Now you've made sure that your Edison has all the tools it needs to handle images and send them to Twitter!
Download the code attached to this step, and fill in the following parts with your own keys and access codes;
Also fill in your own camera IP in the indicated field along with your Twitter handle
Once you've written this code, save it as "tweetpic.py"
Now, open up FileZilla and connect to your Edison.In the left pane, navigate to the location where you saved your script. Right click on it and click on upload. The Transfer bar on the bottom should indicate a successful transfer.
Once that is done, open the PuTTY terminal and type in python tweetpic.py
Great! Now your Edison is constantly sniffing your Twitter feed for commands!
Now for the fun part!
Go to your Twitter account and tweet "@(your Twitter Handle) Click!"
Wait for a few moments, till you get a notification. Is it what you think it is?
Your Edison has successfully read your command, processed it, then it grabbed a photograph from your Android Ward and uploaded it as a reply to your tweet!
You now have a fully functioning IoT Ward which allows you to look at what's happening at home from anywhere in the world!
Now, you'll be preparing your very own light sensor which allows the Edison to detect if the lights around it are on or off!
For that, you'll have to do a little soldering, or if you aren't sure about it, you can build the circuit on a breadboard as well!
Arrange the components as shown, and solder them according to the circuit diagram.Please make note of the supply voltage and ground pins.
What the photoresistor does, is that it varies its voltage according to the light incident on it, and thus voltage changes can be tracked when it is connected in series with another resistor. The configuration of our circuit reflects in the fact that the voltage increases with an increase in incident light.
Once you've built the circuit, use the jumper wires to connect the sensor to the breakout board as follows;
- Connect the supply terminal of the sensor to the 5V on the board
- Connect the ground terminal to the GND terminal of the board
- Connect the output terminal of the sensor to the A0 terminal of the board
Cool! Now your Edison has a sensor to detect any changes in the light around it!
Now, in order to get the Edison to work as a security system, you'll have to change some code that you've fed to it. To do that, just use the attached code, and upload it to the Edison, just as you did in the earlier step.
Use the attached code, named tweetint.py.
Once you've done that, it's time to test your new security system!
Open up a PuTTY terminal, and use the command python tweetint.py
Now switch off the lights in the room, and open up your Twitter account.Now if your Twitter Handle is say; "chucknorris", tweet the following message "@chucknorris Start!"
You should see a message on your PuTTY Terminal saying "Security System started!"
Now, switch on your lights.
You should get a photograph as a reply to your tweet on Twitter!
All Done? Great!Congratulations! You've built your very own home made security system that you can use to detect any unwanted presence in your house!
import tweepy import cv2 import urllib import time consumer_key = *YOUR KEY HERE* consumer_secret = *YOUR KEY HERE* access_token = *YOUR TOKEN HERE* access_token_secret = *YOUR TOKEN HERE* your_handle = *ENTER YOUR TWITTER HANDLE HERE* camip = *YOUR CAMERA IP HERE* chk_old=0 auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) imag = urllib.URLopener() while 1: tweet = tweepy.Cursor(api.search, q = your_handle, lang = 'en') count = 0 for tweet in tweet.items(): print (tweet.text, tweet.author.screen_name,tweet.id) txt = tweet.text hand = tweet.author.screen_name chk = tweet.id cond = chk_old!=chk print cond count += 1 if count > 0: break if txt == your_handle + ' Click!': if cond: imag.retrieve('http://'+camip+':8080/shot.jpg','shot.jpg') img = cv2.imread('shot.jpg') time.sleep(3) this = '/home/root/shot.jpg' psts = '@'+hand+' This is what I can see!' api.update_with_media(filename=this, in_reply_to_status_id=chk, status=psts) chk_old = chk print chk_old print 'done' time.sleep(11)
import tweepy import cv2 import urllib import time import mraa consumer_key = *YOUR KEY HERE* consumer_secret = *YOUR KEY HERE* access_token = *YOUR TOKEN HERE* access_token_secret = *YOUR TOKEN HERE* your_handle = *ENTER YOUR TWITTER HANDLE HERE* camip = *YOUR CAMERA IP HERE* chk_old=0 auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) imag = urllib.URLopener() light = mraa.Aio(0) tweet = tweepy.Cursor(api.search, q = your_handle , lang = 'en') count = 0 for tweet in tweet.items(): print (tweet.text, tweet.author.screen_name,tweet.id) txt = tweet.text hand = tweet.author.screen_name chk = tweet.id cond = chk_old!=chk print cond break if txt== your_handle + ' Start!': print('Security System Started!') while count<1: tweet = tweepy.Cursor(api.search, q = '@satyasiot', lang = 'en') count = 2 val = float(light.read()) print val if val > 500: if cond: imag.retrieve('http://'+camip+':8080/shot.jpg','shot.jpg') img = cv2.imread('shot.jpg') time.sleep(3) this = '/home/root/shot.jpg' psts = '@satyasiot Intruder Alert!' api.update_with_media(filename=this, in_reply_to_status_id=chk, status=psts) chk_old = chk print chk_old print 'done' time.sleep(11) for tweet in tweet.items(): txt = tweet.text hand = tweet.author.screen_name chk = tweet.id if txt=='@satyasiot Stop!': while 1: