Travis Johnston
Published

Door Ajar Sensor

This project checks if your door has been opened and keeps track of when it's opened or closed.

BeginnerShowcase (no instructions)6 hours839
Door Ajar Sensor

Story

Read more

Schematics

Door Ajar Checker

Sensor Setup

Here is the picture showing the photon wired up to collect data for the door

Sensor In Action

This is showing how everything is attached to collect data

Door Wire Set Up

This is showing the wires attached to the door jam to tell if it is open or closed. You can see the wires are applied with tape and lined up so that they can make contact. They are hanging up with just some tape.

Door Opening and Closing

Shown below is a graph of the door being open and closed over a few hours. As you can see the door was open more than when it was closed.

Code

DoorSensor

Arduino
int pinvalue = 1;
int status=0;

int led = D7;

void setup() {
    
    pinMode(led,OUTPUT);
    pinMode(D0,INPUT);
    
    Particle.variable("pinvalue", &pinvalue, INT);
    Particle.variable("status", &status, INT);
    
    Particle.function("pinvalue",pinvalueTracker);

    Particle.subscribe("checkdoor",doorLightValue);
    
}

void loop() {
        status = digitalRead(led);
        pinvalue = digitalRead(D0);
        delay(10000);

    if (pinvalue==0) {
        Particle.publish("checkdoor","open");
        
    }
    if (pinvalue==1) {
        Particle.publish("checkdoor","closed");
    }
}

void doorLightValue(const char *event, const char *data)
{
  if  (strcmp(data,"open")==0) {
   
    digitalWrite(led,HIGH);
    delay(5000);
   
  }
  else if (strcmp(data,"closed")==0) {

    digitalWrite(led,LOW);
    delay(5000);
  }
   
}

int pinvalueTracker(String command) {

    if (command=="on") {
        digitalWrite(led,HIGH);
     
        return 1;
    }
    else if (command=="off") {
        digitalWrite(led,LOW);
        
        return 0;
    }
   
}

Credits

Travis Johnston

Travis Johnston

1 project • 1 follower

Comments