sgbaird commited on
Commit
8197dfa
·
1 Parent(s): 9937236
scripts/{basic_rgb_led_sensor.py → basic_send_receive.py} RENAMED
@@ -1,17 +1,11 @@
1
- """
2
- https://ac-microcourses.readthedocs.io/en/latest/courses/hello-world/1.4.1-onboard-led-temp.html
3
-
4
- permalink: https://github.com/AccelerationConsortium/ac-microcourses/blob/07dc9a0286ded2e21ea64f02d8ae697717e786b9/docs/courses/hello-world/1.4.1-onboard-led-temp.ipynb
5
- """
6
-
7
  import paho.mqtt.client as mqtt
8
  import json
9
  from queue import Queue
10
 
11
- PICO_ID = "test" # UPDATE THIS TO YOUR ID
12
 
13
- command_topic = f"sdl-demo/picow/{PICO_ID}/GPIO/28/"
14
- sensor_data_topic = f"sdl-demo/picow/{PICO_ID}/as7341/"
15
 
16
  HIVEMQ_USERNAME = "sgbaird"
17
  HIVEMQ_PASSWORD = "D.Pq5gYtejYbU#L"
@@ -20,27 +14,31 @@ HIVEMQ_HOST = "248cc294c37642359297f75b7b023374.s2.eu.hivemq.cloud"
20
  sensor_data_queue: "Queue[dict]" = Queue()
21
 
22
 
23
- def get_paho_client(
24
- sensor_data_topic, hostname, username, password=None, port=8883, tls=True
25
- ):
26
- client = mqtt.Client(protocol=mqtt.MQTTv5) # create new instance
 
27
 
 
 
 
 
 
28
  def on_message(client, userdata, msg):
29
  sensor_data_queue.put(json.loads(msg.payload))
30
 
31
  def on_connect(client, userdata, flags, rc, properties=None):
32
  if rc != 0:
33
  print("Connected with result code " + str(rc))
34
- client.subscribe(sensor_data_topic, qos=1)
35
 
36
  client.on_connect = on_connect
37
  client.on_message = on_message
38
 
39
- if tls:
40
- client.tls_set(tls_version=mqtt.ssl.PROTOCOL_TLS_CLIENT)
41
  client.username_pw_set(username, password)
42
  client.connect(hostname, port)
43
- client.subscribe(sensor_data_topic, qos=2)
44
 
45
  return client
46
 
@@ -56,12 +54,13 @@ def send_and_receive(client, command_topic, msg, queue_timeout=60):
56
  return sensor_data
57
 
58
 
59
- client = get_paho_client(
60
- sensor_data_topic, HIVEMQ_HOST, HIVEMQ_USERNAME, password=HIVEMQ_PASSWORD
 
 
61
  )
62
 
63
- command_msg = {"R": 10, "G": 10, "B": 10}
 
64
  sensor_data = send_and_receive(client, command_topic, command_msg, queue_timeout=30)
65
  print(sensor_data)
66
-
67
- 1 + 1
 
 
 
 
 
 
 
1
  import paho.mqtt.client as mqtt
2
  import json
3
  from queue import Queue
4
 
5
+ PICO_ID = "test-fan" # UPDATE THIS TO YOUR ID
6
 
7
+ command_topic = f"fan-control/picow/{PICO_ID}/speed"
8
+ sensor_data_topic = f"fan-control/picow/{PICO_ID}/rpm"
9
 
10
  HIVEMQ_USERNAME = "sgbaird"
11
  HIVEMQ_PASSWORD = "D.Pq5gYtejYbU#L"
 
14
  sensor_data_queue: "Queue[dict]" = Queue()
15
 
16
 
17
+ def create_paho_client(tls=True):
18
+ client = mqtt.Client(protocol=mqtt.MQTTv5)
19
+ if tls:
20
+ client.tls_set(tls_version=mqtt.ssl.PROTOCOL_TLS_CLIENT)
21
+ return client
22
 
23
+
24
+ # Define setup separately since sensor_data is dynamic
25
+ def setup_paho_client(
26
+ client, rpm_data_topic, hostname, username, password=None, port=8883
27
+ ):
28
  def on_message(client, userdata, msg):
29
  sensor_data_queue.put(json.loads(msg.payload))
30
 
