Justin ShieldsMelissa Rincon
Published

Automatic Deadbolt MEGR 3171 IoT Project

It is an automated deadbolt that can be controlled by your phone via an app or manually at the door via a push button.

BeginnerFull instructions provided6 hours612
Automatic Deadbolt MEGR 3171 IoT Project

Things used in this project

Hardware components

Photon
Particle Photon
×2
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
Jumper wires (generic)
Jumper wires (generic)
×9
5 mm LED: Green
5 mm LED: Green
×1
High Brightness LED, White
High Brightness LED, White
×1
portable usb charger
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
deadbolt
×1

Software apps and online services

Maker service
IFTTT Maker service

Hand tools and fabrication machines

knife

Story

Read more

Schematics

Circuits

Servo photon schmatic

this allows the photon to work the servo motor in order to work the deadbolt

Controller photon schmatic

This is how the controller and indicator photon is wired

Data

Code

Lock and Unlock

C/C++
This is the code used to activate the servo motor to engage or disengage the deadbolt lock. This code also allows the photon to communicate with another photon so that they are both on the same page and understand where the lock is at.
int valLock = D0;
Servo serv; 
//int lock;
int nLock; 
int boardLed; 
int valUnlock = 90;
int button = D1;
void setup() {
pinMode(boardLed,OUTPUT);
pinMode(valLock,OUTPUT);
pinMode(button, INPUT_PULLUP);
}


void loop() {
//Lock function
{int lock(String) ;
    //Moves servo to lock position
    serv.write(valLock);
    //Lock number changes to 1
    nLock = 1;
    //Publishes "Locked"
    Particle.publish("Reese_door_lock","Locked");
    //Switches the external LED off
    digitalWrite(boardLed,LOW);
}
}
void cycle(){

//Unlock function
{int unlock(String) ;
    //Moves servo to unlock position
    serv.write(valUnlock);
    //Lock number changes to 0
    nLock = 0;
    //Publishes "Unlocked"
    Particle.publish("Reese_door_lock","Unlocked");
    //Switches the board LED on
    digitalWrite(boardLed,HIGH);
}
    //If the button is pressed, the locked switches between locked and unlocked
    if (analogRead(button)<100) {
        //Lock sequence
        if (nLock==0) {
            //Moves servo to locked position
            serv.write(valLock);
            //Changes lock number to 1
            nLock = 1;
            //Publishes "Locked"
            Particle.publish("Reese_door_lock","Locked");
            //Switches the board LED off
            digitalWrite(boardLed,LOW);
            delay(1000);
        }
        //Unlock sequence
        else if (nLock==1) {
            //Moves servo to unocked position
            serv.write(valUnlock);
            //Changes lock number to 0
            nLock = 0;
            //Publishes "Unlocked"
            Particle.publish("Reese_door_lock","Unlocked");
            //Switches the board LED on
            digitalWrite(boardLed,HIGH);
            delay(1000);
        }
}}
void myHandler(const char *event, const char *data) {
    //When the "Lock" event data is published with the sucribed with "rosco_door_lock", the lock switches bewtween lock and unlock
    if (strcmp(data,"Lock")==0) {
        //Lock sequence
        if (nLock==0) {
            //Moves servo to lock position
            serv.write(valLock);
            //Changes lock number to 1
            nLock = 1;
            //Publishes "Locked"
            Particle.publish("Reese_door_lock","Locked");
            //Switches board LED off
            digitalWrite(boardLed,LOW);
        }
        //Unlock sequence
        else if (nLock==1) {
            //Moves servo to unlock position
            serv.write(valUnlock);
            //Changes lock number to 0
            nLock = 0;
            //Publishes "Unlocked"
            Particle.publish("Reese_door_lock","Unlocked");
            //Switches the board LED on
            digitalWrite(boardLed,HIGH);
        }
    }
}

Controller

C/C++
This code takes works with communication from the other photon to activate the correct corresponding LED indicator light, as well as communicating with the servo based photon to lock or unlock when commanded via a push button or an application.
//Power to switch is on pin A0
int power = A0;
//Button on pin A1
int button = A1;
//Lock LED is on pin D0
int ledLock = D0;
//Unlock LED is on pin D1
int ledUnlock = D1;
//Board LED is on pin D7
int boardLed = D7;
//Assigns variables to lock(1) or unlock(0)
int nLock = 0;

void setup() {
    //Describes outputs and input of pins
    pinMode(power,OUTPUT);
    pinMode(button,INPUT);
    pinMode(boardLed,OUTPUT);
    pinMode(ledLock,OUTPUT);
    pinMode(ledUnlock,OUTPUT);
    
    //Suscribes to other photon
    Particle.subscribe("Reese_door_lock", myHandler);
    
    //Powers the pin connected to the switch 
    analogWrite(power,4095);
    
    //both LEDs turn on when switch is powered
    digitalWrite(ledLock,HIGH);
    digitalWrite(ledUnlock,HIGH);
    
    //Flashes board LED when setup is complete
    digitalWrite(boardLed,HIGH);
    delay(300);
    digitalWrite(boardLed,LOW);
    delay(300);
    digitalWrite(boardLed,HIGH);
    delay(300);
    digitalWrite(boardLed,LOW);
    delay(300);
    digitalWrite(boardLed,HIGH);
    delay(300);
    digitalWrite(boardLed,LOW);
    
    //Lock LED turns off to leave the Unlock LED on
    digitalWrite(ledLock,LOW);
}

void loop() {
   //If the button is pressed, "Lock" is published and the board LED flashes 3 times
    if (analogRead(button)<100) {
            Particle.publish("rosco_door_lock","Lock");
            digitalWrite(boardLed,HIGH);
            delay(100);
            digitalWrite(boardLed,LOW);
            delay(100);
            digitalWrite(boardLed,HIGH);
            delay(100);
            digitalWrite(boardLed,LOW);
            delay(100);
            digitalWrite(boardLed,HIGH);
            delay(100);
            digitalWrite(boardLed,LOW);
            delay(1000);
    }
}

void myHandler(const char *event, const char *data) {
    //When "Unlocked" is recieved from "mbenne_door_lock", the unlocked LED turns on and the locked LED turns off
    if (strcmp(data,"Locked")==0) {
        digitalWrite(ledUnlock,LOW);
        digitalWrite(ledLock,HIGH);
    }
    //When "Locked" is recieved from "mbenne_door_lock", the locked LED turns on and the unlocked LED turns off
    else if(strcmp(data,"Unlocked")==0) {
        digitalWrite(ledLock,LOW);
        digitalWrite(ledUnlock,HIGH);
    }
}

Credits

Justin Shields

Justin Shields

1 project • 1 follower
Melissa Rincon

Melissa Rincon

1 project • 0 followers

Comments