Skip to content

Commit

Permalink
LED example: Add device button to toggle led
Browse files Browse the repository at this point in the history
  • Loading branch information
rroemhild committed Dec 7, 2019
1 parent f981433 commit 723f5f4
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion examples/led/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import settings

from machine import Pin
from aswitch import Pushbutton

from homie.constants import FALSE, TRUE, BOOLEAN
from homie.device import HomieDevice
from homie.node import HomieNode
from homie.property import HomieNodeProperty
from machine import Pin


# reversed values for the esp8266 boards onboard led
Expand All @@ -15,6 +18,8 @@ def __init__(self, name="Onboard LED", pin=2):
super().__init__(id="led", name=name, type="LED")
self.pin = pin
self.led = Pin(pin, Pin.OUT, value=0)
self.btn = Pushbutton(Pin(0, Pin.IN, Pin.PULL_UP))
self.btn.press_func(self.toggle_led)

self.power_property = HomieNodeProperty(
id="power",
Expand All @@ -30,6 +35,14 @@ def on_power_msg(self, topic, payload, retained):
self.led(ONOFF[payload])
self.power_property.data = ONOFF[self.led()]

def toggle_led(self):
if self.power_property.data != TRUE:
self.led(0)
self.power_property.data = TRUE
else:
self.led(1)
self.power_property.data = FALSE


def main():
# Homie device setup
Expand Down

0 comments on commit 723f5f4

Please sign in to comment.