Motion Detection Alarm System




A basic USB camera can be used to detect motion in a room. In the following steps we will show you how you can use Reactive Blocks to program a ready to run Java application that sends SMS or Email when motion is detected. With Reative Blocks you make Java SE applications so the alarm application can be deployed on any machine with Java SE and a camera attached or integrated. The application is easily deployed on the Pi since the Raspbian release has Oracle Java pre-installed. .

This is what you need:

  1. Raspberry Pi Model B+ with Raspbian OS
  2. Standard USB camera
  3. Ethernet cable
  4. Screen and keyboard for the Pi
  5. Windows or MAC computer with JDK
  6. Reactive Blocks, an Eclipse plugin, to program the application.

This alarm application is one of several modifiable example systems that are accessible after you install Reactive Blocks. The application is made ready to run out of the box and you won´t need any Java programming skills to complete the tutorial. To make your own Reactive Blocks applications and building blocks, you need to be a proficient Java programmer. When building more complex applications with several sensors and actuators, Reactive Blocks makes it easy to get the logic and the data flow right. Reactive Blocks is free for open source projects.

Step 1: Install Reactive Blocks

Install Reactive Blocks to your computer from,
Reactive Blocks is free for Open Source Projects.

Step 2: Download the Application From the Libraries

Make sure you have opened the Reactive Blocks perspective and that you are in the Blocks view.
From the Blocks view select the import button and choose Reference applications. Select the Motion Detection project. At this point you will be asked to register with a google ID. This will give you immediate access to the building blocks libraries and modifiable systems and tutorials.

The Motion Detection project you downloaded includes three applications, the Alarm System SMS, the Alarm System Email and a test application the Test motion detection. In this tutorial we guide you through the Alarm System SMS . If you rather send Email you can do the same steps with the Alarm System Email

Step 3: The Application at Top Level

This step gives an explanation of the application. The first picture shows the diagram of what the complete application looks like at the top level. It is composed of 7 building blocks; 5 standard building blocks from the libraries and 2 custom made blocks that were made for this application.

Standard Blocks from the IoT library are:

  • Grabber, grabs images from the USB camera
  • Motion detection, performs the actual image detection
  • Periodic Timer, starts at accurate time stamps and sends a signal on the tick pin every period.
  • Buffer Eager, pushes out the first element in the queue automatically. The bufferensures that only one message is sent to the SMS Send block since it can only process one message at a time.
  • SMS Send Buffered, sends SMS through the Twilio service. The elements to be sent are buffered to ensure that only one message is sent at a time.
  • Limiter, to avoid too many SMS when an intruder is detected.
  • Read Java Properties file, reads config data from input file
  • View, views an image on the screen.

The Periodic Timer block works as a system engine. It will periodically issue a tick that sends a signal to the Grabber block via the grab pin.The Grabber block uses the Open Intelligent Multimedia Analysis for Java, OpenIMAJ. When the block is initialized, a separate thread is started to read data from the camera. The thread listens to a command queue. When the block receives a command via the grab pin it will take one picture and deliver it on the image pin.

The image is then passed to the Motion Detection block that performs the actual image detection. The Motion detection block also uses OpenIMAJ. A method within this block compares two images and will flag changes in image when motion is detected. The method is very CPU intensive and has to be run in a separate thread. When an image is detected it is signaled on the motionDetected pin which will trigger the generation of an SMS message.

The light blue boxes are all Java methods specifically made for this application. Clicking on a method block opens the Java editor.

When you click on a building block you can see the details of the logic inside the block. If you for example look inside the ReadConfig block you will see it is made by combining one existing building blocks and 4 Java methods.

The Limiter puts a limit on the amount of generated messages. Notice the P in the right corner of the building block. It indicates that you can set the miniterval of how long the application must wait before it sends a new SMS. The default setting is 300000ms, i.e. 5 minutes. To change the value. Right click the block and choose parameters and generics.

Step 4: Enable SMS

This alarm application uses Twilio as SMS service. To enable SMS you must edit the file config/ You can find it in the package explorer view under br.ref.motiondetection, see the picture.

This file is the same file used in the Alarm System Email. If you rather have email notifications you will use this application and edit the file with your email credentials.

If you don´t have the Twilio service you can modify the application to use your own SMS service. We also have a ready to use building block for the Clickatell service. Or you can attach a modem to send SMS.

You can find building blocks for SMTP email, AirVantage Cloud, IBM Bluemix and Xively to name a few.

