working mqtt?
This commit is contained in:
parent
3720863d15
commit
0ec4645f54
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
venv/
|
venv/
|
||||||
|
test.py
|
7
.vscode/launch.json
vendored
7
.vscode/launch.json
vendored
@ -4,6 +4,13 @@
|
|||||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Python: Current File",
|
||||||
|
"type": "python",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${file}",
|
||||||
|
"console": "integratedTerminal"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "Python: Flask",
|
"name": "Python: Flask",
|
||||||
"type": "python",
|
"type": "python",
|
||||||
|
70
mqtt.py
Normal file
70
mqtt.py
Normal file
@ -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()
|
@ -1,4 +1,3 @@
|
|||||||
ast
|
|
||||||
black==21.9b0
|
black==21.9b0
|
||||||
click==8.0.3
|
click==8.0.3
|
||||||
colorama==0.4.4
|
colorama==0.4.4
|
||||||
@ -11,6 +10,7 @@ itsdangerous==2.0.1
|
|||||||
Jinja2==3.0.2
|
Jinja2==3.0.2
|
||||||
MarkupSafe==2.0.1
|
MarkupSafe==2.0.1
|
||||||
mypy-extensions==0.4.3
|
mypy-extensions==0.4.3
|
||||||
|
paho-mqtt==1.6.1
|
||||||
pathspec==0.9.0
|
pathspec==0.9.0
|
||||||
pexpect==4.8.0
|
pexpect==4.8.0
|
||||||
platformdirs==2.4.0
|
platformdirs==2.4.0
|
||||||
|
Loading…
x
Reference in New Issue
Block a user