Skip to content

Commit

Permalink
Add example code for Gosund smart power socket
Browse files Browse the repository at this point in the history
  • Loading branch information
rroemhild committed Sep 25, 2019
1 parent 97ca0b5 commit 2f27168
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
21 changes: 21 additions & 0 deletions examples/gosund/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
========================================
Example code for Gosund SP1 Power Socket
========================================

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.

Blue LED = power off
Red LED = power on

https://www.amazon.de/gp/product/B0777BWS1P

Gosund must be flashed with ``-fm dout`` to acceess the repl.

.. code-block:: shell
esptool.py --port $(PORT) --baud 460800 write_flash --flash_size=detect --verify -fm dout 0x0 micropython/ports/esp8266/build/firmware-combined.bin
TODO
----

* Add Support for power monitoring chip
80 changes: 80 additions & 0 deletions examples/gosund/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import uos
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()
while True:
led(not led())
sleep_ms(250)


class SmartSocket(HomieNode):
def __init__(self):
super().__init__(id="relay", name="Relay 16A", type="Gosund SP1 v23")
uos.dupterm(None, 1) # disable REPL so we can use the blue led
self.led_b = Pin(1, Pin.OUT, value=1)
self.led_r = Pin(13, Pin.OUT, value=1)
self.relay = Pin(14, 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(3, Pin.IN))
self.button.release_func(self.toggle, ())
self.button.long_func(reset, (self.led_r,))

def off(self):
self.relay(0)
self.led_b(0)
self.led_r(1)
self.relay_property.data = FALSE

def on(self):
self.relay(1)
self.led_b(1)
self.led_r(0)
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.run_forever()


if __name__ == "__main__":
sleep_ms(500)
main()

0 comments on commit 2f27168

Please sign in to comment.