Intro: Edison Monitoring System With Motion Detection, Cloud Connection and Video Streaming
This project is about using Edison as a core of the motion detection system with webcam, which is also connected to Twitter, Google Drive, Xively and can blink the LED and beep too (of course! :). It also streams the webcam video over the network, so you can check it using any standard browser.
Edison runs an opens-source Motion software, which serves as a logic hub. Motion analyzes webcam stream in the real time and detects motion in it. When motion is detected, it executes several user-defined actions, which in our case will do all that cloud connection described above, i.e. post an event to Xively, upload a sequence of video frames with motion detected in them onto Google Drive, post a notification into Twitter and perform a visual/audible alert via buzzer and LED. All while streaming the webcam video over HTTP to your browser.
We'll be using Edison with an Arduino expansion board and a uvcvideo-supported webcam in this project. I used Microsoft HD-3000, but there are a lot of webcams supported by that driver, so pick your favorite one. Those ones capable of MJPG streaming in additon to raw are highly recommended as that uses much less USB bandwidth. For the beeping and buzzing part you'll need a small breadboard, a LED and a buzzer.
You can see the setup in action on several videos made by IFA 2014 (where I've presented it as a part of Edison official launch) visitors. Links are at the bottom of the main README in the GitHub repository I've created for this project.
Step 1: Install Motion
1. You can compile Motion from sources if you want to, however there is a ready-made package in my Edison package repository. Configure your board to connect to it using instructions on this page.
2. After that install the package by running on your board, as root user:
opkg update && opkg install motion
Step 2: Configure Motion
1. Motion is configured through a text-based configuration file, /etc/motion.conf by default. Download a pre-made version from my GitHub repo devoted to this project or use it as a base for your own version.
2. After that, configure Motion to autostart by creating an init script or systemd service configuration file. The latter would be better as Edison uses systemd as a primary system control and it would be a more "native" way to do things.
Init scripts do work too though and I've used namely that way. See a pre-made script here, you'll need to put it into /etc/init.d directory. By default it uses root user to run Motion, however if you aren't going to use the GPIO triggering functionality - i.e. buzzing and beeping, then if would be more secure to run it under a dedicated, unprivileged user, which you'll need to create.
See a step about web server setup later on for commands you need to run to create this user. You can run web server and Motion under the same user.
Step 3: Configure Web Server for Streaming the Video Across the Network
Motion is able to stream the video directly in the MJPG mode, however looks like there are bugs with the way it does that on all modern browsers (I've tried IE, Firefox and Chrome), so I've created a very simple wrapper page that works that around. The page is served by Node.js-based web server in this project.
1. First, create a user, home directory and a group for it. We'll use it to run the web server.
groupadd -r motion useradd -r -m -s /bin/true -g motion -G video motion
After that you should have a "motion" user, which is a member of "motion" and "video" groups (the latter one is to be able to access the webcam video stream - in case you run Motion under this user too as suggested in the Motion configuration step). You will also have a /home/motion directory created and set as a home directory for this user.
2. Copy the web server autostart script into /etc/init.d to have it started on OS boot.
3. Copy the "scripts" directory with all the contents into /home/motion. The "scripts" directory contains all the scripts we'll use in this project (including the web server one). See the README.md file on GitHub for details on the folder contents.
4. Install necessary Node.js modules to run the web server. Do that by running the below commands on the board, as root:
cd /home/motion/scripts && npm install
5. Edit the IP in /home/motion/scripts/html/index.html, paste the one your board has. When the web server is started, the page and the video stream will be available at http://<board IP>:10080
Step 4: Install Temboo SDK and Create/configure Your Temboo Account
To make the process faster and easier as well as to experiment with Temboo I used their service for all cloud connection scripts used in the project (Google Drive, Xively, Twitter), so you'll need to setup an account there.
There's a free plan that is limited from the number of service calls and the volume of data up/download standpoints, but both were not a problem during my tests as they were big enough.
1. You will need a Temboo Node.js SDK, my project scripts use it. Download it from Temboo web page, because NPM version severely lags behind. I've created this project a while ago and used version 2.2.0 of the SDK. If there's a problem with the current one - just update the project scripts or download an older SDK version.
2. Unpack the SDK into /home/motion/scripts/node_modules/temboo directory, project scripts expect to see it there. Or just modify scripts depending on your SDK location.
Step 5: Setup Pieces Necessary for Cloud Connection
1. As we're using three cloud services here: GDrive, Xively and Twitter, you will need to setup developer accounts on each of them. I will not go into details of doing that as each service has a nice (and up-to-date, unlike this description) instruction on doing that.
2. After obtaining necessary credentials, go to Temboo library page and run through OAuth Choreos for each of the services we'll use, to obtain necessary OAuth tokens, which will need to be pasted into our scripts. Again, the Temboo descriptions are much better than anything I could have here, so just execute their [very simple] instructions. Use comments if you get stuck, I'll try to help.
3. Paste the tokens into scripts, namely twitter_post.js, gdrive_upload.js and xively_post.js - look for comments in the script body to find the relevant places. I've tried to make them stand out very clearly.
Step 6: Setup the HW Part
1. Switch Edison into USB Host mode by flipping the mechanical switch on the Arduino expansion board into external power plug and full-size USB port.
2. Connect your webcam and make sure it's detected by the OS (check dmesg output after connecting the camera, you should see messages from a driver about detection)
3. On the breadboard, connect the LED and buzzer in parallel and connect them both to the digital pin #7 on the expansion board. You can use a different pin, but then you'll need to modify /home/motion/scripts/gpio_blink.sh script, including properly setting muxes, level shifters and pull-ups. Make sure to switch the expansion board into 3.3V mode or add a simple resistor divider for a LED if you want to use 5V.
4. Instead of a buzzer and a LED that could of course be any device triggered by a digital GPIO pin - the options are endless and project-specific.
Step 7: Done!
By now you should have a working system. Do some tests by e.g. waving your hand in front of a camera, you should see events appearing in Xively, photos being uploaded onto Google Drive, Twitter posts apearing and LED & buzzer acting respectively.
The web page should be showing the webcam stream in your browser.
Such a setup could be a base for various derived projects, so hopefully it will help you to implement a system you want to have!