Browse Source

add sensor status topic

Thomas Jensen 1 month ago
parent
commit
c70b1e3eff
1 changed files with 19 additions and 3 deletions
  1. 19
    3
      temp_sensor.py

+ 19
- 3
temp_sensor.py View File

@@ -11,6 +11,7 @@ sensors = {
11 11
 
12 12
 msgs = []
13 13
 temps = []
14
+available = []
14 15
 
15 16
 
16 17
 def add_msg_to_queue(topic, payload):
@@ -22,6 +23,20 @@ def add_msg_to_queue(topic, payload):
22 23
   }
23 24
 
24 25
   msgs.append(msg)
26
+  print(msg)
27
+
28
+
29
+def set_sensor_status():
30
+  for uuid, sensor in sensors.items():
31
+    msg = {
32
+      'topic': 'sensor/office/temp/{}/status'.format(sensor),
33
+      'payload': 'online' if sensor in available else 'offline',
34
+      'qos': 0,
35
+      'retain': False
36
+    }
37
+
38
+    msgs.append(msg)
39
+    print(msg)
25 40
 
26 41
 
27 42
 while True:
@@ -29,15 +44,16 @@ while True:
29 44
     temp = sensor.get_temperature()
30 45
 
31 46
     add_msg_to_queue(sensors[sensor.id], temp)
32
-    print("Sensor {} has temperature {:.2f}".format(sensors[sensor.id], temp))
33 47
 
34 48
     temps.append(temp)
49
+    available.append(sensors[sensor.id])
35 50
 
36 51
   average = sum(temps)/len(temps)
37
-  print("Average temperature is {:.2f}".format(average))
52
+
53
+  set_sensor_status()
38 54
   add_msg_to_queue('average', average)
39
-  
40 55
   publish.multiple(msgs, hostname="192.168.1.119", client_id="rack_temp")
41 56
 
42 57
   temps.clear()
58
+  available.clear()
43 59
   time.sleep(15)

Loading…
Cancel
Save