maximkulkin / esp-homekit-demo

Demo of Apple HomeKit accessory server library
MIT License
805 stars 233 forks source link

SonOff switch button with timer #337

Closed ricou27 closed 4 years ago

ricou27 commented 4 years ago

Hello,

I would like when I push the button to activate the SonOff contact, de-activate it after 1 sec. With the Sonoff_basic example, I tried to add some code to the "button_event_single_press" switch case like : switch_on.value.bool_value = !switch_on.value.bool_value; relay_write(switch_on.value.bool_value); homekit_characteristic_notify(&switch_on, switch_on.value); vTaskDelay(1000 / portTICK_PERIOD_MS); switch_on.value.bool_value = !switch_on.value.bool_value; relay_write(switch_on.value.bool_value); homekit_characteristic_notify(&switch_on, switch_on.value);

but impossible to do the job.

Have you an idea on how to do that

Thanks

maximkulkin commented 4 years ago

You can not just do vTaskDelay inside button callback, you're blocking from inside interrupt which might mess up the whole OS.

Instead you need to set up a timer to switch your thing back. Check out lock example, where (if you set unlock_period setting) lock is opened only for small duration and then locked back.

ricou27 commented 4 years ago

Many thanks for your answer. That’s Work by using a timer. Regards