Introduction: 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 www.bitreactive.com/installation/,
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/alarm-system.properties. 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: http://reference.bitreactive.com/tutorials/selfie...

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: http://reference.bitreactive.com/tutorials/send-email.html

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@10.10.15.107:~/
  • 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 alarm_system.properties 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@10.10.15.107:~/

10.10.15.107 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 alarm_system.properties 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: http://reference.bitreactive.com/tutorials/selfie-with-mqtt.html

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

Comments

author
eezaysn made it!(author)2017-02-03

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: *****@gmail.com
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 *****@gmail.com

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

- Content:
Hello there,

this message is sent with the Reactive Blocks SDK.


---
Reason:
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

author
nivenenna made it!(author)2017-02-05

Hi. sure you have network from the R-Pi to gmail.com? Usually when there are problems with sending email it is what is described on the bottom of this tutorial: http://reference.bitreactive.com/tutorials/send-email.html. 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.

author
eezaysn made it!(author)2017-02-20

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.

author
nivenenna made it!(author)2017-02-21

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.

Cheers!

author
Stephen+LEE made it!(author)2016-10-31

It's very cool. I made a similar JPEG camera. I'm going to add openCV function.

https://www.instructables.com/id/Smart-JPEG-Camera-for-Home-Security/

5814bc4315be4d5ba200082d.jpeg
author
goldswimmerb made it!(author)2016-10-04

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

author
nivenenna made it!(author)2016-10-04

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!

author
dragslicker made it!(author)2016-06-06

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

author
nivenenna made it!(author)2016-06-06

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

author
dragslicker made it!(author)2016-06-06

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.

author
nivenenna made it!(author)2016-06-07

OK:)

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

author
HaroonR11 made it!(author)2016-05-18

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?

author
nivenenna made it!(author)2016-05-18

Hi

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.

Anne

author
HaroonR11 made it!(author)2016-05-24

Hi

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

author
nivenenna made it!(author)2016-05-24

Is it written in Java?

author
HaroonR11 made it!(author)2016-05-28

Hello

Did u get it?

author
HaroonR11 made it!(author)2016-05-24

vid= videoinput('winvideo',2);

set(vid,'FramesPerTrigger'I);

set(vid,'TriggerREpeat',inf);

set(vid,'ReturnedColorspace','rgb');

set(vid,'Timeout',50);

vid.FrameGrabInterval=5

triggerconfig(vid,'manual');

start(vid);

for ii=1:50

FaceDetect=vision.CascadeObjectDetector('FrontalFaceLBP');

I=getsnapshot(vid);

I = imread('p2.jpg')

trigger(vid);

%figure(I);

imshow(I);

BB=step(FaceDetect,I);

if (numel(BB)==0)

continue;

end;

rectangle('position',BB,'LineWidth',4,'LineStyle','-','EdgeColor','b');

title('EyesDetection');

Face=imcrop(I,BB);

EyeDetect=vision.CascadeObjectDetector('EyePairSmall');

BB2=step(EyeDetect,Face);

b=size(BB2);

if(numel(BB2)==0)

disp=('Not detected');

continue;

else

disp('Detected');

end;

Eyes=imcrop(Face,BB2);

Figure(3);

imshow(Eyes);

Eyes_BW=im2bw(Eyes,0.3);

Figure(I);

imshow(Eyes_BW);

se=strel('square',1);

Eyes_Dilated=imdilate(Eyes_BW,se);

meanIntensity=mean2(Eyes_BW);

disp(meanIntensity)

if meanIntensity>0.92

imshow(I),title('Eyes Closed');

disp('Eye Closed')

else

imshow(I),title('Eyes Open');

disp('Eye Open')

end

figure(I);

imshow(Eyes_Dilated),title('Dilated');

end;

stop(vid);

delete(vid);

clearall

## this is the code which i'm running on matlab

author
HaroonR11 made it!(author)2016-05-18

i need some help in my project which is detection of eye blinking system for which i've upload a video file to check the the eye blinking actually i want to know that how to upload that video file in raspberry pi using matlab to make it standalone thanks

author
nivenenna made it!(author)2016-05-18

Hi
I don´t understand your question. Are you thinking about using anything from this project? If not, I don´t think I can help you as I don´t have any experience with Matlab.

author
IbrahimK33 made it!(author)2016-05-01

