From 0c688b1504032a9f8bb1145c99eb020bf5d0d9e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoffer=20Bjo=CC=88rnram?= Date: Thu, 28 Aug 2025 22:04:21 +0200 Subject: [PATCH] Make auto-lock non-blocking --- DoorLock.ino | 43 +++++++++++++++++++++++++++---------------- Lock.h | 28 +++++++++++----------------- 2 files changed, 38 insertions(+), 33 deletions(-) diff --git a/DoorLock.ino b/DoorLock.ino index b7cb895..a006d98 100644 --- a/DoorLock.ino +++ b/DoorLock.ino @@ -2,37 +2,34 @@ #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() ////////////////////////////////////// @@ -40,5 +37,19 @@ void 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; + } } diff --git a/Lock.h b/Lock.h index 60fda53..a69b3f6 100644 --- a/Lock.h +++ b/Lock.h @@ -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; @@ -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"); @@ -23,22 +23,16 @@ 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 } @@ -46,7 +40,7 @@ void loop(){ // loop() method if(current->getVal()==target->getVal()) // if current-state matches target-state there is nothing do -- exit loop() return; - + } // loop - + };