-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdanfoss-modbus-mqtt.py
executable file
·100 lines (73 loc) · 2.47 KB
/
danfoss-modbus-mqtt.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/python
# Read temperature from Danfoss EKC202 and publish it on MQTT.
# Eirik Haustveit, 2016
import serial
import time
import crc16
import struct
import paho.mqtt.client as mqtt
ser = serial.Serial('/dev/danfoss', 9600, timeout=1, rtscts=False)
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_EVEN #set parity check: no parity
ser.stopbits = serial.STOPBITS_ONE #number of stop bits
ser.xonxoff = False #disable software flow control
ser.rtscts = False #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False #disable hardware (DSR/DTR) flow control
ser.writeTimeout = 0.1 #timeout for write
ser.readTimeout = 0.1
#print ser
#address = '\x09'
#command = '\x11'
#
#data = address + command
#data = data + compute_crc(data)
#
#print('Transmitting: ' + str(data))
#ser.rts = True
#
#ser.dtr = True
#time.sleep(0.1)
#ser.write(data)
#
#ser.write(':010310010001EA\r\n')
#print repr(ser.read(1000)) # Read 1000 bytes, or wait for timeout
def read_input_register(dev, reg):
command = chr(dev) + '\x04' + struct.pack('>H',reg) + '\x00\x01'
crc = crc16.calcString(command, 0xFFFF)
command = command + struct.pack('<H',crc)
#print 'Register: ' + str(reg)
#print ":".join("{:02x}".format(ord(c)) for c in command)
ser.rts = True
ser.write(command)
time.sleep(0.008)
ser.rts = False
time.sleep(1)
response = ser.readline()
#TODO: Implement check of checksum(CRC), and handle any modbus error messages
#checksum = ord(response[5:7])
#comp_checksum = struct.pack('<H',crc16.calcString(response[1:5], 0xFFFF))
#print 'chk: ' + str(comp_checksum)
return response
def main():
mqttc = mqtt.Client()
mqttc.connect("localhost")
mqttc.loop_start()
# while True:
response = read_input_register(1, 2575)
if response:
print ":".join("{:02x}".format(ord(c)) for c in response)
temperature = ord(response[4:5])/10.0
print 'Temperature: ' + str(temperature)
mqttc.publish("ekc2021/temperature", temperature)
else:
print "No data for EKC module at address 1"
response = read_input_register(2, 2575)
if response:
temperature = ord(response[4:5])/10.0
print 'Temperature: ' + str(temperature)
mqttc.publish("ekc2022/temperature", temperature)
else:
print "No data for EKC module at address 2"
# time.sleep(2)
if __name__ == '__main__':
main()