i am so thankful for ur reply mrs.nivenenna

1-i had moved the file by using a USB to the raspberry pi.

2- you can see in the picture there is a problems with my code.

3-why we are using the Ethernet cable.

thank you ,i must do it. :) with your help Mrs.Nivenenna.

20160501_225935.jpg20160501_225927.jpg
author
nivenenna made it!(author)2016-05-01

Hmm. I see. Hard to say. I can think of these three reasons on top of my head:
1. The Java version on your Raspberry Pi is too old compared to the one your computer
2. The file was corrupted when you copied it. I guess you guys already have been playing around with this one..
3. When you exported the project as a runnable JAR file from Eclipse. Are you sure you selected runnable JAR? ( there is an option for just JAR file too)

author
IbrahimK33 made it!(author)2016-05-01

and how to do this step,what it means


  • Go to the directory where the generated JAR file is located
author
nivenenna made it!(author)2016-05-01

When you generate the code from Eclipse you will get a .jar file. This file will be placed in a folder on your computer that you have specified. Folder and directory is really the same thing. If you did not specify a directory/folder, it will end up in the the default folder which is on my computer in documents/workspace folder. Alternatively, you can search in your files on your computer to see if you can find in what directory/folder it is. When you know where it is you are ready for the next step: copy that file over to the Raspberry PI. You can of course just use a memory stick, but the tutorial does it remotely:

From the terminal window on your computer, try locate the file from that directory. the command cd is used to change directory. If you google:" how to change directory from terminal" you will see many good explanations on this.
Here is another good explanation: http://trevorappleton.blogspot.no/2014/03/remotely-copy-files-to-and-from-your.html

author
IbrahimK33 made it!(author)2016-05-01

hi all,i am a student.

i had made the 8 steps and it works on my laptop ,it had sent a message to my email,but i need help in the last steps .to use the raspberry pi. cam

first the computer must be joined with the raspberry by an Ethernet cable.

2,what to do ??

3- i can send a video stream from the raspberry pi to the internet ,i cant use that

plz a help ,any ethernet cable can be used or there must be a special one.

author
nivenenna made it!(author)2016-05-01

Hi
Thanks for trying our Reactive Blocks. You should be able to use any ethernet cable. Just hook up the Raspberry PI to the Internet. Then you should be able to access it from any computer that is logged on to the Internet. But I am not sure if I understand what you are saying after 3.

author
k3nob1 made it!(author)2016-04-26

thanks for a amazing project I do have a question

what about the hardware? ....require a motion detection sensor? bcose i read everything about the software and the code but i think i missing the hardware part ...(sorry im a newby)

thank you in advance

author
nivenenna made it!(author)2016-04-26

Thanks for your feedback. No you don´t need sensor to detect motion. Only a camere. The application uses an image detection algorithm to detect the changes in the image. If you run the application on your laptop the application uses the built in camera of your computer. If you run the application on a Raspberry Pi, you need to attach a USB camera.

author
dbrett21 made it!(author)2016-02-24

when i was downloading eclipse it said that i did not have java installed even though i had installed the latest version? help please!

author
nivenenna made it!(author)2016-02-25

Verify that Java in installed in the computer [1]. Or, make Eclipse

use a specific version of Java [2].



[1] http://www.mkyong.com/java/how-to-check-current-java-version-installed-in-your-computer/
[2] https://wiki.eclipse.org/Eclipse.ini

author
dbrett21 made it!(author)2016-02-23

I have that exact same camera and have never found anything like this that would work for it until now

author
nivenenna made it!(author)2016-02-24

Nice:) Did you try?

author
dbrett21 made it!(author)2016-02-24

I was going right now to do it but I just found out that my computer had malware?

author
dbrett21 made it!(author)2016-02-24

I haven't yet but i will later today

author
LazarusB2 made it!(author)2016-02-12

Hi, i have done this. But it only sends an email after the very first motion detection and after that it shows that motion is detected but does not send emails. Its on the pc, not deployed into the RP yet

author
nivenenna made it!(author)2016-02-13

Hi LazarusB2.

For how long did you run the application? The limiter may have prevented consecutive emails. Let me look into this on Monday and return to you then! Thanks for trying out Reactive Blocks.

Cheers!

author
nivenenna made it!(author)2016-02-15

