我无法将外部按钮连接到我的 Homekit 系统(没有 homebridge 的 ESP8266)

I can not connect an external button to my Homekit system (ESP8266 without homebridge)

看完这个视频。 https://www.youtube.com/watch?v=QBj8OLig8Kg 这是如何创建一个 homekit 系统而无需使用我想创建自己的桥。

我的想法是让 2 个不同的灯连接到互联网,我用 siri 和 2 个可以关闭灯的外部按钮来处理它们,而不必总是使用 siri。 (例如,如果我的电池 phone 没电了)。所有这些都在同一个 ESP 8266 中。

我已经可以使用以下代码在不需要 homebridge 的情况下操作两盏灯

#define Relay1    16    //D0
#define Relay2    5     //D1

#define button1   14    //D5
#define button2   12    //D6    
#define button3   13    //D7

bool state1 = false;
bool state2 = false;


// ----------------------------------------------------   Inicializacion de wifi  -------------------------------------------------

static void wifi_init() {
    struct sdk_station_config wifi_config = {
        .ssid = WIFI_SSID,
        .password = WIFI_PASSWORD,
    };

    sdk_wifi_set_opmode(STATION_MODE);
    sdk_wifi_station_set_config(&wifi_config);
    sdk_wifi_station_connect();
}

// --------------------------------------------------------   Logica de Relay1   -----------------------------------------------------

void relay1_write(bool on) {
    gpio_write(Relay1, on ? 1 : 0);
}


void relay1_init() {
    gpio_enable(Relay1, GPIO_OUTPUT);
    relay1_write(state1);
}


void relay1_identify_task(void *_args) {
    for (int i=0; i<3; i++) {
        for (int j=0; j<2; j++) {
            relay1_write(true);
            vTaskDelay(100 / portTICK_PERIOD_MS);
            relay1_write(false);
            vTaskDelay(100 / portTICK_PERIOD_MS);
        }

        vTaskDelay(250 / portTICK_PERIOD_MS);
    }

    relay1_write(state1);
    vTaskDelete(NULL);
}


void relay1_identify(homekit_value_t _value) {
    printf("Relay 1 identify\n");
    xTaskCreate(relay1_identify_task, "Relay Identify", 128, NULL, 2, NULL);
}


homekit_value_t relay1_on_get() {
    return HOMEKIT_BOOL(state1);
}


void relay1_on_set(homekit_value_t value) {
    if (value.format != homekit_format_bool) {
        printf("Invalid value format: %d\n", value.format);
        return;
    }

    state1 = value.bool_value;
    relay1_write(state1);
}


// --------------------------------------------------------   Logica de Relay2   -----------------------------------------------------

void relay2_write(bool on) {
    gpio_write(Relay2, on ? 1 : 0);
}


void relay2_init() {
    gpio_enable(Relay2, GPIO_OUTPUT);
    relay2_write(state2);
}


void relay2_identify_task(void *_args) {
    for (int i=0; i<3; i++) {
        for (int j=0; j<2; j++) {
            relay2_write(true);
            vTaskDelay(100 / portTICK_PERIOD_MS);
            relay2_write(false);
            vTaskDelay(100 / portTICK_PERIOD_MS);
        }

        vTaskDelay(250 / portTICK_PERIOD_MS);
    }

    relay2_write(state2);
    vTaskDelete(NULL);
}


void relay2_identify(homekit_value_t _value) {
    printf("Relay 2 identify\n");
    xTaskCreate(relay2_identify_task, "Relay Identify", 128, NULL, 2, NULL);
}


homekit_value_t relay2_on_get() {
    return HOMEKIT_BOOL(state2);
}


void relay2_on_set(homekit_value_t value) {
    if (value.format != homekit_format_bool) {
        printf("Invalid value format: %d\n", value.format);
        return;
    }

    state2 = value.bool_value;
    relay2_write(state2);
}


// ------------------------------------------------   Configuracion del server de Homekit   ------------------------------------------

homekit_accessory_t *accessories[] = {
    HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_lightbulb, .services=(homekit_service_t*[]){
        HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]){
            HOMEKIT_CHARACTERISTIC(NAME, "Cuarto"),
            HOMEKIT_CHARACTERISTIC(MANUFACTURER, "Estonian Port"),
            HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "ASD123"),
            HOMEKIT_CHARACTERISTIC(MODEL, "C.U.C.A"),
            HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "0.1"),
            HOMEKIT_CHARACTERISTIC(IDENTIFY, relay1_identify),
            NULL
        }),
        HOMEKIT_SERVICE(LIGHTBULB, .primary=true, .characteristics=(homekit_characteristic_t*[]){
            HOMEKIT_CHARACTERISTIC(NAME, "Luz"),
            HOMEKIT_CHARACTERISTIC(ON, false,
            .getter=relay1_on_get,
            .setter=relay1_on_set
            ),
            NULL
        }),
        NULL
    }),
    HOMEKIT_ACCESSORY(.id=2, .category=homekit_accessory_category_lightbulb, .services=(homekit_service_t*[]){
        HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]){
            HOMEKIT_CHARACTERISTIC(NAME, "Cuarto"),
            HOMEKIT_CHARACTERISTIC(MANUFACTURER, "Estonian Port"),
            HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "ASD123"),
            HOMEKIT_CHARACTERISTIC(MODEL, "C.U.C.A"),
            HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "0.1"),
            HOMEKIT_CHARACTERISTIC(IDENTIFY, relay2_identify),
            NULL
        }),
        HOMEKIT_SERVICE(LIGHTBULB, .primary=true, .characteristics=(homekit_characteristic_t*[]){
            HOMEKIT_CHARACTERISTIC(NAME, "Luz"),
            HOMEKIT_CHARACTERISTIC(ON, false,
            .getter=relay2_on_get,
            .setter=relay2_on_set
            ),
            NULL
        }),
        NULL
    }),

    NULL
};


homekit_server_config_t config = {
    .accessories = accessories,
    .password = "111-11-111"
};


// -----------------------------------------------------------   MAIN   ----------------------------------------------------------

void user_init(void) {
    uart_set_baud(0, 115200);

    wifi_init();
    relay1_init();
    relay2_init();
    homekit_server_init(&config);
}

但我不知道如何实现这两个外部按钮。我尝试实施:

void setup()
{
  pinMode(button1, INPUT);   
  pinMode(button2, INPUT);
}

void loop() {
    if (digitalRead(button1))
    {
     relay1_write(state1);
    }
}

但它给了我错误,因为我没有办法实现这个库。 我也试着理解这个例子。 https://github.com/maximkulkin/esp-homekit-demo/tree/master/examples/button 但它不适用于我想做的事情,因为在那种情况下,因为按钮被 homekit 识别,我希望它只是关闭并打开灯

非常感谢您的宝贵时间!

在写我的问题时,我意识到了一些事情,在考虑得更好之后,我认为我应该将输出继电器连接到一个组合灯键上。这样一来,就有可能以类比的方式打开和关闭灯。带来不便敬请谅解!谢谢