maximkulkin / esp-homekit-demo

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

lightbulb relay #345

Closed QDUNI closed 4 years ago

QDUNI commented 4 years ago

Hi,

Im trying to create a lightbulb relay in Homekit. I want the relay to turn on for 12 sec if I set the lightbulb on and off 12 sec on an other relay. For brightness I want the relay to turn on for a part of these 12 sec. 12/100 * brightness. I already wrote the software. but now im thinking is this the best way ? and is it safe to toggle the relay with brightness...?

thnx!

maximkulkin commented 4 years ago

What is the use case for such lightbulb? Is light bulb an AC bulb? By saying brightness do you mean implementing dimming? If so, relays are not the right way to implement dimming for an AC lightbulb. You will need a zero crossing detector + triac circuit (google for AC dimmer e.g. this )

QDUNI commented 4 years ago

its my fireplace. I can switch it on and of by triggering a relay on for a few seconds. why is a relay no good for this? The relays are free and have no current when they switch

maximkulkin commented 4 years ago

Is it a gas fireplace with electric valve and igniter? Is it designed to be turned on and off that frequently? How does brightness control fits into this?

QDUNI commented 4 years ago

Its designed with a 433 remote. you can increase the flame by holding the flame button. and decrease by holding small flame button. On the controller unit is a panel connector for home automation. If you wire pin 1 with 3. the flame will go up. if you wire 1 with 2 the flame will go down. Now I want the brightness to set the fireplace flame on a certain height. By wiring pin 1 and 3 for a amount you can determine the height of the flame

QDUNI commented 4 years ago

I Got some problems in the code.... if I turn on the device the device will switch call this function

void relay_write(int relay, int msec)
{
    gpio_write(relay, 1);
    vTaskDelay(msec / portTICK_PERIOD_MS);
    gpio_write(relay, 0);
}

turning on takes 12 sec this is to much for Homekit to handle? the relay will turn on for 12 sec and then turn off, Great! But Homekit shows a spinning wheel and says not responding.... If I refresh Homekit by switching to another room and go back to the room with the device it will show the correct state. How can I Fix this issue? is the problem due to vTaskDelay(msec / portTICK_PERIOD_MS); ?

maximkulkin commented 4 years ago

This is an incorrect way to do that, because this code is most likely called directly from characteristic callback which blocks the entire HomeKit server thread for 12 seconds and thus HomeKit server can not respond to iOS controller (e.g. iPhone) that characteristic update was successful (or perform any other processing).

The correct way to do that is to update state and then arm a timer with timeout callback that will deactivate whatever needs to be deactivated (in this case, set GPIO to 0). You can take a look at lock example where lock is unlocked for certain period (e.g. several seconds) and then locked back again.

QDUNI commented 4 years ago
#include <etstimer.h>
#include <esplibs/libmain.h>
ETSTimer Relay_timer;
void relay_write(int relay, int msec)
{
    gpio_write(relay, 1);
    sdk_os_timer_disarm(&Relay_timer); // This makes sure the the timer is stopped? 
    sdk_os_timer_arm(&Relay_timer, msec, 0); //This starts the timer?
    gpio_write(relay, 0);
}

Is there anything else I need?

maximkulkin commented 4 years ago

You need to read that example more

renandw commented 4 years ago

You will need a zero crossing detector + triac circuit (google for AC dimmer e.g. this )

Hi, does de sonnoff_basic_pwm example works with the zero-crossing detector? thanks

maximkulkin commented 4 years ago

@QDUNI you need to look for timer callback and how to setup timer callback. What you're missing is that those disarm+arm lines do not magically fix everything - pause execution, return control to server and then unpause when timer triggers and continue from that point. You need to put your gpio_write(relay, 0) into a separate function which will be called when timer expires.

@renandw no, i do not think so. For my personal needs I've built a library for that - esp-ac-dimmer (keep in mind that if you plan to use it with Triac-based dimmer, the only dimmer type that will work is forward phase dimmer because of the way Triac works).

QDUNI commented 4 years ago

I already fixed it ! It works amazing thnx!