Get in touch



Intruder detection with Raspberry-Pi


This demo is featuring use of Raspberry-Pi as a gateway in an alarm system. We connected a camera and accelerometer to the Pi and used Reactive Blocks to make a simple intruder detection application.

Every time vibration is detected, the camera takes a picture and checks if there is a change in the image. If a change is detected, it sends an SMS and an email with an attached image of the intruder.




To make this demo application, we used the following:

  • Raspberry-Pi with Raspbian OS
  • A standard USB camera
  • An accelerometer, we used XLoBorg
  • Reactive Blocks development platform 



You can see the XLoBorg accelerometer connected to the GPIO I2C bus of the R-Pi in the lower right corner.  Refer to these documents for specification.

How we built it

The Raspbian release has Oracle Java preinstalled, so running Reactive Block applications on Raspbian is very easy. At the top level we ended up with this system:


The top system consists of 5 building blocks; 3 standard and 2 custom made blocks:

Custom made building blocks to handle hardware

  • Vibration Sensor, interfaces accelerometer via I2C bus.
  • Frame Grabber, grab images from USB camera

In addition the hasChanges() method that compares two images.

Standard blocks from the M2M library:

  • Limiter, this block is introduces to avoid too many mails/SMS when intruders are detected..
  • Standard blocks for sending SMS and email.

Lets have a closer look at the two custom made building blocks

 Vibration sensor


You will see that it consists of all standard building blocks from our libraries.

The R-PI I2C building block, supporting I2C communication bus on the Pi, is used to initialize and read data off the accelerometer. It is is polled at 10 Hz, but vibration has to be continuous for more than 1 second to be detected.

The isVibration() method will return true and start a timeout if acceleration all axes combined is above 1.05g or below 0.95g.

If detection continues for more than one second the vibrationDetection pin is fired. This one second filtering was added not to overload the Pi with too much heavy image processing.

Frame Grabber


The Frame Grabber block is relatively straight forward, a separate thread is started to read data from the camera. The thread listens to a command queue and grabs images when commanded to. When the blocks receive a command via the grab pin it will take one picture and deliver it on the frame pin.

The block uses OpenCV and JavaCV, these libraries must be installed on the Raspberry-Pi. This is no big deal but building and compilation takes several hours and should preferably be done while you are at home resting or sleeping.

OpenCV can be found here and JavaCV can be found here.

Detect changes in image



Viewing again the system at the top level, you can see there is a separate method hasChanges() that will detect if motion is in the stream of images.

This method uses OpenCV and JavaCV to compare two images and will flag change in image as motion detected.

This method is very CPU-intensive, so the actual processing has to be run in a separate thread not to violate recommended Reactive Blocks programming.

A positive (i.e. change in image stream) is signaled with the MOTIONDETECTED event. Attached to the event is also a string with filename of image.

The limiter is used to limit number of messages to once a minute; in a real system this should probably be higher.



Notification is done with SMS and email.  The file with image is attached to the email, and could be attached to the SMS if the service supported that feature.

If you want to make a detection system like this one, sign up and log in to get access to the building blocks used in this project.

You can of course experiment and use a motion detection sensor instead of an accelerometer to detect changes. We have building blocks and example systems for that as well. Log in to learn more and start developing your own applications with Reactive Blocks. Please feel free to contact me if you are interested.




Posted: 25. August 2014

Contact information

Professor Brochs gate 2, 1st floor
tel: + 47 957 26 339 NO 997 140 672 MVA