-
Notifications
You must be signed in to change notification settings - Fork 0
/
UMR2toMQTT.py
52 lines (46 loc) · 1.54 KB
/
UMR2toMQTT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 17 20:15:35 2023
@author: Ferdinand
"""
import json
import requests
import time
import paho.mqtt.client as mqtt
from requests.exceptions import HTTPError
#Define input variables
# Provide IP-address of UMR2
UMR2_IP = "192.168.2.138"
# Interval for UMR2 polling in seconds
timesleep = 10
# Define MQTT parameters
MQTT_HOST = "192.168.2.31"
MQTT_PORT = 1883
MQTT_TOPIC = "Vloerverwarming"
#Define derived variables
UMR2_URL = "http://" + UMR2_IP + "/get.json?f=$.status.*"
MQTT_KEEPALIVE_INTERVAL = 3 * timesleep
def main():
mqttc = mqtt.Client()
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
mqttc.loop_start()
while True:
try:
response = requests.get(UMR2_URL)
jsonResponse = response.json()
except HTTPError as http_err:
response=""
except Exception as err:
response=""
if response != "":
mode = str(jsonResponse['status']['outputs']['heater']['mode'])
pump = int(jsonResponse['status']['outputs']['pump']['speed'])
valve = int(jsonResponse['status']['outputs']['valves'][8]['state'])
Tsupply = jsonResponse['status']['inputs']['max']['temperature']
Treturn = jsonResponse['status']['inputs']['return']['temperature']
DeltaT = float(Tsupply) - float(Treturn)
MQTT_MSG = json.dumps({"Mode": mode,"Pomp": pump,"Klep": valve,"Tsupply": Tsupply,"Treturn": Treturn,"DeltaT": DeltaT});
mqttc.publish(MQTT_TOPIC, MQTT_MSG)
time.sleep(timesleep)
if __name__ == '__main__':
main()