Home Monitoring with Raspberry Pi and Node

Home Monitoring with Raspberry Pi and Node.js

Raspberry Pi is a quite powerful, versatile board and it offers a lot for the $35 price. I bought & configured one for a NAS  +  miniDLNA server and it works surprisingly well. Having an old webcam laying around, the idea of a DIY Home Monitoring & Intruder Alert system came to life. Because I wanted to be able to access the video stream from pc & mobile, I went for an implementation with Node.js and a responsive web app.

The project is designed as an end to end solution that, besides offering a live video stream, also actively monitors for movement with the help of a PIR sensor: if an Alarm is triggered, you get a SMS notification on your phone and the snapshots taken during the Alarm time span (customizable) are uploaded via FTP to your server.

Activation and Deactivation of the Alarm Mode

  • there are 2 ways to toggle:
    • from the Web Client user interface
    • with a Button – for convenience reasons: it is faster than connecting from your phone / pc & toggling the Alert Mode checkbox
  • I added a 10 seconds customizable delay which allows the user to move out of the PIR sensor range
  • a Led indicates the Alarm Mode enabled/disabled status

In order to avoid false positives from the PIR motion sensor, I added extra safety checks: a detection counter & detection interval. The Alarm gets triggered when the sensor detects movement 3 times in a row in a 5 seconds timespan (both values configurable in code).

The full source code is available on github.

Project Requirements

Software

  • Raspbian – should come with the Raspberry Pi sd card
  • Node.js – for the server
  • Mjpg_streamer – to generate the video stream
  • Bootstrap – frontend of the web client
  • Twilio for SMS Api
  • Shell scripting – for easy application daeomn start (interactive & background)

Hardware

  • Raspberry Pi
    • I used Model B Revision 2 with Raspbian – any model should be ok, just be careful with the Gpio configuration pin mappings, they can differ
  • Generic USB webcam (compatible with Raspberry Pi & Raspbian)
  • Led & Button
  • PIR motion sensor
    • The one I used is available here https://www.sparkfun.com/products/13285
    • It normally connects to Analog Input (ex. on Arduino); however you can use it with Digital as well if you connect a 10K resistor between VCC & Signal
    • To make things easier you can purchase this sensor https://www.adafruit.com/products/189 and skip the soldering part (+ this one has configurable sensitivity built-in, so you might be able to skip the one implemented in the code)

Schematics

Home Monitoring hardware schematics diagram

Node.js Application

Dependencies

  • express: ^4.12.3
  • ftp: ^0.3.10
  • http-auth: ^2.2.8
  • ini: ^1.3.4
  • pi-gpio: 0.0.7
  • socket.io: ^1.3.5
  • twilio: ^2.3.0

You install them with NPM:

npm install module --save

Generic Application.js

It is the basic application object, defined to be reusable in other projects Contains the basic server code, generic config file read/write operations, generic Init & Execute & Exit methods implementations

Home Monitoring ApplicationHM.js

  • config.ini file
  • default video quality & alert mode settings
  • Twilio sms Api SidTokenTo number, From number
  • Ftp settings
  • Authentication (digest http authentication) – defaults are admin & password šŸ™‚
  • Web Client application
    • accessible from anywhere via port forwarding
    • available also on mobile (responsive web client)
  • Monitoring
    • gets video from Mjpg_streamer server and sends it to the connected clients
    • Mjpg_streamer was used as server, but if you prefer another tool like ffmpeg, you can easily replace it in the start-webcam.sh script

Home Monitoring responsive Web Client

The client application was designed to be accessible on all platforms (pc / tablet / mobile).

Home Monitoring web client application

Video streaming quality settings

By default the 480p at 25fps is enabled (initial settings are loaded from the config.ini file)

My webcam is a low-end 5+ years old 2mp device, but for those of you with better webcams I also added 720p & 1080p

Video resolutions and Fps can be configured from the /static/js/script.js file

ui.quality480p.change(function(){ ConfigUpdateQuality("640x480",25); });
ui.quality720p.change(function(){ ConfigUpdateQuality("1280x720",15); });
ui.quality1080p.change(function(){ ConfigUpdateQuality("1920x1080",5); });

Alarm Alert Mode

Home Monitoring Alarm

If any motion is detected by the PIR sensor, an Alarm is triggered (a sms notification is sent and photo snapshots are uploaded to the ftp server):

  • initial state is loaded from the config.ini file
  • you can enable/disable monitoring from checkbox button in the UI
  • the state of the Alert Mode is shown both in the UI (the checkbox) and by the LED
    • the physical Button can be also used to toggle the Alert Mode
  • all state changes are sent to all connected clients
  • if an Alarm is triggered, the UI checkbox button background will be changed to Red

Connected Clients

Home Monitoring clients list

The dropdown shows a list of all connected clients (connection timestamp & IP) that are currently viewing the video stream.

Shell Scripts

start-app.sh

  • You can start the application in 2 modes:
  • Interactive (for dev / testing): ./start-app.sh
  • Background: ./start-app.sh -background
#!/bin/bash
# application start in interactive or background mode
#arguments:  [-background]
cd /home/pi/Desktop/rpiWorkspace/Node/HomeMonitoring/
if [ "$1" = "-background" ]; then
   sudo nohup node ./App-home-monitoring.js &>log.txt &
else
   sudo node ./App-home-monitoring.js
fi

start-webcam.sh

  • Used by the application to enable/disable video streaming when clients are connected or when an Alarm is triggered by the PIR sensor.
#!/bin/bash
# webcam video stream
# arguments:  [resolution] [port] [fps]
pkill mjpg_streamer
sudo nohup ./mjpg-streamer/mjpg_streamer -i "./mjpg-streamer/input_uvc.so -y -r $1 -f $3 -q 75" -o "./mjpg-streamer/output_http.so -n -p $2" &

Application Execution – session example

Home Monitoring console session

References

One thought on “Home Monitoring with Raspberry Pi and Node.js”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.