From ad40b59053f72addac1153cf3e0b1686c86779de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ebbe=20Ba=C3=9F?= Date: Fri, 16 Feb 2024 19:42:33 +0100 Subject: [PATCH] added on message and mqtt sub MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ebbe Baß --- client/main.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/client/main.py b/client/main.py index 5cda4ca..f0ae10c 100644 --- a/client/main.py +++ b/client/main.py @@ -56,18 +56,16 @@ def is_connected_to_wifi(): output = subprocess.check_output(['iwgetid']) return output.split('"')[1] is not None -def update_led_strip(r, g, b, dmx_address, strip, LED_PER_PIXEL): - for i in range(LED_COUNT): - pixel_index = i // LEDS_PER_PIXEL - dmx_index = dmx_address + (pixel_index * 3) - - strip[i] = Color(r, g, b) +def update_led_strip(r, g, b, pixel, strip): + strip[int(pixel)] = Color(r, g, b) def mqtt_listner(universe, dmx_address, strip, LEDS_PER_PIXEL): try: while True: - - update_led_strip(values, dmx_address, strip, LEDS_PER_PIXEL) + for i in range(LED_COUNT): + pixel_index = i // LEDS_PER_PIXEL + dmx_index = dmx_address + (pixel_index * 3) + update_led_strip(r, g, b, pixel, strip) except Exception as e: print(f"Error: {e}") @@ -81,6 +79,10 @@ def loopCheckSettingUpdates(): print(f"Error: {e}") time.sleep(2) +def on_message(mqttc, obj, msg): + output = [msg.topic, msg.payload, msg.qos] + return output + if __name__ == "__main__": # Connect to Wi-Fi if is_connected_to_wifi() is not None: @@ -91,6 +93,12 @@ if __name__ == "__main__": global dmx_address universe, dmx_address = get_assigned_params() + mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) + mqttc.connect("192.168.0.1", 1883, 60) + mqttc.on_message = on_message + for address in range(dmx_address): + mqttc.subscribe(str(universe)+"/"+str(address), 0) + settingsUpdateThread = Thread(target=loopCheckSettingUpdates, args=()) pixelUpdateThread = Thread(target=mqtt_listner, args=(universe, dmx_address, strip, LEDS_PER_PIXEL))