Mobile Deadbolt Monitor

Did you remember to lock the door? This quickie internet of things prototype lets you check if the door is locked using your phone. Uses primarily Arduino, PHP, and Javascript. Scroll down for pics!

ARDUINO


/*
  Door Monitor

  Uses a sensor to monitor the locked/unlocked status of
  a door, lock, or other mechanism. An LED displays the
  current status -- red for locked and green for unlocked.
  When change is detected, it is recorded online.

  * RGB LED on pins D0, D1, D2
  * Hall Effect Sensor on pin D4

  @author Stephen Nomura
  @version 1.0 2016-01-29
*/

/* CONSTANTS & VARIABLES
------------------------------------------------------*/
int rgbLedPins[] = { D0, D1, D2 };
int sensorPin = D4;
int sensorState;
int sensorStatePrev = -1;

/* SETUP
------------------------------------------------------*/
void setup(){
  pinMode(rgbLedPins[0], OUTPUT);
  pinMode(rgbLedPins[1], OUTPUT);
  pinMode(rgbLedPins[2], OUTPUT);
  pinMode(sensorPin, INPUT);
  Particle.variable("lockState",sensorState);
}

/* LOOP
------------------------------------------------------*/
void loop(){
  monitorHallSensor();
  delay(1000);
}

/* FUNCTIONS
------------------------------------------------------*/
void monitorHallSensor(){
  sensorState = digitalRead(sensorPin);
  if(sensorState != sensorStatePrev){ // only trigger on change
    if(sensorState == LOW){ // locked
      setRgbLedByHex("FF0000", rgbLedPins);
    }
    else { // unlocked
      setRgbLedByHex("00FF00", rgbLedPins);
    }
    Particle.publish("lock_record_state");
  }
  delay(50);
  sensorStatePrev = sensorState;
}

int setRgbLedByHex(String hex, int pins[]){
  // error handling
  if( hex.length() != 6 ) return -1;

  // split hex strings, convert to decimal, invert, send to LED
  analogWrite( pins[0], 255 - strtol(hex.substring(0,1),NULL,16) );
  analogWrite( pins[1], 255 - strtol(hex.substring(2,3),NULL,16) );
  analogWrite( pins[2], 255 - strtol(hex.substring(4,5),NULL,16) );
  return 1;
}