Note that to make changes, you need to first make your own project. Then copy/duplicate the application into your newly created project. You can see how this is done here:

Don´t hesitate to contact me if you need help

Step 5: Build and Run the Application on Your Laptop

You need to build the application before you run it, and deploy it to the
Raspberry Pi. This is done using the Reactive Blocks code generator that creates a complete Java project from the structure of the building blocks. The code that represents the diagrams is the synchronization code that handles events in the right order and at the right time, and typically accounts for more than 60% of lines of the code in your program. Since the building blocks are precise mathematical structures the generator can transform this logic into very efficient code. This is very nice Reactive Blocks feature because to write such code manually is tedious and difficult. To generate the code, this is all you need to do:

Right click the project Alarm System SMS under the Projects tab and Select Java standard edition. Make sure logging info is INFO. The generated project will then open automatically under the Package Explorer tab. See the figure for details.

Step 6: Run the Application on Your Computer

Now you can execute the generated project. In the Package Explorer view under the generated _exe project, right click AlarmSystemSMS.launch and select RunAs and then choose AlarmSystemSMS.

Watch the window on the screen and the Console view. Should look something like the second picture.

Terminate the application by closing the camera-window.

For the send email application: If you get authentication error, make sure SMTP sending is allowed for your email. See troubleshooting at the bottom of the email tutorial:

Step 7: Export As Runnable JAR File

If you want to run the application on a Raspberry Pi, the next step is to export the generated project to a runnable JAR file. To do this, right click the executable project in the package explorer view and choose export. Then select the Runnable JAR file option and hit next. In the following window select the proper launch configuration and export destination. In the export destination, use the browse button so you can put the .jar file a place where you can find it.

Step 8: Prepare Your Raspberry Pi

  • Prepare the SD card with Raspbian (we have tested using NOOBS)
  • Insert the SD card into the Raspberry pi
  • Connect the camera
  • Connect the network
  • Connect a screen and keyboard to the Pi
  • Start the Raspberry pi by connecting the micro USB for power supply.
  • Find out the IP address of your Raspberry Pi. When started up, your Raspberry Pi will report something like "My IP address is 10.10. 15.107"
  • We have tested with Raspbian (default login: pi, password: raspberry), which includes Java by default.

Step 9: Deploy the Application to Your Raspberry Pi

To copy the runnable jar file from your computer over to the Rapsberry Pi follow these steps:

  • Open a terminal on your machine
  • Go to the directory where the generated JAR file is located
  • Type scp alarmsystem.jar pi@
  • You will be asked for the password combination. "pi" is the default user name and "raspberry" the default password
  • Go to the directory where the configuration file is located. Easiest for me is to manually copy the file from the Eclipse editor over to the same directory as the jar file.
  • Type scp motiondetection.ini pi@ is the IP address, replace it by the IP address of your Raspberry Pi.

- You can of course also use a memory stick to copy it over to the Pi

Step 10: Run the Application on Your Raspberry Pi

You are now ready to run the application.

  • Login to the Raspberry Pi either directly or remotely via ssh.
  • Make a new config directory: mkdir config
  • Check if the runnable JAR file and .ini has been copied to the Raspberry Pi
  • Move the .ini file to the config directory: mv config/
  • Type sudo java -jar alarmsystem.jar
  • Watch the output on the console
  • Terminate with ^C

Step 11: Modify Your Application

You can now modify and extend your application as you like. If you want to extend an imported system like this alarm system, you first need to make your own project and duplicate the alarm system to your own project.

Note that to make changes, you need to first make your own project. Then copy/duplicate the application into your newly created project. You can see how this is done here:

Follow the IoT Tuturial trail to get step-by-step descriptions on how to build or modify specific Reactive Blocks applications.

4 People Made This Project!


  • Party Challenge

    Party Challenge
  • Gardening Contest

    Gardening Contest
  • Fandom Contest

    Fandom Contest

95 Discussions

Lars RÖ

4 months ago

Nice tutorial.
No problem to have e-mail sent but sms doesn't work.
This is from the console:
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #ff0000}
Starting runtime...

10:28:59 WARNComponent: No Twilio-accountSid in config file! SMS Will not be sent.

10:28:59 WARNComponent: No Twilio-authorizationToken in config file! SMS Will not be sent

10:28:59 WARNComponent: No Twilio-fromNumber in config file! SMS Will not be sent

Jan 14, 2019 10:28:59 AM org.bridj.BridJ log

INFO: Not enough args for null OpenIMAJGrabber.startSession(int, int, int)
It looks like that no SID or Token or FromNumber is et but it is.

