HOWTO: Interface wired security sensors using Arduino

I have attached some simple code based on a sketch posted earlier by Mic on this board, and steps to make this work. Hope this can be useful to someone.

  1. Create a bunch of motion detectors in Vera manually

Go to Apps → Develop Apps → Create Device
Enter Device Type: urn:schemas-micasaverde-com:device:MotionSensor:1
Enter Device file: D_MotionSensor1.xml

  1. Reload vera, then reload your browser and note down the ids of the sensors you created

  2. Plug their ids into the sketch and configure the input pins you will use on the Arduino board

  3. Connect contact closure sensors across the GND and input pins

Notes:

  • In my setup the motion sensors were creating too much traffic towards Vera, so I put in a 30 sec delay. 30 secs after being tripped Arduino will report the reset to Vera, and in the meantime it will be silent
  • Note the very different approach to door closure sensors, as the above issue is not applicable
  • I am using the internal pull-up resistors on the Arduino because without this I found I was getting false contact closures when in an open circuit (e.g. door would appear to be constantly toggling in state)
  • I connect the sensors between GND and the input pins, not between 5v and input pins. This is because I am using the internal pull-up resistors on the Arduino. When the contacts are open the input pin floats high (around 1-2v), then when the contact is closed they are grounded and go low.
  • I have it working very well with the Quest PIRs (much faster than my ZWave sensors!). I also plan to add a flow switch to the bath and shower so I can turn the bathroom fan off for 20 mins at bathtime but back on again when we get into the shower.
////
//
// Micasaverde and arduino input output interaction 
// original code by @Mic from the Micasaverde forum
// trigger timeouts added by Chris Birkinshaw (@chrisbirkinshaw)
//
////

#include <SPI.h>
#include <Ethernet.h>
boolean reading = false;

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x19 }; // Be sure this address is unique in your network
IPAddress ip(172,16,0,246); //Address ip valid on your network

// Define the Vera device ids for the sensors (in this example first 5 are motion, 6th is a door contact closure)
#define DEVID1 "151" 
#define DEVID2 "152"
#define DEVID3 "153"
#define DEVID4 "154"
#define DEVID5 "155"
#define DEVID6 "156"

//Numeric Pin where you connect your sensor to Arduino
#define pinDevid1 2 
#define pinDevid2 3
#define pinDevid3 4
#define pinDevid4 5
#define pinDevid5 6
#define pinDevid6 7

// Enable serial console debugging mode
#define DEBUG false

// Setup variables for the sensors
char serverName[] = "172.16.0.2"; //Vera ip address
boolean pinDevid1State = false;
boolean pinDevid2State = false;
boolean pinDevid3State = false;
boolean pinDevid4State = false;
boolean pinDevid5State = false;
boolean pinDevid6State = false;

unsigned long pinDevid1Tripped = false;
unsigned long pinDevid2Tripped = false;
unsigned long pinDevid3Tripped = false;
unsigned long pinDevid4Tripped = false;
unsigned long pinDevid5Tripped = false;
unsigned long pinDevid6Tripped = false;

unsigned long time;

// Setup the timeout for motion sensors (30 secs is suggested to avoid overloading vera with requests)
int timeout = 30000;

// Initialize the Ethernet client library
// with the IP address and port of the server
EthernetServer server = EthernetServer(80); //port 80
EthernetClient client;

