Webcam Security System

A while ago I set up an old webcam / Raspberry Pi to serve as a security camera. Using Motion[1], its easy to set up a system that will record any movement and can be manually enabled or set to run on a simple schedule such as while I am away at work.

Eventually I made the system a bit smarter to automatically turn on when I leave home and turn off as soon as I get back. Added to that, if anything moves while I am out, I get a message with a photo sent to my phone.

For fun, I also added an AI to my messaging system so if I get bored I can have a chat or ask it to do some pre-programmed tasks (such as shutting down systems on the network, taking a photo or changing its settings).

The whole system is built using:

  • Motion[1]
  • Python
  • Nmap[2] / hping3[3]
  • Telegram[4]
  • PHP / SQL

Python Process

The heart of the system is written in Python and is set to run as a systemd process. It mostly sleeps and wakes up every so often (~10 seconds) to check for new motion events or messages from telegram.

Network presence detection

It turns out that its not easy to detect the presence of iPhones on a network. In order to save power they can be come rather quiet and uncommunicative to network chatter.

At first I tried to build something in Python but after it became clear it was no small task, I decided to delegate to a subprocess and run a system tool. Next, I went with a script using hping3[3] and arp tables. This still wasn’t fantastic so at the moment I am just using an nmap[2] ping. From my tests, this seems to work better than hping3 at finding the iPhone but my tests weren’t very scientific.

Monitoring

I set up a database to log network presence and created a small PHP script to graph phones on the network. In the example below, you can see the blue graph for my phone, pink for my girlfriends and green shows when one of us are at home (and the system is turned off).

2019-02-13

2019-02-14

2019-02-15

Messaging

Initially I build a messaging component using the free Telstra API[5]. This provided an API allowing me to send SMS messages to my phone when the system started up or movement was captured. I ripped this out when Telstra added more limits on the API and put more of a push to their priced model.

Telegram

After looking for a better way to do push notifications to my phone, I eventually settled on creating a Telegram[4] bot. This allayed my cost aversion, provided the push notification behaviour that I was after and opened up easier two way communication.

AI

I took an old copy of the A.L.I.C.E. AIML[6] bot and plugged it into my Telegram bot. Its hardly going to convince anybody in a Turing test but is still quite a bit of fun to chat to.

Image Capture

Motion provides a very easy to set up frame/video capture process on Linux. It has the ability to trigger actions when motion is detected but I have instead chosen to go with periodically polling its output for events and archiving captures.

Capture from Motion

References

(last checked 2019-02-16)

  1. https://motion-project.github.io/
  2. https://nmap.org/
  3. http://www.hping.org/
  4. https://telegram.org/
  5. https://dev.telstra.com/
  6. https://en.wikipedia.org/wiki/AIML