So for my first software<->hardware project I wanted to build a primitive burglar alarm that notifies me whenever the door is opened.
So you'll need these things:
- Raspberry Pi
- A reed relay (magnet switch)
- Some old IDE cable
- 1 x 10kΩ resistor
If you already have a Raspberry Pi, this project will cost you about 10€
Step 1: The circuit
Here's the diagram:
This is a very simple circuit that even I could build :D
This is what my first self-soldered circuit looks like:
note that I used the IDE cable to connect the GPIO pins to the board. This info graphic helped me a lot!
![]() |
| Raspberry connected to the circuit via IDE cable Note: You see more than one resistor in the picture above because this was my first version which had two. I managed to make the circuit even simpler thanks to George Toderici |
It's not pretty but it does its job!
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 (the cables on the left in the picture above)
![]() |
| Reed switch with a magnet under it hold in place professionally by some tape |
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:
This is the code:
You'll have to change the following things:
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:
- (if you don't have nodejs installed and are using raspian) apt-get install nodejs npm
- npm install emailjs
- npm install rpi-gpio
This is the code:
You'll have to change the following things:
- If you're using a different pin than I in my circuit diagram, change the doorpin value
- Change your email credentials (user, password, host if you're not using gmail)
- Change the "from:" and "to:" addresses in the readInput function
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",
password:"YourPassword",
host: "smtp.gmail.com",
ssl: true});
var laststate = 1;
gpio.setup(doorpin, gpio.DIR_IN,readInput);
function readInput()
{
gpio.read(doorpin, function(err, value){
if(laststate!=value)
{
console.log(translateStatus(value));
server.send({ //sending email
text: translateStatus(value),
from: "Door <youremail@gmail.com>",
to: "somebody <youremail@gmail.com>",
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.jsIf everything works and you open, then close your door you should get a mail like this:




Really impressed! Did you create a separate email account just for your Door Alarms? That seems like the easiest way to give it very high priority on a phone or tablet etc.
ReplyDeleteI would like to try to implement your solution but maybe change the email code to append a line in a text file on my NAS or something.
Also, all that effort & only scotch tape to hold it up?? Have you used glue or epoxy now that you know it works?
Thanks! For testing purposes I used my own gmail account but then I gave it a seperate account.
DeleteI used the tape just the first days and then changed it to a thin double sided scotch tape so it looks like the wire is just sticking to the wall
I am following your steps to do the same with a switch and pi connected to my chicken coop. The coop is automated and I'd like to get an email when the door opens/closes in the morning/night.
ReplyDeleteI am new to this and am getting this error when I run the script
var gpio = require('rpi-gpio');
^
SyntaxError: invalid syntax
Maybe this makes sense to you?
I am running wheezy-raspbian and have the switch running using this adafruit tutorial
http://learn.adafruit.com/adafruits-raspberry-pi-lesson-12-sensing-movement/overview
Thanks for your help (in advance).
And if your not interested - you can tell me to buzz off and I'll attempt to do more than copy/paste someone else's project. Maybe I'll do some more research.
this looks like you're not trying to start the script with nodejs
Deletedid you save the script to (eg.) dooralert.js and then run it via: nodejs dooralert.js
Excellent. I will try that soon. Thanks for the reply. And this is exactly what I did not do.
DeletePerfect.. no longer getting an error - but.
DeleteI'm not registering a change of state for the switch? It is always showing "open"?
I followed this tutorial and wired the board as they did and have their python script running flawlessly.
http://learn.adafruit.com/adafruits-raspberry-pi-lesson-12-sensing-movement/software
Any thoughts? And I did change the doorpin value to my gpio pin.
Again! Thanks for your help!