Hi LazarusBS

Notice the P in the left corner of the limiter building block. If you right click the block and select parameters and generics you will see that you can set the duration of how long the application shall wait before creating another message. The default is 5 minutes, so you need to wait for a while unless you go ahead and use a different value. I just tried with 30 seconds. You need to rebuild the application after you change the value. Hope this solves the problems. Thanks to your feedback I also discovered a timeout that is set to low in this application (Perhaps you also notice the failed to send message even though the message is sent, caused by this). You can fix this if you go into the createEmail method and set int timeout = 10000; I will fix this for the downloadable version.

Cheers!

author
lazarusb1 made it!(author)2016-02-15

Thanks a lot nivenenna, I have found all the things you have mentioned. I will use them and let you know what happened.

author
AiyshaJ made it!(author)2016-01-26

Raspberry PI based security Camera using Motioneye https://ashi15.wordpress.com/2016/01/18/raspberry-pi-based-security-camera-network-using-motioneye/

author
Minieee86 made it!(author)2015-11-24

i have test it on my computer with Windows and it run smoothly.. but when i try on the raspberry pi i get this kind of error..

12277125_1640544489548853_1314690322_n.jpg
author
nivenenna made it!(author)2015-11-24

The problem is connected to memory available for the application on your raspberry pi. Some suggestions:

Play with the VM settings to adjust the available memory. When running the application on your raspberry pi use java -Xmx..... to adjust the memory.

Another idea is to try to take less images by editing the "getTimer" operation to influence how often an image is taken. Change for example from 500 to 1000.

Let us know if that solves the problem...

author
nivenenna made it!(author)2015-11-26

Are you using a Raspberry B+? Just tested it and it works. Could be that your camera resolution is too high maybe.

author
Minieee86 made it!(author)2015-11-23

hi.. can you help me please? i got same error in the raspberry pi when i try to run the jar file.. it can display but cannot capture the motion and send notification to my email.. btw im using raspberry pi model b+

author
nivenenna made it!(author)2015-11-23

Hi. Not sure what same error you are referring to? Did you test it on your laptop first? Can you copy paste the error you get, then it will be easier to figure out the problem.

author
GuillaumeD11 made it!(author)2015-11-14

i am trying to change the sms feature for a send email one but the step by step email config give me a lot of error both these import cannot be resolve:

import com.bitreactive.library.email.sendemail.SendEmail;

import com.bitreactive.library.email.sendemail.SendEmail.Email;

author
nivenenna made it!(author)2015-11-15

Let me go through the steps myself and see what the problem is. Will get back to you by tomorrow

author
nivenenna made it!(author)2015-11-15

I assume you started by creating your own project and then copying the application into your newly created project?

author
GuillaumeD11 made it!(author)2015-11-15

yes i started by creating my own project to try the smtp and i wanted to add it to the project after but i think that the tutorial on how to make the smtp work is out of date since the library for Email is not the same version as in the turorial. I followed every step and i get the error from the import that i told you as well as some error in the program that it cannont find the Email class and the Sendemail one too

i am a student of computer programming but my knowledge is not enough to figure out what is wrong with the program the only thing i could think about was creating new classes but i hit the same problem i am not sure what i am supose to put in those class so everything work together

author
nivenenna made it!(author)2015-11-18

So then you added block “Send Email” from library “Send SMTP email” to send an email instead of a SMS.

As you noticed, the classes Email and SendEmail are not available right away within the Java code of your system. This is because eclipse does not automatically add the necessary import statements. However eclipse will suggest the classes you have to import: So when you click for example the red underlined “Email” within the Java file, eclipse suggests to import the proper class, which you just click to add the import statement. Do the same with “SendEmail”. ( you can copy the necessary Java Code from the send Email tutorial: http://reference.bitreactive.com/tutorials/send-email-java-code.html)

We will create a system sending emails instead of SMS and make it available here on Instructables shortly. Thanks for the inspiration!

author
GuillaumeD11 made it!(author)2015-11-19

ok i will try this and i will keep you updated. THank you for your help.

author
nivenenna made it!(author)2015-11-20

Hi

Recommend you download the newly published project! It is much smoother and easier! Should let you send email right out of the box! You simply put in your email credentials in the alarm_system properties file and build and run the application. Just follow the modified instructable:)