My door sends me emails

Simple and cheap Raspberry Pi burglar alarm

[UPDATE] I've rewritten this article for a more modern approach: My door sends me chat messages

The thing I like most about the Raspberry Pi are the GPIO pins. They allow you to break the limits of software and access external hardware. I've always been a software guy but also wanted to control some kind of hardware from the software.

So for my first software<->hardware project I wanted to build a primitive burglar alarm that notifies me whenever the door is opened.

You'll need these things:

If you already have a Raspberry Pi, this project will cost you about 10€

Step 1: The circuit

Circuit of the burglar alarm

It's so simple that even I did it right on my first try

Step 2: The reed switch

The reed switch is the main part of this project because it switches electricity only when a magnet is close to the head. So if the door closes the switch will lose the connection and the raspberry will notice a change from 1 to 0

For testing purposes I just fixed the reed switch and the magnets under it on the door with tape as you can see in the picture below. The two wires of the switch are of course connected to the circuit according to the circuit.

reed switch on the door

Step 3: The software

We're just a few lines of code away from completing this project! For coding I use nodeJS because I like how it works and as a web developer I'm comfortable with JavaScript. Also the package manager (npm) is a real plus for node!

In this example I let the raspberry inform me via email when the door status changes but you can do all kinds of stuff with that like play a sound, post it on twitter (probably not the best idea)

You'll need two packages from npm. Install them with these commands:

  1. (if you don't have nodejs installed and are using raspian) apt-get install nodejs npm
  2. npm install emailjs
  3. npm install rpi-gpio

This is the code: You'll have to change the following things:

var gpio = require('rpi-gpio');
var email   = require("emailjs/email");
var doorpin = 7; //the GPIO port you connected to the cicruit
var server  = email.server.connect({
                user:    "your.username",
                host:    "",
                ssl: true});
var laststate = 1;

gpio.setup(doorpin, gpio.DIR_IN,readInput);

function readInput()
{, function(err, value){
                server.send({ //sending email
                   text:    translateStatus(value),
                   from:    "Door <>",
                   to:      "somebody <>",
                   subject: translateStatus(value)
                }, function(err, message) { console.log(err || message); });
        laststate = value;

  setTimeout(readInput,1000); //recheck door every second

function translateStatus(s){
  if(s==0) return 'The door is now open! '+getTime();
  else return 'The door is now closed! '+getTime();

function getTime(){
        var h = new Date().getHours();
        var m = new Date().getMinutes();
        var s = new Date().getSeconds();
        if(h <10) h = '0'+h;
        if(m <10) m = '0'+m;
        if(s <10) s = '0'+s;
        return h+':'+m+':'+s;

To run the code you'll have to save it in something like dooralert.js and then run it via the following command:

nodejs dooralert.js

If everything works and you open, then close your door you should get a mail like this:

emails are sent to my gmail account whenever the door state changes

Where to go from here

The next thing I want to do with this is connect a webcam to the raspberry via USB and then take a picture every time the door is opened. This will not be so difficult but I'll need to figure out where to put the cam

Tags: alarm api automation burglar door email gpio nodejs raspberry pi

Comment using SSH! Info


Get new posts by email
(~ one email every couple of months & no spam)