Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions DoorLock.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,54 @@
#include "Doorbell.h"
#include "Lock.h"

int doorbellPin = 15; //input pin from doorbell, the other cable must go to GND
int relayPin = 22; // choose a pin to control the relay
int doorbellPin = 12; // input pin from doorbell, the other cable must go to GND
int relayPin = 13; // choose a pin to control the relay
int openTime = 3000; // time in ms the lock should remain open before auto-closing

DoorLock *doorlock; // define doorlock globally to be able to reference it in void loop
unsigned long unlockTime = 0; // variable to store time when doorlock was opened

void setup()
{
Serial.begin(115200);
homeSpan.setLogLevel(1);

homeSpan.setPairingCode("11122333");
homeSpan.setQRID("111-22-333");
homeSpan.setWifiCredentials("WIFI_NAME","WIFI_PASSWORD"); // change credentials

homeSpan.begin(Category::Other,"Intercom");

// set pins as dry contact
pinMode(relayPin, OUTPUT);


new SpanAccessory();
// set pins as dry contact
pinMode(relayPin, OUTPUT);

new SpanAccessory();
new Service::AccessoryInformation();
new Characteristic::Identify();
new Characteristic::Name("Doorbell");
new DoorBell(doorbellPin);

new SpanAccessory();
new SpanAccessory();
new Service::AccessoryInformation();
new Characteristic::Identify();
new Characteristic::Name("Lock Button");
new DoorLock(relayPin);
doorlock=new DoorLock(relayPin);

} // end of setup()

//////////////////////////////////////

void loop()
{
homeSpan.poll();


if(doorlock->current->getVal() == 0){ // 0 means that doorlock is open
if(unlockTime == 0){ // save current time if not already set
unlockTime = millis();
}

if(millis() - unlockTime >= openTime){ // when openTime amount of milliseconds passed, lock again
doorlock->target->setVal(1); // 1 means that doorlock should lock
doorlock->update();
unlockTime = 0; // reset to 0 as doorlock is now locked
}

}else if(unlockTime != 0){ // if door was locked manually before openTime had passed, reset saved unlock time
unlockTime = 0;
}
}
28 changes: 11 additions & 17 deletions Lock.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
struct DoorLock : Service::LockMechanism { // Door LockMechanism

SpanCharacteristic *current; // reference to the Current Door State Characteristic
SpanCharacteristic *target; // reference to the Target Door State Characteristic
SpanCharacteristic *current; // reference to the Current Door State Characteristic
SpanCharacteristic *target; // reference to the Target Door State Characteristic

int lockPin;

Expand All @@ -11,7 +11,7 @@ struct DoorLock : Service::LockMechanism { // Door LockMechanism

current=new Characteristic::LockCurrentState(1); // initial value of 1 means closed
target=new Characteristic::LockTargetState(1); // initial value of 1 means closed

Serial.print("Configuring Door LockMechanism"); // initialization message
Serial.print("\n");

Expand All @@ -23,30 +23,24 @@ boolean update(){ // update() method

if(target->getNewVal()==0){ // if the target-state value is set to 0, HomeKit is requesting the door to be in open position
LOG1("Opening Door\n");
current->setVal(0); // set the current-state value to 0, which means "opening"
LOG1("Current set to 0, opening the gate for 1 second\n");
digitalWrite(lockPin, HIGH); // close the contact on the relay
delay(1000); // wait for a second
LOG1("Door Opened\n");
target->setVal(1); // lock again
LOG1("Target set to 1, closing the gate\n");
digitalWrite(lockPin, LOW); // open contact on the relay
current->setVal(1);
LOG1("Door Closed\n");
digitalWrite(lockPin, HIGH); // close the contact on the relay
current->setVal(0); // set the current-state value to 0, which means "opening"
LOG1("Door Open\n");
} else {
LOG1("Closing Door\n");
digitalWrite(lockPin, LOW);
current->setVal(1); // set the current-state value to 1, which means "closing"
LOG1("Door Closed\n");
LOG1("Door Closed\n");
}

return(true); // return true
}

void loop(){ // loop() method

if(current->getVal()==target->getVal()) // if current-state matches target-state there is nothing do -- exit loop()
return;

} // loop

};