|
| 1 | +/* |
| 2 | + * makesmart_lock.ino |
| 3 | + * |
| 4 | + * Created on: 2021-02-05 |
| 5 | + * Author: cooper @ makesmart.net |
| 6 | + * Thank you for this great library! |
| 7 | + * |
| 8 | + * This example shows how to: |
| 9 | + * 1. define a lock accessory and its characteristics in my_accessory.c |
| 10 | + * 2. get the target-state sent from iOS Home APP. |
| 11 | + * 3. report the current-state value to HomeKit. |
| 12 | + * |
| 13 | + * you can use both: |
| 14 | + * void open_lock(){} |
| 15 | + * and |
| 16 | + * void close_lock(){} |
| 17 | + * |
| 18 | + * at the end of this file to let the lock-mechanism do whatever you want. |
| 19 | + * |
| 20 | + * |
| 21 | + * Pairing Code: 123-45-678 |
| 22 | + * |
| 23 | + * |
| 24 | + * You should: |
| 25 | + * 1. read and use the Example01_TemperatureSensor with detailed comments |
| 26 | + * to know the basic concept and usage of this library before other examples。 |
| 27 | + * 2. erase the full flash or call homekit_storage_reset() in setup() |
| 28 | + * to remove the previous HomeKit pairing storage and |
| 29 | + * enable the pairing with the new accessory of this new HomeKit example. |
| 30 | + * |
| 31 | + */ |
| 32 | + |
| 33 | +#include <Arduino.h> |
| 34 | +#include <arduino_homekit_server.h> |
| 35 | +#include "wifi_info.h" |
| 36 | + |
| 37 | +#define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__); |
| 38 | + |
| 39 | +void setup() { |
| 40 | + Serial.begin(115200); |
| 41 | + wifi_connect(); |
| 42 | + homekit_storage_reset(); |
| 43 | + my_homekit_setup(); |
| 44 | +} |
| 45 | + |
| 46 | +void loop() { |
| 47 | + my_homekit_loop(); |
| 48 | + delay(10); |
| 49 | +} |
| 50 | + |
| 51 | +//============================== |
| 52 | +// HomeKit setup and loop |
| 53 | +//============================== |
| 54 | + |
| 55 | +// access lock-mechanism HomeKit characteristics defined in my_accessory.c |
| 56 | +extern "C" homekit_server_config_t config; |
| 57 | + |
| 58 | +extern "C" homekit_characteristic_t cha_lock_current_state; |
| 59 | +extern "C" homekit_characteristic_t cha_lock_target_state; |
| 60 | + |
| 61 | +static uint32_t next_heap_millis = 0; |
| 62 | + |
| 63 | + |
| 64 | +// called when the lock-mechanism target-set is changed by iOS Home APP |
| 65 | +void set_lock(const homekit_value_t value) { |
| 66 | + |
| 67 | + uint8_t state = value.int_value; |
| 68 | + cha_lock_current_state.value.int_value = state; |
| 69 | + |
| 70 | + if(state == 0){ |
| 71 | + // lock-mechanism was unsecured by iOS Home APP |
| 72 | + open_lock(); |
| 73 | + } |
| 74 | + if(state == 1){ |
| 75 | + // lock-mechanism was secured by iOS Home APP |
| 76 | + close_lock(); |
| 77 | + } |
| 78 | + |
| 79 | + //report the lock-mechanism current-sate to HomeKit |
| 80 | + homekit_characteristic_notify(&cha_lock_current_state, cha_lock_current_state.value); |
| 81 | + |
| 82 | +} |
| 83 | + |
| 84 | +void my_homekit_setup() { |
| 85 | + |
| 86 | + cha_lock_target_state.setter = set_lock; |
| 87 | + arduino_homekit_setup(&config); |
| 88 | + |
| 89 | + |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +void my_homekit_loop() { |
| 94 | + arduino_homekit_loop(); |
| 95 | + const uint32_t t = millis(); |
| 96 | + if (t > next_heap_millis) { |
| 97 | + // show heap info every 30 seconds |
| 98 | + next_heap_millis = t + 30 * 1000; |
| 99 | + LOG_D("Free heap: %d, HomeKit clients: %d", |
| 100 | + ESP.getFreeHeap(), arduino_homekit_connected_clients_count()); |
| 101 | + |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +/* use this functions to let your lock mechanism do whatever yoi want */ |
| 108 | +void open_lock(){ |
| 109 | + Serial.println("unsecure"); |
| 110 | + // add your code here eg switch a relay or whatever |
| 111 | +} |
| 112 | + |
| 113 | +void close_lock(){ |
| 114 | + Serial.println("secure"); |
| 115 | + // add your code here eg switch a relay or whatever |
| 116 | +} |
0 commit comments