void setup() {
  Serial.begin(9600);
  pinMode(pinDevid1, INPUT);
  pinMode(pinDevid2, INPUT);
  pinMode(pinDevid3, INPUT);
  pinMode(pinDevid4, INPUT);
  pinMode(pinDevid5, INPUT);
  pinMode(pinDevid6, INPUT);
  
  // Enable the pullup resitors to protect against false positives on open circuit
  digitalWrite(pinDevid1, HIGH);
  digitalWrite(pinDevid2, HIGH);
  digitalWrite(pinDevid3, HIGH);
  digitalWrite(pinDevid4, HIGH);
  digitalWrite(pinDevid5, HIGH);
  digitalWrite(pinDevid6, HIGH);
  
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  else{
    Serial.println("Ethernet ready local IP:");
    Serial.println(Ethernet.localIP());
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  server.begin();
}

void loop()

{
  SensorDetection();
  }


void SensorDetection(){
      ////
      // Arduino input detection code
      ////
      time = millis();
      
      if (digitalRead(pinDevid1) == HIGH && pinDevid1State == false) 
      {
        if(DEBUG){Serial.println("pinDevid1 is HIGH");}
        pinDevid1Tripped = time;
        if (pinDevid1State == false)
        {
          //Sending request to Vera
          sendToVera(DEVID1, "1");
          pinDevid1State = true;
        }
      }

        if ((time - pinDevid1Tripped) > timeout && pinDevid1State == true)
        {
          pinDevid1State = false;
          //Sending request to Vera
          sendToVera(DEVID1, "0");
        }
      
      if (digitalRead(pinDevid2) == HIGH && pinDevid2State == false) 
      {
        if(DEBUG){Serial.println("pinDevid2 is HIGH");}
        pinDevid2Tripped = time;
        if (pinDevid2State == false)
        {
          //Sending request to Vera
          sendToVera(DEVID2, "1");
          pinDevid2State = true;
        }
      }

        if ((time - pinDevid2Tripped) > timeout && pinDevid2State == true)
        {
          pinDevid2State = false;
          //Sending request to Vera
          sendToVera(DEVID2, "0");
        }
      
      if (digitalRead(pinDevid3) == HIGH && pinDevid3State == false) 
      {
        if(DEBUG){Serial.println("pinDevid3 is HIGH");}
        pinDevid3Tripped = time;
        if (pinDevid3State == false)
        {
          //Sending request to Vera
          sendToVera(DEVID3, "1");
          pinDevid3State = true;
        }
      }

        if ((time - pinDevid3Tripped) > timeout && pinDevid3State == true)
        {
          pinDevid3State = false;
          //Sending request to Vera
          sendToVera(DEVID3, "0");
        }
      
      if (digitalRead(pinDevid4) == HIGH && pinDevid4State == false) 
      {
        if(DEBUG){Serial.println("pinDevid4 is HIGH");}
        pinDevid4Tripped = time;
        if (pinDevid4State == false)
        {
          //Sending request to Vera
          sendToVera(DEVID4, "1");
          pinDevid4State = true;
        }
      }
 
        if ((time - pinDevid4Tripped) > timeout && pinDevid4State == true)
        {
          pinDevid4State = false;
          //Sending request to Vera
          sendToVera(DEVID4, "0");
        }
      
      if (digitalRead(pinDevid5) == HIGH && pinDevid5State == false) 
      {
        if(DEBUG){Serial.println("pinDevid5 is HIGH");}
        pinDevid5Tripped = time;
        if (pinDevid5State == false)
        {
          //Sending request to Vera
          sendToVera(DEVID5, "1");
          pinDevid5State = true;
        }
      }

        if ((time - pinDevid5Tripped) > timeout && pinDevid5State == true)
        {
          pinDevid5State = false;
          //Sending request to Vera
          sendToVera(DEVID5, "0");
        }
      
      
      // We want different behaviour for the door contact closure sensor to avoid toggling
      
      if (digitalRead(pinDevid6) == HIGH && pinDevid6State == false) 
      {
        if(DEBUG){Serial.println("pinDevid6 is HIGH");}
        //Sending request to Vera
        sendToVera(DEVID6, "1");
        pinDevid6State = true;
      }

        if (digitalRead(pinDevid6) == LOW && pinDevid6State == true) 
        {
          if(DEBUG){Serial.println("pinDevid6 is LOW");}
          //Sending request to Vera
          sendToVera(DEVID6, "0");
          pinDevid6State = false;
        }      
    
 }

void sendToVera(String devid, String target){
    if(DEBUG){Serial.println("connecting...");}

  if (client.connect(serverName, 49451)) {
    if(DEBUG){Serial.println("connected to Vera");}

    if(DEBUG){Serial.println("sending request");}
    client.print("GET /data_request?id=lu_variableset&DeviceNum=");
    client.print(devid);
    client.print("&serviceId=urn:micasaverde-com:serviceId:SecuritySensor1&Variable=Tripped&Value=");
    client.print(target);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(serverName);
    client.println("User-Agent: Arduino");
    client.println();
  }
  else {
    if(DEBUG){Serial.println("connection failed");}
  }
  
  // if there are incoming bytes available
  // from the server, read them and print them:
  if(DEBUG){
    if (client.available()) {
    char c = client.read();
    Serial.print(c);
    }
  }

    if(DEBUG){Serial.println();}
    if(DEBUG){Serial.println("disconnecting.");}
    client.stop();
}

Cool, I will go ahead and try this out.

I originally planned using a 433mhz module to send the data to the Vera, but I’ll try your approach and maybe modify it later when I understand all the bits you are doing.

Thanks for sharing.

TommyE