From b5ec64d7e0691b3b1a7ec368d5ae044910054a6b Mon Sep 17 00:00:00 2001 From: Jonas Meeuws Date: Tue, 17 Sep 2024 17:14:07 +0200 Subject: [PATCH] Blink kelder lights on good scan (+ some debug statements for fixing leddy) --- vinscant/mpfiles/main.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/vinscant/mpfiles/main.py b/vinscant/mpfiles/main.py index efcf565..e42b6d5 100644 --- a/vinscant/mpfiles/main.py +++ b/vinscant/mpfiles/main.py @@ -4,6 +4,7 @@ import gc import time import urequests as req +from umqtt.robust import MQTTClient from lib.mfrc522 import MFRC522 import lib.term_color as tc @@ -82,6 +83,9 @@ def good(self, name=None): Timer(0).init(period=500, mode=Timer.ONE_SHOT, callback=self.gotoSleep) if name: leddy.setText(f"Welkom {name}!") + mqtt = Mqtt() + mqtt.blink() + mqtt.close() def error(self): self.led.setColor(*StatusNotifier.colors[0]) @@ -89,7 +93,7 @@ def error(self): self.gotoSleep() class Leddy: - def __init__(self, address="http://10.0.2.3") -> None: + def __init__(self, address="http://leddy") -> None: self.address = address def _post(self, command: str): @@ -102,11 +106,23 @@ def setText(self, text: str): watchdog.feed() self._post(f"Option autoResetMs {5 * 1000}") watchdog.feed() - time.sleep(.5) + time.sleep(1) watchdog.feed() self._post(f"ScrollingText {text}") watchdog.feed() +class Mqtt: + def __init__(self, name="vinscant", host="korner", port="1883") -> None: + self.client = MQTTClient(name, host, port) + self.client.connect() + + def blink(self): + topic = 'zigbee2mqtt/lights/set' + payload = '{"effect": "blink"}' + self.client.publish(bytes(topic, 'utf-8'), bytes(payload, 'utf-8')) + + def close(self): + self.client.disconnect() print("Starting Vinscant...") @@ -180,10 +196,10 @@ def setText(self, text: str): watchdog.feed() if 200 <= res.status_code < 300: name = res.text - print(f"vingo: {tc.GREEN}{name}{tc.RESET}") + print(f"vingo: {res.status_code} {tc.GREEN}{name}{tc.RESET}") notifier.good(name) else: - print(f"vingo: {tc.YELLOW}{res.text}{tc.RESET}") + print(f"vingo: {res.status_code} {tc.YELLOW}{res.text}{tc.RESET}") notifier.error() res.close() # does not support with statements