Sonoff: IRQ on push-button

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
BigMan
Posts: 22
Joined: Sat Oct 29, 2016 2:29 pm

Sonoff: IRQ on push-button

Post by BigMan » 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:

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())
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?

User avatar
Frida
Posts: 40
Joined: Sat Jan 30, 2016 2:20 pm
Location: Middelfart, Denmark

Re: Sonoff: IRQ on push-button

Post by Frida » Sat Dec 17, 2016 3:36 pm

Boiled down, should if zeitschleifer >= 100:, not be if zeitschleife >= 100:
My bet.
Yes Frida is my watchdog!

BigMan
Posts: 22
Joined: Sat Oct 29, 2016 2:29 pm

Re: Sonoff: IRQ on push-button

Post by BigMan » Sat Dec 17, 2016 4:00 pm

Frida wrote:Boiled down, should if zeitschleifer >= 100:, not be if zeitschleife >= 100:
My bet.
uppp ... yes this is a mistake.
But even after I fixed this, it is still not working.

Updated code:

Code: Select all

def umschalten(p):
    zeitschleife = 0
    time.sleep_ms(500)
    while pinSchalter == 0:
        if zeitschleife >= 100:
            machine.reset()
        time.sleep_ms(20)
        zeitschleife += 1
    pinRelay.value(not pinRelay.value())

User avatar
Frida
Posts: 40
Joined: Sat Jan 30, 2016 2:20 pm
Location: Middelfart, Denmark

Re: Sonoff: IRQ on push-button

Post by Frida » Sat Dec 17, 2016 5:00 pm

On my wipy1, I can run this:

Code: Select all

# on wipy1

import micropython
micropython.alloc_emergency_exception_buf(100)

import time
from machine import Pin

#pinRelay = machine.Pin(12, machine.Pin.OUT)
#pinSchalter = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP)
pinRelay=Pin('GP16');pinRelay.value(1);pinRelay.mode(1) #set led off
pinSchalter=Pin('GP17', Pin.IN, Pin.PULL_UP)


def umschalten(p):
    zeitschleife = 0
    time.sleep_ms(500)
    print('her går det godt!')
    while pinSchalter() == 0:
        print('Schalter=0')
        if zeitschleife >= 100:
            #machine.reset()
			print('machine.reset')
        time.sleep_ms(20)
        zeitschleife += 1
    pinRelay.value(not pinRelay.value())

pinSchalter.irq(trigger=machine.Pin.IRQ_FALLING, handler=umschalten)   
Yes Frida is my watchdog!

BigMan
Posts: 22
Joined: Sat Oct 29, 2016 2:29 pm

Re: Sonoff: IRQ on push-button

Post by BigMan » Sat Dec 17, 2016 8:04 pm

Frida wrote:On my wipy1, I can run this:

Code: Select all

# on wipy1

import micropython
micropython.alloc_emergency_exception_buf(100)

import time
from machine import Pin

#pinRelay = machine.Pin(12, machine.Pin.OUT)
#pinSchalter = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP)
pinRelay=Pin('GP16');pinRelay.value(1);pinRelay.mode(1) #set led off
pinSchalter=Pin('GP17', Pin.IN, Pin.PULL_UP)


def umschalten(p):
    zeitschleife = 0
    time.sleep_ms(500)
    print('her går det godt!')
    while pinSchalter() == 0:
        print('Schalter=0')
        if zeitschleife >= 100:
            #machine.reset()
			print('machine.reset')
        time.sleep_ms(20)
        zeitschleife += 1
    pinRelay.value(not pinRelay.value())

pinSchalter.irq(trigger=machine.Pin.IRQ_FALLING, handler=umschalten)   
Looks for me exactly like my code ... or? Is it working on your wipy?
However, if it is exactly like my code, on my Sonoff it's not working.

User avatar
Frida
Posts: 40
Joined: Sat Jan 30, 2016 2:20 pm
Location: Middelfart, Denmark

Re: Sonoff: IRQ on push-button

Post by Frida » Sat Dec 17, 2016 8:26 pm

I have changed your pin to pins for wipy1:

Code: Select all

#pinRelay = machine.Pin(12, machine.Pin.OUT)
#pinSchalter = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP)
pinRelay=Pin('GP16');pinRelay.value(1);pinRelay.mode(1) #set led off
pinSchalter=Pin('GP17', Pin.IN, Pin.PULL_UP)
changed:

Code: Select all

    while pinSchalter == 0:
to:

Code: Select all

     while pinSchalter() == 0:
inserted:

Code: Select all

    print('her går det godt!')
changed:

Code: Select all

         #machine.reset()
         print('machine.reset')
so I could see what happend.

Yes it runs on my wipy1.

If it still dosn't work, let me see the whole programm, so we can get it to work.
Yes Frida is my watchdog!

BigMan
Posts: 22
Joined: Sat Oct 29, 2016 2:29 pm

Re: Sonoff: IRQ on push-button

Post by BigMan » Sat Dec 17, 2016 9:40 pm

Frida wrote: changed:

Code: Select all

    while pinSchalter == 0:
to:

Code: Select all

     while pinSchalter() == 0:


ahh ... how could I oversee that. Sure. Now, I changed it to >>while pinSchalter.value() == 0:<< and it basically work.
(I am only still getting the Message "NameError:" when I press it less than 2Sec. But perhaps it is related with another piece of the code.
Let me sleep over it. Perhaps this is also just because of a stupid typo which I currently don't see with my sleepy eyes.

Post Reply