In this project, we will use Raspberry Pi and motion sensor to make a home intruder alarm system. Which can send alarm message through internet and email.

Perquisite :

You should have registered a mosquitto MQTT broker server. There are many free only mosquitto server online, you can google it. You can also check instruction in http://osoyoo.com/2016/09/07/how-to-install-mosquitto-mqtt-server-on-linux/ and install your own MQTT broker. Write down the IP address or Domain name of your mosquitto server. We will use it later.

Parts and device:

1 x Raspberry Pi 2 or 3

1 x PIR motion sensor

1 x buzzer

breadboards and jumper wires.

Circuit connection Graph:



PIR sensor signal pin(yellow pin) connect to Pi GPIO 17

Buzzer signal pin(purple pin) connect to PI GPIO 18

PIR sensor red pin to 5V

PIR sensor and buzzer black pin to GND

Software installation

Step 1) In your mosquitto broker server, subscribe a topic “securitytest” by running following command:sudo mosquitto_sub -h localhost -t “securitytest” -v


Step 2) Install Node.js on Raspberry Pi by running following commands in raspberry pi terminal:

curl -sLS https://apt.adafruit.com/add | sudo bash

sudo apt-get install node

If node.js is installed successfully, you should see node.js version by running following command:

node -v

Step 3) Download and unzip sample files by running following command:

wget http://osoyoo.com/driver/intruderDetection.tar.gz

tar -zxvf intruderDection.tar.gz

Step 4) Initialize node project


cd intruderDetection

npm init


In original source code, we use free mqtt broker server : test.mosquitto.org , If you have your own mosquito broker,

please type command nano index.js to edit index.js file and change the ip address (in line 12) to your mosquitto broker server IP.

Step 5) Install GPIO communication module onoff:

npm install onoff –save

Step 6) Install Email module nodemailer

npm install nodemailer –save


Step 7) Start node.js program by running following command:

sudo node index.js

Now make some motion near PIR sensor, you will hear buzzer sounds  In raspberry Pi terminal, you will see “osoyoo/alert: Intruder detected” message.

in another Linux computer, you can test subscribe above alert information.

If you don’t have mqtt client, run following command to install mosquitto client first

sudo apt-get install mosquitto-clients

now you test subscribe mqtt broker  in test.mosquitto.org with topic “revspace/sensors/# with following command:

sudo mosquitto_sub -h test.mosquitto.org -t “revspace/sensors/” -v

You should see many temperature,humidity and other sensor information rolling in your screen. that means you has connected and registered with test.mosquitto.org mqtt broker.

now quite above subscribing by typing ctrl-C, and subscribe another top by typing following command:

sudo mosquitto_sub -h test.mosquitto.org -t “osoyoo/#” -v

When PIR sensor detects motion,   you will see “osoyoo/alert: Intruder detected” message  too. This means raspberry pi has published a mqtt message to test.mosquitto.org broker and you got such message through subscription.

In next article, we will use meteor to show above alert information into a web page when PIR sensor detects motion.


Leave a Reply

WordPress spam blocked by CleanTalk.
Bombax Theme designed by itx