Hopefully you will have a solution or some suggestions.


2 replies
nivenennaLars RÖ

Reply 4 months ago

Hi Lars
Thanks for trying the tutorial. It has been a long time since we uploaded it and we are sorry we have not been able to keep it up to date. Thank you for pointing out an error! The problem was in the init SMS method that uses parameters from the config file. It is case-sensitive and it used the wrong case. We have now fixed the problem. You can download the project again and it should work. Please let us know if it is still a problem. Thank you very much for letting us know:)

nivenennaLars RÖ

Reply 4 months ago

Thanks for trying the tutorial. It has been a few years and we are sorry we have not been able to keep the tutorial up to date. Thanks for pointing out an error. The problem was in the initSMS method where it read a value from the config file (it is uppercase sensitive and it had the wrong case). We have now fixed the problem so if you download the project again, it should work. Again. Thank you very much for letting us know:). Please don´t hesitate to contact us again.


11 months ago

It is a SEVERE error to use a camera as motion detector. It does not work in the darkness and it will give false alarms. If you have for example a tree in front of your window and the wind moves the tree you will have shadows that move on the ground of your flat and the camera will detect this as a motion. I strongly advise to use a REAL motion detector (which are not expensive) instead of a camera.


Answer 1 year ago


Thanks for trying out Reactive Blocks:) I think the pi cam does not behave like a USB camera out of the box. Depending on your precise Raspberry Pi setup, the Pi Cam Module (v1 or v2, standard or noir) will be recognised differently. The newer raspbian OS versions have the V4L2 driver activated by default, in this case there is a chance that OpenIMAJ (which our library is using) will detect and access the camera correctly. (the discussion regarding the V4L2 driver:


2 years ago

question: i tried to launch alarm system email with smtp port: 587 but failed. I got this:

Starting runtime...
2017-02-04 11:19:30 INFO Component:87 - Receiver: *****
2017-02-04 11:19:33 INFO Motiondetection:87 - Motion detector initialized
2017-02-04 11:19:40 INFO SendEmailsBuffered:87 - sending email to receiver *****

2017-02-04 11:19:41 INFO SendEmailsBuffered:87 - Failed to send the following email:
- Subject: Motion sensor alarm detected
- To: *****

- Content:
Hello there,

this message is sent with the Reactive Blocks SDK.

Send Email takes more time that specified. Possible problems: no networks, the server is unavailable.---

2017-02-04 11:19:50 WARN Runtime:299 - \--> Discarded event: SUCCEED_b007 State Machine: ComponentSM

Please reply asap, begging u

3 replies

Reply 2 years ago

Hi. sure you have network from the R-Pi to Usually when there are problems with sending email it is what is described on the bottom of this tutorial: But your error message is different. Make sure you have network from R-Pi. I will need a more complete log-file to see what the error could be.


Reply 2 years ago

hey sorry for the late reply, at last it managed to detect motion and send alert via email BUT only once. after that, whenever motion appeared in front of webcam, nothing happened. i need to terminate it first in order to detect motion again.


Reply 2 years ago

Hi. Are you waiting long enough between the motion appearances? Right now the limiter block is set to 5 minutes which means the application will ignore motions detected within that period. You can change that time interval by right-clicking the limiter block and choose parameters and generics. Try reducing it to, let say 30 000 (which is 30 sec). Let me know if this fixed the problem.



2 years ago

By chance would this work with teh native Raspberry pi Camera?

1 reply

Reply 2 years ago

I have not tested the native camera. I think it will but if not, it should not be too hard to change the code to make it work. Cheers!


2 years ago

I am wondering where do i find the Twilio-receiver-list information and same with the email-receiver-list information.

3 replies

Reply 2 years ago

edit the file config/ See how in step 4. Let me know if you have any issues


Reply 2 years ago

No I understand that but I am using the sms function and was wondering where I can find the code for these function not in the steps more in like the twilio website and such.


Reply 2 years ago


We used the code from (you will find it under documents). You will also find the code if you double click the building block and look into the method.


3 years ago

Thanks for the rply

Like this project about the motion detection i want it to use it for face detection how can i get through it?

Or can i do it with reactive blocks?

3 replies

Reply 3 years ago


Do you use a camera for face detection? Do you have the algorithm for face detection?
If you have the Java code, you can make it into a building block. If it is written in C you can still access the code.



Reply 2 years ago


Yes i' using it for the face detection. I've algorithm for it but dont know how to plant it in building block