From 0ec4645f544677a228d4ce3b93fd5f74b78e1ac9 Mon Sep 17 00:00:00 2001 From: "karl.hudgell" Date: Thu, 28 Oct 2021 10:06:23 +0100 Subject: [PATCH] working mqtt? --- .gitignore | 1 + .vscode/launch.json | 7 +++++ mqtt.py | 70 +++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 +- 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 mqtt.py diff --git a/.gitignore b/.gitignore index 0647239..8eb8f62 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ venv/ +test.py \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index dcb584e..036ed59 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,13 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal" + }, { "name": "Python: Flask", "type": "python", diff --git a/mqtt.py b/mqtt.py new file mode 100644 index 0000000..f375d05 --- /dev/null +++ b/mqtt.py @@ -0,0 +1,70 @@ +import random +import time +import json + +from paho.mqtt import client as mqtt_client +from server import run_bulb_action +from colormap import rgb2hex + + +broker = "vps.k-world.me.uk" +port = 1883 +topic = "ledLight/set" +# generate client ID with pub prefix randomly +client_id = f"python-mqtt-{random.randint(0, 100)}" +username = "karl" +password = "karlmax" + + +def convert_to_bulb_format(colour_map): + hex = rgb2hex( + colour_map["color"]["r"], colour_map["color"]["g"], colour_map["color"]["b"] + ) + hex = hex.replace("#", "") + value = "56" + hex + "00f0aa" + return value + + +def connect_mqtt() -> mqtt_client: + def on_connect(client, userdata, flags, rc): + if rc == 0: + print("Connected to MQTT Broker!") + else: + print("Failed to connect, return code %d\n", rc) + + client = mqtt_client.Client(client_id) + client.username_pw_set(username, password) + client.on_connect = on_connect + client.connect(broker, port) + return client + + +def publish(client): + result = client.publish("ledLight/state", "on") + status = result[0] + if status == 0: + print(f"Send `on` to topic `{topic}`") + else: + print(f"Failed to send message to topic {topic}") + + +def subscribe(client: mqtt_client): + def on_message(client, userdata, msg): + print(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic") + a = json.loads(msg.payload.decode()) + hex = convert_to_bulb_format(a) + run_bulb_action(hex) + + client.subscribe(topic) + client.on_message = on_message + + +def run(): + client = connect_mqtt() + publish(client) + subscribe(client) + client.loop_forever() + + +if __name__ == "__main__": + run() diff --git a/requirements.txt b/requirements.txt index 70f2994..4fea0ff 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ -ast black==21.9b0 click==8.0.3 colorama==0.4.4 @@ -11,6 +10,7 @@ itsdangerous==2.0.1 Jinja2==3.0.2 MarkupSafe==2.0.1 mypy-extensions==0.4.3 +paho-mqtt==1.6.1 pathspec==0.9.0 pexpect==4.8.0 platformdirs==2.4.0