Skip to content

Commit

Permalink
Add example code for Obi wifi power socket
Browse files Browse the repository at this point in the history
  • Loading branch information
rroemhild committed Jul 30, 2019
1 parent 1368ae2 commit 0a28f0d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/obi-socket/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
========================================
Example Code for Obi Wifi Stecker Schuko
========================================

This example code can switch the relay on and off via MQTT or the button on the top. Long press on the button will reset the device.

https://www.obi.de/hausfunksteuerung/wifi-stecker-schuko/p/2291706
76 changes: 76 additions & 0 deletions examples/obi-socket/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import settings

from time import sleep_ms
from machine import Pin
from aswitch import Pushbutton

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


def reset(led):
import machine
wdt = machine.WDT()
wdt.feed()
led(0)
machine.reset()


class SmartSocket(HomieNode):
def __init__(self):
super().__init__(id="relay", name="Wifi Power Socket", type="OW8266-02Q")
self.led = Pin(4, Pin.OUT, value=1)
self.r_on = Pin(12, Pin.OUT)
self.r_off = Pin(5, Pin.OUT)

self.relay_property = HomieNodeProperty(
id="power",
name="Relay",
settable=True,
retained=True,
datatype="bool",
default=FALSE,
restore=True,
)
self.add_property(self.relay_property)

self.button = Pushbutton(Pin(14, Pin.IN, Pin.PULL_UP))
self.button.release_func(self.toggle, ())
self.button.long_func(reset, (self.led,))

def off(self):
self.r_off(0)
sleep_ms(100)
self.r_on(1)
self.relay_property.data = FALSE

def on(self):
self.r_on(0)
sleep_ms(100)
self.r_off(1)
self.relay_property.data = TRUE

def callback(self, topic, payload, retained):
if b"power" in topic:
if payload == FALSE:
self.off()
elif payload == TRUE:
self.on()

def toggle(self):
if self.relay_property.data == TRUE:
self.off()
else:
self.on()


def main():
homie = HomieDevice(settings)
homie.add_node(SmartSocket())
homie.start()


if __name__ == "__main__":
main()

0 comments on commit 0a28f0d

Please sign in to comment.