Browse Source

add average value

master
Thomas Jensen 1 year 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 = {
}

msgs = []
temps = []


def add_msg_to_queue(topic, payload):
msg = {
'topic': 'sensor/office/temp/{}'.format(topic),
'payload': "{:.2f}".format(payload),
'qos': 0,
'retain': False
}

msgs.append(msg)


while True:
for sensor in W1ThermSensor.get_available_sensors():
msg = {
'topic': 'sensor/office/temp/{}'.format(sensors[sensor.id]),
'payload': "{:.2f}".format(sensor.get_temperature()),
'qos': 0,
'retain': False
}
temp = sensor.get_temperature()

add_msg_to_queue(sensors[sensor.id], temp)
print("Sensor {} has temperature {:.2f}".format(sensors[sensor.id], temp))

msgs.append(msg)
print("Sensor {} has temperature {:.2f}".format(sensors[sensor.id], sensor.get_temperature()))
temps.append(temp)

average = sum(temps)/len(temps)
print("Average temperature is {:.2f}".format(average))
add_msg_to_queue('average', average)
publish.multiple(msgs, hostname="192.168.1.119", client_id="rack_temp")

temps.clear()
time.sleep(15)

Loading…
Cancel
Save