How to make a $30 internet connected door alarm

We run a vacation rental and have always wanted a simple way of knowing when our guests have checked-in. Some owners have a security system or electronic door lock that notifies them. But these often come with a subscription fee and seemed overkill for our purposes.

What I'll describe in this post is how we built a simple device that sends us an email each time the front door is opened or closed. We then use gmail filters to file these emails away most of the time. When we are waiting for a check-in we adjust the filters to bring our attention to it using the LED gmail notifier that I posted about previously.




What you will need:

  • A Nanode (an ethernet capable Arduino compatible device)
  • A Reed Switch
  • A length of 2-core wire to reach from your Nanode to the monitored door
  • An ethernet cable
  • A way to power the Nanode
I bought the Nanode Gateway without Radio ($22 with free shipping from Wicked Device), and an Interlogix Door Terminal Contact ($6.29 with free shipping from Amazon). I'm using a length of unused speaker wire that I had already, but this can often be bought very cheaply in thrift stores like Habitat for Humanity Restore. To power the Nanode I'm using a spare micro-USB and USB power adapter.

The Nanode is going to monitor the reed switch, and communicate with a PHP script to notify you by email of the door opening or closing. Let's start by creating the PHP script. On your server create a monitor.php with the following code:


<?php
    $state = $_GET["state"];
    $subject  = "Door Monitor: $state";
    mail("your_email@example.com",$subject,$state,"From: your_email@example.com\n");
    echo "Done";
?>

You can test this from your browser by opening http://www.example.com/monitor.php?state=open which should return the message "Done" and send you an email - once you've adjusted your email address in the script appropriately.

Now we need to write the script for the Nanode which will monitor the reed switch and call the PHP script when there is a state change.

// door monitor

#include <EtherCard.h>

const byte BUTTON = 2;
int previousState = HIGH;

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x32 };

byte Ethernet::buffer[700];
static uint32_t timer;

// change this to to server where you hosted your monitor.php
char website[] PROGMEM = "example.com";

// called when the client request is complete
static void my_callback (byte status, word off, word len) {
}

void setup () {
  Serial.begin(57600);
  Serial.println("\n[webClient]");
  digitalWrite (BUTTON, HIGH);  // internal pull-up resistor

  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
    Serial.println( "Failed to access Ethernet controller");
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);  

  if (!ether.dnsLookup(website))
    Serial.println("DNS failed");
    
  ether.printIp("SRV: ", ether.hisip);
}

void loop () {
  ether.packetLoop(ether.packetReceive());
  
  if (digitalRead (BUTTON) == HIGH && previousState == LOW) {
    ether.browseUrl(PSTR("/monitor.php?state=closed"), "", website, my_callback);
    Serial.println("closed");
    previousState = HIGH;
  } else if (digitalRead (BUTTON) == LOW && previousState == HIGH) {
    ether.browseUrl(PSTR("/monitor.php?state=open"), "", website, my_callback);
    Serial.println("open");
    previousState = LOW;
  }
}

That's it. Attach the reed switch to the door, and wire it to your Nanode. One side goes to the 3.3V pin, and the other side to digital pin 2 (BUTTON variable in the script). Connect the Nanode to your router and power it on. After a few moments it will be monitoring the door. Open it and you should shortly receive an email with the subject "Door Monitor: open". Close the door and you will receive an email "Door Monitor: closed".

This device obviously has some limitations. If the internet connection goes down, or the power goes out, then there will be no alerts. But those are fortunately unusual events for us, so it meets our needs.