31
  def on_connect(client, userdata, flags, rc, properties=None):
32
  if rc != 0:
33
  print("Connected with result code " + str(rc))
34
+ client.subscribe(rpm_data_topic, qos=1)
35
 
36
  client.on_connect = on_connect
37
  client.on_message = on_message
38
 
 
 
39
  client.username_pw_set(username, password)
40
  client.connect(hostname, port)
41
+ client.loop_start() # Use a non-blocking loop
42
 
43
  return client
44
 
 
54
  return sensor_data
55
 
56
 
57
+ client = create_paho_client(tls=True)
58
+
59
+ client = setup_paho_client(
60
+ client, sensor_data_topic, HIVEMQ_HOST, HIVEMQ_USERNAME, password=HIVEMQ_PASSWORD
61
  )
62
 
63
+ # Sending speed command
64
+ command_msg = {"speed": 50} # Update the speed value as needed (0-100)
65
  sensor_data = send_and_receive(client, command_topic, command_msg, queue_timeout=30)
66
  print(sensor_data)
 
 
scripts/onboard_led_temp.py DELETED
@@ -1,73 +0,0 @@
1
- """
2
- https://ac-microcourses.readthedocs.io/en/latest/courses/hello-world/1.4.1-onboard-led-temp.html
3
-
4
- permalink: https://github.com/AccelerationConsortium/ac-microcourses/blob/07dc9a0286ded2e21ea64f02d8ae697717e786b9/docs/courses/hello-world/1.4.1-onboard-led-temp.ipynb
5
- """
6
-
7
- COURSE_ID = "<your_id_here>" # UPDATE THIS TO YOUR ID
8
-
9
- command_topic = f"{COURSE_ID}/onboard_led"
10
- sensor_data_topic = f"{COURSE_ID}/onboard_temp"
11
-
12
- HIVEMQ_USERNAME = "sgbaird"
13
- HIVEMQ_PASSWORD = "D.Pq5gYtejYbU#L"
14
- HIVEMQ_HOST = "248cc294c37642359297f75b7b023374.s2.eu.hivemq.cloud"
15
-
16
- import paho.mqtt.client as mqtt
17
- import json
18
- from queue import Queue
19
-
20
- sensor_data_queue: "Queue[dict]" = Queue()
21
-
22
-
23
- def get_paho_client(
24
- sensor_data_topic, hostname, username, password=None, port=8883, tls=True
25
- ):
26
-
27
- client = mqtt.Client(
28
- mqtt.CallbackAPIVersion.VERSION2, protocol=mqtt.MQTTv5
29
- ) # create new instance
30
-
31
- def on_message(client, userdata, msg):
32
- sensor_data_queue.put(json.loads(msg.payload))
33
-
34
- # The callback for when the client receives a CONNACK response from the server.
35
- def on_connect(client, userdata, flags, rc, properties=None):
36
- if rc != 0:
37
- print("Connected with result code " + str(rc))
38
- # Subscribing in on_connect() means that if we lose the connection and
39
- # reconnect then subscriptions will be renewed.
40
- client.subscribe(sensor_data_topic, qos=1)
41
-
42
- client.on_connect = on_connect
43
- client.on_message = on_message
44
-
45
- # enable TLS for secure connection
46
- if tls:
47
- client.tls_set(tls_version=mqtt.ssl.PROTOCOL_TLS_CLIENT)
48
- # set username and password
49
- client.username_pw_set(username, password)
50
- # connect to HiveMQ Cloud on port 8883 (default for MQTT)
51
- client.connect(hostname, port)
52
- client.subscribe(sensor_data_topic, qos=2)
53
-
54
- return client
55
-
56
-
57
- def send_and_receive(client, command_topic, msg, queue_timeout=60):
58
- client.publish(command_topic, msg, qos=2)
59
-
60
- client.loop_start()
61
-
62
- while True:
63
- sensor_data = sensor_data_queue.get(True, queue_timeout)
64
- client.loop_stop()
65
- return sensor_data
66
-
67
-
68
- client = get_paho_client(
69
- sensor_data_topic, HIVEMQ_HOST, HIVEMQ_USERNAME, password=HIVEMQ_PASSWORD
70
- )
71
-
72
- onboard_temp = send_and_receive(client, command_topic, "toggle", queue_timeout=30)
73
- print(onboard_temp)