In this lesson, we will use Raspberry Pi , PIR motion sensor and buzzer to make a home intruder alarm system. When an intruder is detected by PIR motion sensor, the buzzer will beep and Raspberry Pi will send alarm message to remote MQTT client device through Internet.
You should have registered a mosquitto MQTT broker server. There are many free only mosquitto server online, you can google it. We use broker.mqtt-dashboard.com in this lesson. 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.
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
Step 1) 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:
Step 3) Download and unzip sample files by running following command:
tar -zxvf intruderDetection.tar.gz
Step 4) Initialize node project
cd intruderDetection npm init
In original source code, we use free mqtt broker server : broker.mqtt-dashboard.com , 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
Result on Raspberry Pi:
Now make some motion near PIR sensor, you will hear buzzer sounds In raspberry Pi terminal, you will see “osoyoo/alert: Intruder detected” message.
Result on MQTT client device:
You can use any MQTT client software or mobile app to receive remote alert message from Raspberry Pi. You can google keyword “MQTT client” to get more information.
If you want to use Raspberry Pi or other linux computer as MQTT client. You can take following steps to test our alarm message:
Step 1)Install mosquitto client software in Pi (you should open another terminal window if you are currently running sudo node index.js command, don’t stop this program)
sudo apt-get install mosquitto-clients
Step 2) subscribe message from broker.mqtt-dashboard.com with topic “osoyoo/#” with following command:
sudo mosquitto_sub -h "broker.mqtt-dashboard.com" -t "osoyoo/#" -v
When PIR sensor detects motion, you will see “osoyoo/alert: Intruder detected” message in client terminal window. This means raspberry pi has published a mqtt message to broker.mqtt-dashboard.com broker and you got such message through subscription.
If you are using other MQTT client device, please subscribe the topic “osoyoo/#” from broker url “broker.mqtt-dashboard.com”. Once human motion is detected by Raspberry Pi, client device will also show ” “osoyoo/alert: Intruder detected” message.