HomeKit on ESP8266
ESP01

Lưu tạm:

26/2/2023 - Đêm lạnh

Chương trình chính trong HomeKIT dùng cho khóa cửa.

Trong đó, bổ xung thêm từ code ví dụ của thư viện:

/*

 * 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__);


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);

}


//==============================

// 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;

static uint32_t switch_last_changed = 0;

bool SwitchChanged;

int DoorClock_Timer = 10;   //Thời gian chờ để tự động khóa cửa (đơn vị giây)



#define PIN_SWITCH 3  //Thay đổi thành chân OUTPUT cần dùng, trên ESP01_Relay là GPIO: 0 | For testing: LED_BUILTIN  | RX: 3


//Called when the switch value is changed by iOS Home APP

void cha_switch_on_setter(const homekit_value_t value) {

bool on = value.bool_value;

cha_switch_on.value.bool_value = on; //sync the value

LOG_D("Switch: %s", on ? "ON" : "OFF");

digitalWrite(PIN_SWITCH, on ? LOW : HIGH);

  

   //Khóa cửa sau 10s (1)

  switch_last_changed = millis() + DoorClock_Timer*1000;

  SwitchChanged = true;

}


void my_homekit_setup() {

pinMode(PIN_SWITCH, OUTPUT);

digitalWrite(PIN_SWITCH, HIGH);


//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());

}

  //Khóa cửa sau 10s (2)

  if(SwitchChanged == true && t > switch_last_changed){

    SwitchChanged = false;

    digitalWrite(PIN_SWITCH,!digitalRead(PIN_SWITCH));

    bool switch_is_on = false;

    cha_switch_on.value.bool_value = switch_is_on;

    homekit_characteristic_notify(&cha_switch_on, cha_switch_on.value);

   }

}