Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * switch.ino
- *
- * Created on: 2020-05-15
- * Author: Mixiaoxiao (Wang Bin)
- *
- * HAP section 8.38 Switch
- * An accessory contains a switch.
- *
- * This example shows how to:
- * 1. define a switch accessory and its characteristics (in my_accessory.c).
- * 2. get the switch-event sent from iOS Home APP.
- * 3. report the switch value to HomeKit.
- *
- * You should:
- * 1. read and use the Example01_TemperatureSensor with detailed comments
- * to know the basic concept and usage of this library before other examples。
- * 2. erase the full flash or call homekit_storage_reset() in setup()
- * to remove the previous HomeKit pairing storage and
- * enable the pairing with the new accessory of this new HomeKit example.
- */
- #include <Arduino.h>
- #include <arduino_homekit_server.h>
- #include "wifi_info.h"
- #define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n"), ##__VA_ARGS__);
- bool isPCOn;
- bool changeExecutedFromHomekit;
- const long interval = 20000;
- unsigned long previousMillis = 0;
- void setup() {
- Serial.begin(115200);
- wifi_connect(); // in wifi_info.h
- //homekit_storage_reset(); // to remove the previous HomeKit pairing storage when you first run this new HomeKit example
- my_homekit_setup();
- }
- void loop() {
- my_homekit_loop();
- delay(10);
- LOG_D("Loops");
- unsigned long currentMillis = millis();
- //Only continue if we passed the interval
- if (currentMillis - previousMillis < interval) {
- // save the last time you blinked the LED
- return;
- }
- previousMillis = currentMillis;
- syncPCAndHomekitStatus();
- }
- //==============================
- // HomeKit setup and loop
- //==============================
- // access your HomeKit characteristics defined in my_accessory.c
- extern "C" homekit_server_config_t config;
- extern "C" homekit_characteristic_t cha_switch_on;
- static uint32_t next_heap_millis = 0;
- #define GPIO_OUT_SW 14 //D5 - Schaltet Relay
- #define GPIO_IN_STATUS 12 //D6 - PC Status mit Pullup
- #define DURATION_HOLD_TOGGLE 300
- #define DURATION_HOLD_TURN_OFF_HARD 6000
- #define RELAY_CLOSED 1
- #define RELAY_OPEN (!RELAY_CLOSED)
- //Called when the switch value is changed by iOS Home APP
- void cha_switch_on_setter(const homekit_value_t value) {
- changeExecutedFromHomekit = true;
- cha_switch_on.value.bool_value = value.bool_value;
- //Read PC Status LED
- syncPCAndHomekitStatus();
- }
- void pushPowerButton() {
- digitalWrite(GPIO_OUT_SW, RELAY_OPEN);
- delay(DURATION_HOLD_TOGGLE);
- digitalWrite(GPIO_OUT_SW, RELAY_CLOSED);
- }
- void my_homekit_setup() {
- initRelayPinAsClosed();
- //Add the .setter function to get the switch-event sent from iOS Home APP.
- //The .setter should be added before arduino_homekit_setup.
- //HomeKit sever uses the .setter_ex internally, see homekit_accessories_init function.
- //Maybe this is a legacy design issue in the original esp-homekit library,
- //and I have no reason to modify this "feature".
- cha_switch_on.setter = cha_switch_on_setter;
- arduino_homekit_setup(&config);
- //report the switch value to HomeKit if it is changed (e.g. by a physical button)
- //bool switch_is_on = true/false;
- //cha_switch_on.value.bool_value = switch_is_on;
- //homekit_characteristic_notify(&cha_switch_on, cha_switch_on.value);
- }
- void my_homekit_loop() {
- arduino_homekit_loop();
- const uint32_t t = millis();
- if (t > next_heap_millis) {
- // show heap info every 5 seconds
- next_heap_millis = t + 5 * 1000;
- LOG_D("Free heap: %d, HomeKit clients: %d",
- ESP.getFreeHeap(), arduino_homekit_connected_clients_count());
- }
- }
- void updatePCIsOnStatus() {
- isPCOn = false;
- int status = 0;
- status = digitalRead(GPIO_IN_STATUS);
- if (status = 0)
- {
- isPCOn = true;
- }
- }
- void syncPCAndHomekitStatus() {
- LOG_D("Syncing States");
- updatePCIsOnStatus();
- //Homekit Status
- bool shouldPCBeTurnedOn = cha_switch_on.value.bool_value;
- if (shouldPCBeTurnedOn != isPCOn) {
- //2 Options:
- //Power button was pressed and Homekit status is wrong -> Update HomekitStatus
- if (!changeExecutedFromHomekit) {
- LOG_D("Difference from Power button without scheduled change -> Syncing To Homekit");
- cha_switch_on.value.bool_value = isPCOn;
- homekit_characteristic_notify(&cha_switch_on, cha_switch_on.value);
- } else {
- //Change was executed from Homekit -> Push button!
- LOG_D("Difference from Power button with Sheduled Change -> Push button!");
- changeExecutedFromHomekit = false;
- pushPowerButton();
- }
- }
- }
- void initRelayPinAsClosed() {
- pinMode(GPIO_OUT_SW, OUTPUT);
- digitalWrite(GPIO_OUT_SW, RELAY_CLOSED);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement