espressif / esp-homekit-sdk

544 stars 98 forks source link

Manual Lightbulb control with a switch #72

Closed matheusesperanca closed 2 years ago

matheusesperanca commented 2 years ago

Hi there,

I used Lightbulb example on ESP32 and everything is working.

I can't find some solution to get the handlers to change the light state manually (with a switch), how I can do this?

Thanks.

shahpiyushv commented 2 years ago

@matheusesperanca , you can use the push button logic from here and register your callback.

Sample:

button_handle_t btn_handle = iot_button_create(BUTTON_GPIO, BUTTON_ACTIVE_LEVEL);
if (btn_handle) {
    /* Register a callback for a button tap (short press) event */
    iot_button_set_evt_cb(btn_handle, BUTTON_CB_TAP, push_btn_cb, NULL);
}

In the push_btn_cb, you can turn on/off the light and also report the state change to HomeKit by calling hap_char_update_val()

matheusesperanca commented 2 years ago

Solved, thanks!

mcpat-it commented 2 years ago

@shahpiyushv and which "HAP characteristic object handle" and "Pointer to new value" to use?

mcpat-it commented 2 years ago

After hours of searching, I found out...

//turn on light
hap_val_t appliance_value = {
        .b = true
};
hap_char_t *temp = hap_serv_get_char_by_uuid(service, HAP_CHAR_UUID_ON);
hap_char_update_val(temp, &appliance_value);