-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example code for Obi wifi power socket
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |