Twitter notifications for MotionEyeOS video surveillance system on Raspberry Pi
MotionEyeOS is a wonderful project by Calin Crisan that converts your single board computer into a video surveillance system in a matter of minutes. It is supported on a number of devices and is well maintained with good community support.
Now that the latest Raspberry Pi Zero version (W) comes with onboard Wi-Fi, it makes an ideal candidate to deploy MotionEyeOS across your perimeter and run a cheap video surveillance system with lesser clutter of having to attach a Wi-Fi dongle as in the previous versions of Pi Zero.
Motion notifications
MotionEyeOS offers push notifications in the form of email alerts and custom scripts upon detection of motion. The community has already chipped in with options like Pushover and IFTTT. I was playing around with the software on Raspberry Pi over the last few days and thought that Twitter notifications on mobile would be nice to have. I did some basic search to see if someone already implemented this, however couldn’t find any and decided I will code my own.
Now the SD card images supplied by MotionEyeOS are stripped down versions of the Raspbian OS with only the necessary features for performance reasons. To enable Twitter notifications using my method below, you will have to install MotionEyeOS on top of Raspbian (lite version preferred). This is to ensure that the Python Twitter module can be installed.
Installation steps
I assume that you have the latest version of Raspbian Jessie Lite written to the Micro SD card. I have tested the following method on Pi Zero and Pi 3B and works fine. Use sudo where necessary.
1> Power ON the Pi and open a SSH session.
2> Use raspi-config to enable the camera module.
3> Open the /etc/modules file and add bcm2835-v4l2 on a new line.
4> Follow the steps mentioned here under ‘Instructions’ to complete the installation of MotionEyeOS on Raspbian.
5> Create a dummy Twitter account and setup the API keys. Here is a good resource on generating the Twitter API keys to use in the script.
6> Install Python Twitter
pip install python-twitter
7> There are a couple of ways of receiving the push notifications on your Twitter account:
a) Setup the dummy Twitter account to post a Tweet every time motion is detected - script 1. Configure your Twitter account to follow the dummy account and receive push notifications on your mobile device using the steps here.
b) Script variant that tweets the latest captured image along with the tweet - script 2.
IMPORTANT: If you are planning to use this, make sure that your tweets are protected so that only you or those accounts that you permit can view your tweets.
c) Setup the dummy Twitter account to send Direct Messages (DM) to your Twitter account - script 3. Enable notifications for DMs on your mobile device.
Using b) above has the added advantage of adding the latest image captured by MotionEyeOS to the Tweet. DMs do not support attaching an image though.
8> Once you finalize the script to use, create folder at /var/lib/motioneye and store the python script there. Ensure that the script has execute permissions.
9> Login to the web GUI for MotionEyeOS and configure to run the script under ‘Motion Notifications’
Caveats
- The Twitter API has limits on the number of tweets and direct messages. However during my tests, none of these limits were hit and I have a fully functional surveillance system at home using this method.
- In one of the scripts (2), I have added a method to upload the latest image capture to the motion alert tweet. This method has its own privacy implications which is the same as uploading your data to any other cloud service. Make sure that you know what you are doing. :)
- Do not expose your live feed to internet using port forwarding. In my case, I OpenVPN into my Raspberry instance to view the live feed.
- Needless to say, use strong passwords for SSH and other service that you enable with MotionEyeOS. Do not re-use the passwords. Also remember that MotionEyeOS stores the password in plaintext in config files.
Hope this post is useful for those who wish to setup Twitter notifications for MotionEyeOS. If you run into problems, please post a comment below and I will try my best to help.