forked from matthijsvisser/kamstrup-402-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kamstrup_meter.py
executable file
·221 lines (194 loc) · 5.05 KB
/
kamstrup_meter.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/python
#
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
# ----------------------------------------------------------------------------
#
# Modified by Frank Reijn and Paul Bonnemaijers for Kamstrup Multical 402
# Modified by Matthijs Visser, refactored and simplified code
import serial
import math
import sys
import datetime
import json
import urllib.request
import logging
import time
from logging.handlers import TimedRotatingFileHandler
log = logging.getLogger("log")
log.setLevel(logging.INFO)
kamstrup_402_params = {
"energy" : 0x3C,
"power" : 0x50,
"temp1" : 0x56,
"temp2" : 0x57,
"tempdiff" : 0x59,
"flow" : 0x4A,
"volume" : 0x44,
"minflow_m" : 0x8D,
"maxflow_m" : 0x8B,
"minflowDate_m" : 0x8C,
"maxflowDate_m" : 0x8A,
"minpower_m" : 0x91,
"maxpower_m" : 0x8F,
"avgtemp1_m" : 0x95,
"avgtemp2_m" : 0x96,
"minpowerdate_m": 0x90,
"maxpowerdate_m": 0x8E,
"minflow_y" : 0x7E,
"maxflow_y" : 0x7C,
"minflowdate_y" : 0x7D,
"maxflowdate_y" : 0x7B,
"minpower_y" : 0x82,
"maxpower_y" : 0x80,
"avgtemp1_y" : 0x92,
"avgtemp2_y" : 0x93,
"minpowerdate_y": 0x81,
"maxpowerdate_y": 0x7F,
"temp1xm3" : 0x61,
"temp2xm3" : 0x6E,
"infoevent" : 0x71,
"hourcounter" : 0x3EC,
}
# Kamstrup uses the "true" CCITT CRC-16
def crc_1021(message):
poly = 0x1021
reg = 0x0000
for byte in message:
mask = 0x80
while (mask > 0):
reg <<= 1
if byte & mask:
reg |= 1
mask >>= 1
if reg & 0x10000:
reg &= 0xffff
reg ^= poly
return reg
# Byte values which must be escaped before transmission
escapes = {
0x06: True,
0x0d: True,
0x1b: True,
0x40: True,
0x80: True,
}
class kamstrup(object):
def __init__ (self, port, parameters):
self.serial_port = port
self.parameters = parameters
try:
self.serial = serial.Serial(
port = self.serial_port,
baudrate = 1200,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_TWO,
bytesize = serial.EIGHTBITS,
timeout = 2.0)
except serial.SerialException as e:
log.exception(e)
def run (self):
values = {}
if self.serial.is_open:
self.close()
if self.open():
for parameter in self.parameters:
value = self.readparameter(int(str(kamstrup_402_params[parameter]), 0))
if value is not None:
values[parameter] = value
self.close()
return values
def open (self):
try:
self.serial.open()
log.debug('Opened serial port')
return True
except (ValueError, Exception) as e:
log.error(e)
return False
def close (self):
self.serial.close()
log.debug('Closed serial port')
def rd (self):
receivedByte = self.serial.read(size=1)
if len(receivedByte) == 0:
log.debug("Rx timeout")
return None
byte = bytearray(receivedByte)[0]
return byte
def send (self, prefix, msg):
message = bytearray(msg)
command = bytearray()
message.append(0)
message.append(0)
checksum = crc_1021(message)
message[-2] = checksum >> 8
message[-1] = checksum & 0xff
command.append(prefix)
for byte in message:
if byte in escapes:
command.append(0x1b)
command.append(byte ^ 0xff)
else:
command.append(byte)
command.append(0x0d)
try:
self.serial.write(command)
except serial.SerialTimeoutException as e:
log.exception(e.message)
def recv (self):
receivedMessage = bytearray()
filteredMessage = bytearray()
while True:
receivedByte = self.rd()
if receivedByte == None:
return None
if receivedByte == 0x40:
receivedMessage = bytearray()
receivedMessage.append(receivedByte)
if receivedByte == 0x0d:
break
i = 1;
while i < len(receivedMessage) - 1:
if receivedMessage[i] == 0x1b:
value = receivedMessage[i + 1] ^ 0xff
if value not in escapes:
log.warning("Missing Escape %02x" % value)
filteredMessage.append(value)
i += 2
else:
filteredMessage.append(receivedMessage[i])
i += 1
if crc_1021(filteredMessage):
log.error("CRC error")
return None
return filteredMessage[:-2]
def readparameter (self, parameter):
self.send(0x80, (0x3f, 0x10, 0x01, parameter >> 8, parameter & 0xff))
receivedMessage = self.recv()
if (receivedMessage == None):
log.warning('No response from meter')
return None
elif ((receivedMessage[0] != 0x3f) or
(receivedMessage[1] != 0x10) or
(receivedMessage[2] != parameter >> 8) or
(receivedMessage[3] != parameter & 0xff)):
log.warning('Message is invalid')
return None
# Decode the mantissa
value = 0
for i in range(0, receivedMessage[5]):
value <<= 8
value |= receivedMessage[i + 7]
# Decode the exponent
i = receivedMessage[6] & 0x3f
if receivedMessage[6] & 0x40:
i = -i
i = math.pow(10,i)
if receivedMessage[6] & 0x80:
i = -i
value *= i
return float(value)