Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. /*
  2. * switch.ino
  3. *
  4. * Created on: 2020-05-15
  5. * Author: Mixiaoxiao (Wang Bin)
  6. *
  7. * HAP section 8.38 Switch
  8. * An accessory contains a switch.
  9. *
  10. * This example shows how to:
  11. * 1. define a switch accessory and its characteristics (in my_accessory.c).
  12. * 2. get the switch-event sent from iOS Home APP.
  13. * 3. report the switch value to HomeKit.
  14. *
  15. * You should:
  16. * 1. read and use the Example01_TemperatureSensor with detailed comments
  17. * to know the basic concept and usage of this library before other examples。
  18. * 2. erase the full flash or call homekit_storage_reset() in setup()
  19. * to remove the previous HomeKit pairing storage and
  20. * enable the pairing with the new accessory of this new HomeKit example.
  21. */
  22.  
  23. #include <Arduino.h>
  24. #include <arduino_homekit_server.h>
  25. #include "wifi_info.h"
  26.  
  27. #define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n"), ##__VA_ARGS__);
  28.  
  29. bool isPCOn;
  30. bool changeExecutedFromHomekit;
  31. const long interval = 20000;
  32. unsigned long previousMillis = 0;
  33.  
  34. void setup() {
  35.  
  36. Serial.begin(115200);
  37. wifi_connect(); // in wifi_info.h
  38. //homekit_storage_reset(); // to remove the previous HomeKit pairing storage when you first run this new HomeKit example
  39. my_homekit_setup();
  40. }
  41.  
  42. void loop() {
  43.  
  44. my_homekit_loop();
  45. delay(10);
  46.  
  47. LOG_D("Loops");
  48.  
  49. unsigned long currentMillis = millis();
  50.  
  51. //Only continue if we passed the interval
  52. if (currentMillis - previousMillis < interval) {
  53. // save the last time you blinked the LED
  54. return;
  55. }
  56.  
  57. previousMillis = currentMillis;
  58.  
  59. syncPCAndHomekitStatus();
  60.  
  61. }
  62.  
  63. //==============================
  64. // HomeKit setup and loop
  65. //==============================
  66.  
  67. // access your HomeKit characteristics defined in my_accessory.c
  68. extern "C" homekit_server_config_t config;
  69. extern "C" homekit_characteristic_t cha_switch_on;
  70.  
  71. static uint32_t next_heap_millis = 0;
  72.  
  73. #define GPIO_OUT_SW 14 //D5 - Schaltet Relay
  74. #define GPIO_IN_STATUS 12 //D6 - PC Status mit Pullup
  75. #define DURATION_HOLD_TOGGLE 300
  76. #define DURATION_HOLD_TURN_OFF_HARD 6000
  77. #define RELAY_CLOSED 1
  78. #define RELAY_OPEN (!RELAY_CLOSED)
  79.  
  80. //Called when the switch value is changed by iOS Home APP
  81. void cha_switch_on_setter(const homekit_value_t value) {
  82.  
  83. changeExecutedFromHomekit = true;
  84.  
  85. cha_switch_on.value.bool_value = value.bool_value;
  86.  
  87. //Read PC Status LED
  88. syncPCAndHomekitStatus();
  89.  
  90. }
  91.  
  92. void pushPowerButton() {
  93. digitalWrite(GPIO_OUT_SW, RELAY_OPEN);
  94. delay(DURATION_HOLD_TOGGLE);
  95. digitalWrite(GPIO_OUT_SW, RELAY_CLOSED);
  96. }
  97.  
  98. void my_homekit_setup() {
  99.  
  100. initRelayPinAsClosed();
  101.  
  102. //Add the .setter function to get the switch-event sent from iOS Home APP.
  103. //The .setter should be added before arduino_homekit_setup.
  104. //HomeKit sever uses the .setter_ex internally, see homekit_accessories_init function.
  105. //Maybe this is a legacy design issue in the original esp-homekit library,
  106. //and I have no reason to modify this "feature".
  107. cha_switch_on.setter = cha_switch_on_setter;
  108. arduino_homekit_setup(&config);
  109.  
  110. //report the switch value to HomeKit if it is changed (e.g. by a physical button)
  111. //bool switch_is_on = true/false;
  112. //cha_switch_on.value.bool_value = switch_is_on;
  113. //homekit_characteristic_notify(&cha_switch_on, cha_switch_on.value);
  114. }
  115.  
  116. void my_homekit_loop() {
  117. arduino_homekit_loop();
  118. const uint32_t t = millis();
  119. if (t > next_heap_millis) {
  120. // show heap info every 5 seconds
  121. next_heap_millis = t + 5 * 1000;
  122. LOG_D("Free heap: %d, HomeKit clients: %d",
  123. ESP.getFreeHeap(), arduino_homekit_connected_clients_count());
  124.  
  125. }
  126. }
  127.  
  128. void updatePCIsOnStatus() {
  129.  
  130. isPCOn = false;
  131. int status = 0;
  132. status = digitalRead(GPIO_IN_STATUS);
  133.  
  134. if (status = 0)
  135. {
  136. isPCOn = true;
  137. }
  138. }
  139.  
  140. void syncPCAndHomekitStatus() {
  141.  
  142. LOG_D("Syncing States");
  143.  
  144. updatePCIsOnStatus();
  145.  
  146. //Homekit Status
  147. bool shouldPCBeTurnedOn = cha_switch_on.value.bool_value;
  148.  
  149. if (shouldPCBeTurnedOn != isPCOn) {
  150. //2 Options:
  151. //Power button was pressed and Homekit status is wrong -> Update HomekitStatus
  152. if (!changeExecutedFromHomekit) {
  153. LOG_D("Difference from Power button without scheduled change -> Syncing To Homekit");
  154. cha_switch_on.value.bool_value = isPCOn;
  155. homekit_characteristic_notify(&cha_switch_on, cha_switch_on.value);
  156.  
  157. } else {
  158. //Change was executed from Homekit -> Push button!
  159. LOG_D("Difference from Power button with Sheduled Change -> Push button!");
  160. changeExecutedFromHomekit = false;
  161. pushPowerButton();
  162. }
  163. }
  164.  
  165. }
  166.  
  167. void initRelayPinAsClosed() {
  168. pinMode(GPIO_OUT_SW, OUTPUT);
  169. digitalWrite(GPIO_OUT_SW, RELAY_CLOSED);
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement