Sonoff: IRQ on push-button
Posted: Sat Dec 17, 2016 12:12 pm
Hi,
I was able to flash a Sonoff device with MicroPython. This Sonoff should switch a lamp on/off. Everyhing works fine and now I want to implement a back-up function, in case my wlan is not available. The idea is to make use of the push-button on the Sonoff. I was already able to define it as an interrupt to turn directly the lamp on/off. The code is pretty simple:
Next, I want to give this push-button a further functionality: to reboot the Sonoff:
- push-button pressed less than 2sec. -> just switch the relay
- push-button pressed/hold longer than 2sec -> reboot the Sonoff
For this I changed the callback-function to this:
But it is not working. When I press and hold the push-button it never makes the machine.reset(). Always it is switching the relay.
I ASSUME, it is because of the bouncing-beviour of the push-button. Any ideas?
I was able to flash a Sonoff device with MicroPython. This Sonoff should switch a lamp on/off. Everyhing works fine and now I want to implement a back-up function, in case my wlan is not available. The idea is to make use of the push-button on the Sonoff. I was already able to define it as an interrupt to turn directly the lamp on/off. The code is pretty simple:
Code: Select all
pinRelay = machine.Pin(12, machine.Pin.OUT)
pinSchalter = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP)
def umschalten(p):
pinRelay.value(not pinRelay.value())
pinSchalter.irq(trigger=machine.Pin.IRQ_FALLING, handler=umschalten)
Next, I want to give this push-button a further functionality: to reboot the Sonoff:
- push-button pressed less than 2sec. -> just switch the relay
- push-button pressed/hold longer than 2sec -> reboot the Sonoff
For this I changed the callback-function to this:
Code: Select all
def umschalten(p):
zeitschleife = 0
time.sleep_ms(500)
while pinSchalter == 0:
if zeitschleifer >= 100:
machine.reset()
time.sleep_ms(20)
zeitschleife += 1
pinRelay.value(not pinRelay.value())
I ASSUME, it is because of the bouncing-beviour of the push-button. Any ideas?