# Smart Home Security Camera

This post explains the technical background of the security camera system, shown in the following movie. The post will answer the questions: How to detect a thief, how to take a photo, how to get notifications on your phone, and whether the system would work in reality.

## How to Detect a Thief?

In order to detect a thief I made use of an acceleration sensor that I placed at the apartment’s door. The sensor listens for significant changes in the measured x-acceleration. This technique was so precise, that even the slightest knock triggered the motion detection.

bool motionDetected(float tolerance) {
int16_t accX = mpu6050Data.acceleration.x;
return (accX > boundaries.max + delta * tolerance ||
accX < boundaries.min - delta * tolerance);
}

Because the door is at the opposite side of the room it was required to transfer the information (whether a motion was detected) to a Raspberry Pi’s camera. I made use of a 433 Mhz transmitter and receiver system with two Teensy microcontrollers respectively.

In a nutshell: When a thief enters the room, the acceleration sensor will recognize that. It will transmit that information to another microcontroller that itself forwards the signal to the Raspberry Pi (via a serial USB connection).

Getting the information from the receiver-microcontroller to the Raspberry Pi was not as easy as I initially thought. I am explaining the installation of the required npm package in the post Install Node SerialPort on Raspberry Pi.

Since I have to enter the room through the same door I needed to check whether it was a guest or me who came in. Recognizing the acceleration profile of the person who opens the door and analyzing it with a self improving machine learning algorithm until it works after I’ve generated 10.000 training samples? Easier: Assigning a static IP address to my smartphone and sending a ping request every time motion was detected. If the phone responds there was no doubt that it was me who came in. Or at least that my phone was within range of the WiFi router.

## How to Take Photos?

The primary component of the system is the security camera: After a thief has been detected, a Node.js script on the Raspberry Pi takes a photo and stores it locally. The npm package raspicam makes that part pretty easy.

I’m using the Rapberry Pi camera module but a USB webcam would do the job just as well.

In the video you saw that I was notified about my dad stealing a bottle of wine while I was hanging out in Karlsruhe’s park. In order to achieve that the Raspberry Pi functions as a Telegram bot. So what’s that? Telegram is an open-source messenger with an amazing API. Among other things you can create bots (Telegram users that are not actual person but a computer). In the past I published a post about another bot that I wrote.

For the notification I created a bot called “Smart Home” that is in my Telegram contact list. Now the Raspberry Pi is the computer behind that bot and sends a photo right into the normal Telegram chat so that I can see it on my phone if I have an internet connection.

The node-telegram-bot-api package simplifies the API for Node.js applications.

## Would it Actually Work?

If someone was seriously trying to enter my room without permission, would it work? I had a comparable situation when the apartment lessors announced, the annual inspection of the fire detection systems of each apartment would be conducted next week. I was not at home so the system was armed and ready to detect the technician.

And indeed: A few hours after I left the apartment, a bunch of photos came in, showing the brave technician doing his job (photo 2). On the way out it looks like he saw the acceleration sensor at the door and inspected it briefly (photo 4). The following photos proof the operability:

### Timo Denk

Software developer at SAP and Denk Development, student of Applied Computer Science at Baden-Württemberg Cooperative State University. Interested in programming, math, microcontrollers, and sports.

## 8 thoughts on “Smart Home Security Camera”

1. Vichyle Her says:

I am trying to use telegram to send a picture when motion is detected by a PIR sensor and the pi NOIR camera takes a photo. However, i am getting errors just simply taking and sending a photo to my phone in a simple program. Hopefully you guys can help and guide me in the right direction…?? I would really appreciate it!

2. Hi Vichyle,
I can only recommend to solve the problems separately. Assuming you want to go with Node.js as well, try to first

1. take photos with Node.js and store them as files,
2. write a simple Telegram bot (https://github.com/telegraf/telegraf/),
3. send photos with the bot, and finally,
4. put everything together.
1. Vichyle Her says:

Is there a way i can contact you through a messenger of some sort for quicker communication??

3. Amin Mazlan says:

I am trying to make your security camera as my final year project, could you show me how to make an acceleration sensor?

1. There is a short snippet in the blog post (“How to Detect a Thief?” section) which explains parts of it. I am reading the acceleration sensor data from an MPU6050 (there are tutorials on the internet for that) and check if it has exceeded a certain threshold. If that’s the case, a door-opening is detected. In my setup, the threshold is determined automatically at initialization (but that can also be hard coded).
Good luck with your final year project!

4. Amin Mazlan says:

thanks for your reply sir, if you dont mind. May i know how you connect it to your telegram, and does it auto disable when you walk in to the door? Thanks Sir

1. Hi Amin, I have connected the Node server to Telegram using the NPM module node-telegram-bot-api. However, these days I would recommend using Telegraf (https://github.com/telegraf/telegraf/).
The alarm did not fire when I came through the door. The system detected that it was me, by sending a ping to my smartphones IP address. If it was connected to the WiFi it would know that I was there too.

5. Amin Mazlan says:

Hi sir, I’m back again, i seems to have problem building the sensor. I didn’t quite sure about the component or hardware that needed to be use in creating the sensor. Could you tell me more details? Thank you if you can help with this.