From 2f4e0dd3b3f65b0a8cd3c2d3f4eb019ce0f79e13 Mon Sep 17 00:00:00 2001 From: s3lph Date: Sun, 10 Sep 2023 05:22:07 +0200 Subject: [PATCH] initial commit --- __init__.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++ flow3r.toml | 11 ++++++++ 2 files changed, 83 insertions(+) create mode 100644 __init__.py create mode 100644 flow3r.toml diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..3aafe1b --- /dev/null +++ b/__init__.py @@ -0,0 +1,72 @@ + +import gc +import network +import socket + +from st3m.application import Application, ApplicationContext + +from umqtt.robust import MQTTClient + +WIFI_SSID = b'...' +WIFI_PSK = b'...' + +MQTT_BROKER_IP = '...' +MQTT_CLIENT_ID = b'flow3r-{}' +MQTT_BASE_TOPIC = b'flow3r-{}/event' + + +class Flow3rOpenhabMqtt(Application): + + def __init__(self, app_ctx: ApplicationContext) -> None: + super().__init__(app_ctx) + self.state = 0 + self.nic = network.WLAN(network.STA_IF) + client_id = MQTT_CLIENT_ID.format(self.nic.config('mac').hex()) + self.base_topic = MQTT_BASE_TOPIC.format(self.nic.config('mac').hex()) + self.mqtt = MQTTClient(client_id, MQTT_BROKER_IP) + self.before = [0]*10 + + def draw(self, ctx: Context) -> None: + if self.state == 0: + return + ctx.line_width = 18 + ctx.line_join = ctx.BEVEL + ctx.rgb(0, 0, 0).rectangle(-120, -120, 240, 240).fill() + ctx.rgb(0.3, 0.3, 0.3).arc(0, 0, 90, 0, 2.39, 0).arc(0, 0, 90, 2.79, 6.29, 0).stroke() + ctx.rgb(0.9, 0.3, 0.1).move_to(-96, 53).line_to(-1, -43).line_to(71, 29).stroke() + + def do_connect(self): + self.nic.active(True) + if not self.nic.isconnected(): + print('connecting to network...') + self.nic.connect(WIFI_SSID, WIFI_PSK) + while not self.nic.isconnected(): + pass + print('network config:', self.nic.ifconfig()) + + def think(self, ins: InputState, delta_ms: int) -> None: + if self.nic.status() not in [network.STAT_CONNECTING, network.STAT_GOT_IP]: + self.do_connect() + if self.nic.status() == network.STAT_GOT_IP and self.state == 0: + gc.collect() + self.mqtt.connect() + self.state = 1 + if self.state == 1: + for i in range(5): + if ins.captouch.petals[i*2].pads.cw or ins.captouch.petals[i*2].pads.ccw: + if self.before[i*2] == 0: + self.mqtt.publish(self.base_topic, b'{{"petal":"{}","event":"press_tip"}}'.format(i)) + self.before[i*2] = 1 + else: + self.before[i*2] = 0 + if ins.captouch.petals[i*2].pads.base: + if self.before[i*2+1] == 0: + self.mqtt.publish(self.base_topic, b'{{"petal":"{}","event":"press_base"}}'.format(i)) + self.before[i*2+1] = 1 + else: + self.before[i*2+1] = 0 + + +if __name__ == "__main__": + import st3m.run + st3m.run.run_view(Flow3rOpenhabMqtt(ApplicationContext())) diff --git a/flow3r.toml b/flow3r.toml new file mode 100644 index 0000000..61449d2 --- /dev/null +++ b/flow3r.toml @@ -0,0 +1,11 @@ +[app] +name = "OpenHAB (MQTT)" +menu = "Apps" + +[entry] +class = "Flow3rOpenhabMqtt" + +[metadata] +author = "s3lph" +license = "MIT" +url = "https://git.kabelsalat.ch/s3lph/flow3r-openhab"