Browse Source

add average value

master
Thomas Jensen 8 months ago
parent
commit
363dcc140a
1 changed files with 24 additions and 8 deletions
  1. 24
    8
      temp_sensor.py

+ 24
- 8
temp_sensor.py View File

@@ -10,18 +10,34 @@ sensors = {
10 10
 }
11 11
 
12 12
 msgs = []
13
+temps = []
14
+
15
+
16
+def add_msg_to_queue(topic, payload):
17
+  msg = {
18
+    'topic': 'sensor/office/temp/{}'.format(topic),
19
+    'payload': "{:.2f}".format(payload),
20
+    'qos': 0,
21
+    'retain': False
22
+  }
23
+
24
+  msgs.append(msg)
25
+
13 26
 
14 27
 while True:
15 28
   for sensor in W1ThermSensor.get_available_sensors():
16
-    msg = {
17
-      'topic': 'sensor/office/temp/{}'.format(sensors[sensor.id]),
18
-      'payload': "{:.2f}".format(sensor.get_temperature()),
19
-      'qos': 0,
20
-      'retain': False
21
-    }
29
+    temp = sensor.get_temperature()
30
+
31
+    add_msg_to_queue(sensors[sensor.id], temp)
32
+    print("Sensor {} has temperature {:.2f}".format(sensors[sensor.id], temp))
22 33
 
23
-    msgs.append(msg)
24
-    print("Sensor {} has temperature {:.2f}".format(sensors[sensor.id], sensor.get_temperature()))
34
+    temps.append(temp)
25 35
 
36
+  average = sum(temps)/len(temps)
37
+  print("Average temperature is {:.2f}".format(average))
38
+  add_msg_to_queue('average', average)
39
+  
26 40
   publish.multiple(msgs, hostname="192.168.1.119", client_id="rack_temp")
41
+
42
+  temps.clear()
27 43
   time.sleep(15)

Loading…
Cancel
Save