Adam WitteWesley Wolfe
Published

Particle Photon Door Lock and Sensor

A magnetic lock and proximity sensor connected to a particle photon that unlocks your door from a phone and records when the door opens.

IntermediateFull instructions provided4 hours1,821
Particle Photon Door Lock and Sensor

Things used in this project

Hardware components

Photon
Particle Photon
×2
Magnetic Reed Switch
×1
Electric Bolt Lock
×1
12v power supply
×1
5V Relay
×1
Resistor 221 ohm
Resistor 221 ohm
×2
LED (generic)
LED (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×10

Software apps and online services

ThingSpeak API
ThingSpeak API
Maker service
IFTTT Maker service

Story

Read more

Schematics

Magnetic Reed Sensor Circuit

This circuit connects a magnetic reed input to the WKP pin and an LED output to the D0 pin using jumper wires and two 220 ohm resistors.

Door Lock Circuit

Circuit connecting the electric door lock and 12 V battery to the 5V relay using jumper wires and a 4.7 ohm resistor.

Code

Magnetic Reed Switch Code

C/C++
This allowed the photon to send data to Think Speak and publish an event to the Particle cloud whenever the door is opened.
// This #include statement was automatically added by the Particle IDE.
#include <Ubidots.h>

// This #include statement was automatically added by the Particle IDE.
#include <HttpClient.h>

// This #include statement was automatically added by the Particle IDE.
#include <ThingSpeak.h>

TCPClient client;
HttpClient http;

//These values come from Ubidots profile information.
#define VARIABLE_ID "a4f0088407e1381b61f4e681b387569f43f7a16e"
#define TOKEN "qfMt66OyPkjURdKUjzRHliGVVzQmEL"


// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {

    { "Content-Type", "application/json" },
    { "X-Auth-Token" , TOKEN },
    { NULL, NULL } // NOTE: Always terminate headers with NULL
};

http_request_t request;
http_response_t response;

// List your variables for each scenario:
char resultstr[64];
char resultstrAnalog[64];
int unixTime = 0;
int doorStat = 0;
int doorStatOLD = 0;
int rawAnalogStrength = 0;
unsigned long myChannelNumber = 256818;
const char * myWriteAPIKey = "S745NICTJUQMU26R";

// Create the setup for the Particle code.
void setup() {
//Create particle variables    
    Particle.variable("isClosed", &doorStat, INT);
    Particle.variable("whenToggle", &unixTime, INT);
    Particle.variable("analogSignal", &rawAnalogStrength, INT);
// Initialize D0 pin as output   
    pinMode(D0, OUTPUT);
// WKP pin is reed switch input.
    pinMode(WKP, INPUT);
    request.hostname = "things.ubidots.com";
    request.port = 80;
// Run the Thingspeak Application
    ThingSpeak.begin(client);
}

void loop() {
// The returned value from the Core is going to be in the range from 0 to 4095

        rawAnalogStrength = analogRead(WKP);
        if (rawAnalogStrength < 400) {
            doorStatOLD = doorStat; // Save current value for later
            doorStat = 1; //closed
            digitalWrite(D0, LOW);    // Turn OFF the LED pins
            delay(100); 
           
            float voltage = rawAnalogStrength * (3.3 / 4095.0);
            ThingSpeak.writeField(myChannelNumber, 1, voltage, myWriteAPIKey);
            delay(20000); // ThingSpeak will only accept updates every 15 seconds.
            
        } else {
            doorStatOLD = doorStat; // Save current value for later
            doorStat = 0; //open
            digitalWrite(D0, HIGH);   // Turn ON the LED pins
            delay(100); 
            
            Particle.publish("doorStat","0"); // Publishes to IFTT
            
            float voltage = rawAnalogStrength * (3.3 / 4095.0);
            ThingSpeak.writeField(myChannelNumber, 1, voltage, myWriteAPIKey);
            delay(20000); // ThingSpeak will only accept updates every 15 seconds
        
            Particle.publish("door_opened");
        }
//Thanks to Hans for the Thinspeak code base.  
if (doorStatOLD != doorStat) { //If the door has opened or closed since the last reading then
// Send to Ubidots
    request.path = "/api/v1.6/variables/"VARIABLE_ID"/values"; //send binary result
    sprintf(resultstr, "{\"value\":%i}",doorStat); 
    request.body = resultstr;
    http.post(request, response, headers);
    Serial.println(response.body);
    delay(100);
}
}

Lock code

C/C++
This code will allow you to control the lock with the input of (on or off). Also this code subscribes to the Magnetic reed switches doorstatus event, which allows the two photons to communicate this variable.
// First, let's create our "shorthand" for the pins
int lock1 = D3;
int boardLed = D7;
    int i = 0;
void setup(){
    pinMode(boardLed,OUTPUT);
    //  sets the Lock as an output for the Photon
    digitalWrite(boardLed,LOW);
   Particle.subscribe("doorstat", doorstat ,"2e003f001447353136383631");
   
Serial.begin(9600);
pinMode(lock1, OUTPUT);

   // We are also going to declare a Particle.function so that we can turn the Lock on and off from the cloud.
   Particle.function("lock",lockToggle);
   // This is saying that when we ask the cloud for the function "lock", it will employ the function lockToggle() from this app.

   // For good measure, let's also make sure both Lock is off when we start:
   digitalWrite(lock1, LOW);

}
void doorstat(const char *event, const char *data)    {
    i++;
  Serial.print(i);
  Serial.print(event);
  Serial.print(", data: ");
  if (data)
    Serial.println(data);
  else
    Serial.println("NULL");
    Particle.publish("doorstat");
}
void loop()
{
  
}

int lockToggle(String command) {
    
    if (command=="on") {
        digitalWrite(lock1,HIGH);
        Particle.publish("doorstat");
        return 1;
    }
    else if (command=="off") {
        digitalWrite(lock1,LOW);
       Particle.publish("doorstat");
        return 0;
    }
    else {
        return -1;Particle.publish("doorstat");
    }
}

Credits

Adam Witte

Adam Witte

1 project • 0 followers
Wesley Wolfe

Wesley Wolfe

1 project • 1 follower
UNCC Mechanical Engineering Student

Comments