|
| 1 | +/* |
| 2 | + * switch.ino |
| 3 | + * |
| 4 | + * This example shows how to: |
| 5 | + * 1. define a switch accessory and its characteristics (in my_accessory.c). |
| 6 | + * 2. get the switch-event sent from iOS Home APP. |
| 7 | + * 3. report the switch value to HomeKit |
| 8 | + * |
| 9 | + * Created on: 2020-05-15 |
| 10 | + * Author: Mixiaoxiao (Wang Bin) |
| 11 | + * |
| 12 | + */ |
| 13 | + |
| 14 | +#include <Arduino.h> |
| 15 | +#include <arduino_homekit_server.h> |
| 16 | +#include "wifi_info.h" |
| 17 | + |
| 18 | +#define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__); |
| 19 | + |
| 20 | +void setup() { |
| 21 | + Serial.begin(115200); |
| 22 | + wifi_connect(); // in wifi_info.h |
| 23 | + my_homekit_setup(); |
| 24 | +} |
| 25 | + |
| 26 | +void loop() { |
| 27 | + my_homekit_loop(); |
| 28 | + delay(10); |
| 29 | +} |
| 30 | + |
| 31 | +//============================== |
| 32 | +// Homekit setup and loop |
| 33 | +//============================== |
| 34 | + |
| 35 | +// access your HomeKit characteristics defined in my_accessory.c |
| 36 | +extern "C" homekit_server_config_t config; |
| 37 | +extern "C" homekit_characteristic_t cha_switch_on; |
| 38 | + |
| 39 | +static uint32_t next_heap_millis = 0; |
| 40 | + |
| 41 | +#define PIN_SWITCH 2 |
| 42 | + |
| 43 | +//Called when the switch value is changed by iOS Home APP |
| 44 | +void cha_switch_on_setter(const homekit_value_t value){ |
| 45 | + bool on = value.bool_value; |
| 46 | + cha_switch_on.value.bool_value = on;//sync the value |
| 47 | + LOG_D("Switch: %s", on ? "ON" : "OFF"); |
| 48 | + digitalWrite(PIN_SWITCH, on ? LOW : HIGH); |
| 49 | +} |
| 50 | + |
| 51 | +void my_homekit_setup() { |
| 52 | + pinMode(PIN_SWITCH, OUTPUT); |
| 53 | + digitalWrite(PIN_SWITCH, HIGH); |
| 54 | + |
| 55 | + //Add the .setter function to get the switch-event sent from iOS Home APP. |
| 56 | + //The .setter should be added before arduino_homekit_setup. |
| 57 | + //HomeKit sever uses the .setter_ex internally, see homekit_accessories_init function. |
| 58 | + //Maybe this is a legacy design issue in the original esp-homekit library, |
| 59 | + //and I have no reason to modify this "feature". |
| 60 | + cha_switch_on.setter = cha_switch_on_setter; |
| 61 | + arduino_homekit_setup(&config); |
| 62 | + |
| 63 | + //report the switch value to HomeKit if it is changed (e.g. by a physical button) |
| 64 | + //bool switch_is_on = true/false; |
| 65 | + //cha_switch_on.value.bool_value = switch_is_on; |
| 66 | + //homekit_characteristic_notify(&cha_switch_on, cha_switch_on.value); |
| 67 | +} |
| 68 | + |
| 69 | +void my_homekit_loop() { |
| 70 | + arduino_homekit_loop(); |
| 71 | + const uint32_t t = millis(); |
| 72 | + if (t > next_heap_millis) { |
| 73 | + // show heap info every 5 seconds |
| 74 | + next_heap_millis = t + 5 * 1000; |
| 75 | + LOG_D("Free heap: %d, HomeKit clients: %d", |
| 76 | + ESP.getFreeHeap(), arduino_homekit_connected_clients_count()); |
| 77 | + |
| 78 | + } |
| 79 | +} |
0 commit comments