My latest little side project has been utilizing a Raspberry-Pi to turn on lights via motion detection. Here’s a quick run down of how I got this working.
What you’ll need:
– Raspberry Pi w/ Raspbian
– Webcam
– Hue lights
– Motion (https://github.com/Motion-Project/motion)
First things first, head over to the motion github page to get that installed on your Pi. This will do all the heavy lifting (why re-invent the wheel here?) as far as motion detection goes. Motion will let you specify a script to execute if it recognizes motion in a specific area of the screen or motion at all. That’s what primarily makes this such a useful tool. You can also have it display your webcam feed to a webpage and show a little crosshair tracking your movement, but that’s just icing on top.
Motion uses a .conf file to specify all these settings including what script to execute when. That script is usually found in /etc/motion/motion.conf. If you need some help, you can check out my .conf file here.
Here’s where I got to flex a little bit of bash coding. Note: Definitely not a bash expert but learning! The magic happens in the turn_light_on.sh script.
#!/bin/bash
#Hue variables
export HUE_BRIDGE_IP=http://192.168.0.2/
export BASE_API=http://192.168.0.2/api
export USER=yDuQBo8mhfNz24YCVoCEkqeU2k05Sdos4c6E4d6D
export CONTENT_JSON="Content-Type: application/json"
#Time variables
export TZ='America/New_York' date
export CURRENT_TIME=$(date +"%H")
export MORNING=7
export EVENING=18
#Debug logging
echo "Time is:"
echo $CURRENT_TIME
echo $( date )
if (( $CURRENT_TIME < $MORNING || $CURRENT_TIME > $EVENING )); then
curl $BASE_API/$USER/lights/1/state -H CONTENT_JSON -X PUT -d '{"on":true}'
curl $BASE_API/$USER/lights/3/state -H CONTENT_JSON -X PUT -d '{"on":true}'
curl $BASE_API/$USER/lights/4/state -H CONTENT_JSON -X PUT -d '{"on":true}'
fi
As you can see, this is really simple. I setup my hue api variables to make curling to my bridge a bit easier, setup my time variables (Don’t want this going off during the day or waking anyone else up), do some log statements so I know stuff is happening, check if the time is valid, and then turn on some lights.
I think if I’ve learned anything from this project it’s 1) Knowing what tools to do for what. For a little hack put together this doesn’t require MVC, server side code, etc etc. This would take triple the time if using Java or C++. This also isn’t really an app idea. 2) This kind of stuff isn’t as hard as people make it out to be. Granted we could go write our own motion detection code, make an app, a web server, etc etc but to start off this is a really simple project that was fun to work with and didn’t require much setup.
Going forward I definitely want to expand this, make it smarter etc etc. I would even consider re-writing the whole thing in OpenCV. To start off though, I’m very happy with this project so far.
All in all, Android and iPhone apps are definitely still fun, but this was a nice break. I think I would have needed a lot more time to do this on either of those platforms.