|
""" |
|
https://ac-microcourses.readthedocs.io/en/latest/courses/hello-world/1.4.1-onboard-led-temp.html |
|
|
|
permalink: https://github.com/AccelerationConsortium/ac-microcourses/blob/07dc9a0286ded2e21ea64f02d8ae697717e786b9/docs/courses/hello-world/1.4.1-onboard-led-temp.ipynb |
|
""" |
|
|
|
COURSE_ID = "<your_id_here>" |
|
|
|
command_topic = f"{COURSE_ID}/onboard_led" |
|
sensor_data_topic = f"{COURSE_ID}/onboard_temp" |
|
|
|
HIVEMQ_USERNAME = "sgbaird" |
|
HIVEMQ_PASSWORD = "D.Pq5gYtejYbU#L" |
|
HIVEMQ_HOST = "248cc294c37642359297f75b7b023374.s2.eu.hivemq.cloud" |
|
|
|
import paho.mqtt.client as mqtt |
|
import json |
|
from queue import Queue |
|
|
|
sensor_data_queue: "Queue[dict]" = Queue() |
|
|
|
|
|
def get_paho_client( |
|
sensor_data_topic, hostname, username, password=None, port=8883, tls=True |
|
): |
|
|
|
client = mqtt.Client( |
|
mqtt.CallbackAPIVersion.VERSION2, protocol=mqtt.MQTTv5 |
|
) |
|
|
|
def on_message(client, userdata, msg): |
|
sensor_data_queue.put(json.loads(msg.payload)) |
|
|
|
|
|
def on_connect(client, userdata, flags, rc, properties=None): |
|
if rc != 0: |
|
print("Connected with result code " + str(rc)) |
|
|
|
|
|
client.subscribe(sensor_data_topic, qos=1) |
|
|
|
client.on_connect = on_connect |
|
client.on_message = on_message |
|
|
|
|
|
if tls: |
|
client.tls_set(tls_version=mqtt.ssl.PROTOCOL_TLS_CLIENT) |
|
|
|
client.username_pw_set(username, password) |
|
|
|
client.connect(hostname, port) |
|
client.subscribe(sensor_data_topic, qos=2) |
|
|
|
return client |
|
|
|
|
|
def send_and_receive(client, command_topic, msg, queue_timeout=60): |
|
client.publish(command_topic, msg, qos=2) |
|
|
|
client.loop_start() |
|
|
|
while True: |
|
sensor_data = sensor_data_queue.get(True, queue_timeout) |
|
client.loop_stop() |
|
return sensor_data |
|
|
|
|
|
client = get_paho_client( |
|
sensor_data_topic, HIVEMQ_HOST, HIVEMQ_USERNAME, password=HIVEMQ_PASSWORD |
|
) |
|
|
|
onboard_temp = send_and_receive(client, command_topic, "toggle", queue_timeout=30) |
|
print(onboard_temp) |
|
|