Reaction on button press while running another function

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
upymar
Posts: 18
Joined: Sat Feb 04, 2017 7:47 am

Reaction on button press while running another function

Post by upymar » Mon Jun 01, 2020 9:43 am

Hi community,

I wrote some small programm for a sonoff s20 switch with a esp8266 core:

Code: Select all

from umqtt.simple import MQTTClient

SERVER = "192.168.1.3"
CLIENT_ID = "sonoff_s20_1"
TOPIC = b"/steckdosen/sonoff_s20_1"

relais = machine.Pin(12, machine.Pin.OUT, value = 1)
button = machine.PIN(0, machine.Pin.IN)
led_green = machine.Pin(13, machine.Pin.OUT, value = 1)

def sub_cb(topic, msg):
    if msg == b"1":
        relais.on()
    elif msg == b"0":
        relais.off()


def main(server=SERVER):
    c = MQTTClient(CLIENT_ID, server)
    # Subscribed messages will be delivered to this callback
    c.set_callback(sub_cb)
    c.connect()
    c.subscribe(TOPIC)
    print("Connected to %s, subscribed to %s topic" % (server, TOPIC))

    try:
        while 1:
            #micropython.mem_info()
            c.wait_msg()
    finally:
        c.disconnect()

main()
I am looking for a way to react on the defined button.
If pressed it should turn the relais of if it is currently switched on and vice versa.

But I don't have an idea how to achieve this.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Reaction on button press while running another function

Post by kevinkk525 » Mon Jun 01, 2020 3:43 pm

You could either use interrupts on the pin to change the state of the relais or you swith to using async programming with an async mqtt client and async button implementation:
https://github.com/peterhinch/micropython-mqtt
https://github.com/peterhinch/micropython-async

You'll definitely learn the most this way.


However, if you just want your sonoff s20 to work, you could use tasmota firmware, or if you prefer to stay in micropython and be able to expand or change its behaviour you could use my framework pysmartnode. I use it for a sonoff s20 and multiple sonoff th10:
https://github.com/kevinkk525/pysmartno ... tag/v6.0.2

Just flash the esp8266 firmware and upload a file "components.py" with content:

Code: Select all

import gc
from pysmartnode import config
from pysmartnode.components.machine.button import ToggleButton
from pysmartnode.components.switches.gpio import GPIO
gc.collect()
relais0 = GPIO(12, discover=True, friendly_name="Relais")
config.addComponent("gpio0", relais0)
gc.collect()

class reset:
    @staticmethod
    async def reset():
        await config._log.asyncLog("info", "Button requested reset", timeout=5,
                                   await_connection=False)
        import machine
        machine.reset()

button0 = ToggleButton(0, long_pressed_component=reset, long_pressed_method="reset",
                       released_component=relais0, suppress=True)
config.addComponent("button0", button0)
and a "config.py" with contents:

Code: Select all

# Required custom configuration
WIFI_SSID = "ssid"
WIFI_PASSPHRASE = "wifipassphrase"
MQTT_HOST = "192.168.178.10"
MQTT_PORT = 1883
MQTT_USER = "user"
MQTT_PASSWORD = "password"

# Optional configuration
MQTT_DISCOVERY_PREFIX = "homeassistant"
MQTT_RECEIVE_CONFIG = False

WIFI_LED = 13  # set a pin number to have the wifi state displayed by a blinking led. Useful for devices like sonoff
WIFI_LED_ACTIVE_HIGH = False  # if led is on when output is low, change to False

WEBREPL_ACTIVE = True  # If you want to have the webrepl active. Configures and starts it automatically.
WEBREPL_PASSWORD = "password"  # Be aware that webrepl needs 1.5kB of RAM!
RTC_TIMEZONE_OFFSET = 1  # offset from GMT timezone as ntptime on esp8266 does not support timezones

# Does not need to be changed normally
DEBUG = False
DEBUG_STOP_AFTER_EXCEPTION = False
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

upymar
Posts: 18
Joined: Sat Feb 04, 2017 7:47 am

Re: Reaction on button press while running another function

Post by upymar » Tue Jun 02, 2020 9:29 am

Thank you.

I will work through this.

Post Reply