-
Notifications
You must be signed in to change notification settings - Fork 0
/
td3511_mbus.be
246 lines (233 loc) · 7.43 KB
/
td3511_mbus.be
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# Berry Script for Smart-Meter Siemens TD-3511 in Upper Austria (Netz OÖ)
# Read Values via M-Bus-Protocol every Second
class TD3511MBUS : Driver
#########################
# Defaults, if not set in Constructor
# AES-Key
static var DEF_KEY = bytes('11C5151F9CB6EFD13E411B815CD62769')
# Serial RX-Pin
static var DEF_RX_PIN = 46
# Serial TX-Pin
static var DEF_TX_PIN = 45
# ID for Smart-Meter in MQTT and Main-View
static var DEF_ID = 'M1'
##########################
# serial port
var ser
# AES KEy
var key
# ID for Smart-Meter
var sm_id
# max number of tries for reading more data
var reread_count
# Values from smart meter
var zeit
var datum
var r_1_7_0
var r_2_7_0
var r_3_7_0
var r_4_7_0
var r_1_8_0
var r_2_8_0
var r_3_8_1
var r_4_8_1
var r_1_128_0
#Constructor
# Parameters
# rx rx-pin for serial
# tx tx-pin for Serial
# aes_key AES-Key for Smart-Meter
# id ID for Smart-Meter in MQTT and MainView
def init(rx, tx, aes_key, id)
# default if nil
if !tx tx=self.DEF_TX_PIN end
if !rx rx=self.DEF_RX_PIN end
if !aes_key self.key = self.DEF_KEY else self.key = aes_key end
if !id self.sm_id = self.DEF_ID else self.sm_id = id end
self.reread_count = 0
self.zeit = ""
self.datum = ""
self.r_1_7_0 = 0.0
self.r_2_7_0 = 0.0
self.r_1_8_0 = 0.0
self.r_2_8_0 = 0.0
self.r_3_7_0 = 0.0
self.r_4_7_0 = 0.0
self.r_1_8_0 = 0.0
self.r_2_8_0 = 0.0
self.r_3_8_1 = 0.0
self.r_4_8_1 = 0.0
self.r_1_128_0 = 0.0
self.ser = serial(rx,tx,9600,serial.SERIAL_8E1)
end
def tddecode(message)
# decode a MBus-Message with smartmeter data
import crypto
import string
if message.size() != 101 return "wrong message size:" + str(message.size()) end
#print(message.tohex())
var iv = bytes(16)
iv = message[11..12] + message[7..10] + message[13..14]
for i: 0 .. 7
iv.add(message[15])
end
var payload = message[19..98]
#print("iv: " + iv.tohex())
#print("key:" + self.key.tohex())
#print("enc:" + payload.tohex())
var aes = crypto.AES_CBC()
aes.decrypt1(self.key, iv, payload)
#print("dec:" + payload.tohex())
if payload[0..1] != bytes('2F2F') return "wrong decoded message format" end
# decode the values
#print("1.7.0 : " + payload[44..47])
var v_0_9_12_all = payload[4..9]
#print("0.9.1+0.9.2 : " + v_0_9_12_all.tohex())
var v_0_9_1_sec = v_0_9_12_all[0] & 0x3F
var v_0_9_1_min = v_0_9_12_all[1] & 0x3F
var v_0_9_1_hour = v_0_9_12_all[2] & 0x1F
var v_0_9_1_day = v_0_9_12_all[3] & 0x1F
var v_0_9_1_mon = (v_0_9_12_all[4] & 0x0F)
var v_0_9_1_year = ((v_0_9_12_all[3] & 0xE0) >>5 ) | ((v_0_9_12_all[4] & 0xF0) >>1) + 2000
var v_1_7_0 = payload[44..47].get(0,4)
var v_2_7_0 = payload[51..54].get(0,4)
var v_1_8_0 = payload[12..15].get(0,4)
var v_2_8_0 = payload[19..22].get(0,4)
var v_3_7_0 = payload[58..61].get(0,4)
var v_4_7_0 = payload[66..59].get(0,4)
var v_3_8_1 = payload[28..31].get(0,4)
var v_4_8_1 = payload[38..41].get(0,4)
var v_1_128_0 = payload[74..77].geti(0,4)
self.zeit = string.format("%02d:%02d:%02d",v_0_9_1_hour, v_0_9_1_min, v_0_9_1_sec)
self.datum = string.format("%04d-%02d-%02d",v_0_9_1_year, v_0_9_1_mon, v_0_9_1_day)
self.r_1_7_0 = v_1_7_0
self.r_2_7_0 = v_2_7_0
# in k
self.r_1_8_0 = v_1_8_0/1000.0
self.r_2_8_0 = v_2_8_0/1000.0
self.r_3_7_0 = v_3_7_0
self.r_4_7_0 = v_4_7_0
#in k
self.r_3_8_1 = v_3_8_1/1000.0
self.r_4_8_1 = v_4_8_1/1000.0
self.r_1_128_0 = v_1_128_0/1000.0
print(self.sm_id, "Zeit:" + self.zeit)
print(self.sm_id, "Datum:" + self.datum)
print(self.sm_id, string.format("1.7.0 : %9d W", self.r_1_7_0))
print(self.sm_id, string.format("2.7.0 : %9d W", self.r_2_7_0))
print(self.sm_id, string.format("1.8.0 : %9.3f kWh", self.r_1_8_0))
print(self.sm_id, string.format("2.8.0 : %9.3f kWh", self.r_2_8_0))
print(self.sm_id, string.format("3.7.0 : %9d var", self.r_3_7_0))
print(self.sm_id, string.format("4.7.0 : %9d var", self.r_4_7_0))
print(self.sm_id, string.format("3.8.1 : %9.3f kvarh", self.r_3_8_1))
print(self.sm_id, string.format("4.8.1 : %9.3f kvarh", self.r_4_8_1))
print(self.sm_id, string.format("1.128.0 : %9.3f kWh", self.r_1_128_0))
return "OK"
end
def tdread()
# read data from serial line and interpret content
import string
if self.ser.available()<5
return
#return "no data(<5)"
end
if self.ser.available()==5 && self.ser.read()==bytes('1040F03016')
# got Slave-Query from Master for Adress 240(F0)
self.ser.write(bytes('E5'))
print("got SND_NKE, sent ACK")
return
end
if self.ser.available()> 5 && self.ser.available() <101
# we got not enough data
# wait for more data max 5 times
self.reread_count += 1
if self.reread_count > 5
#read and drop data
self.ser.read()
self.reread_count = 0
print("droped wrong size message")
return
end
print("waiting for more data..." )
return
end
if self.ser.available()>101
#got too much, empty buffer
#return str(ser.available()) + ":" + ser.read().tohex()
self.ser.read()
return
end
if self.ser.available()==101
# we got a SND_UD with data?
var message = self.ser.read()
#print(message.tohex())
if message[0] != 0x68
return "wrong message StartByte:" + string.hex(message[0])
end
if message[1] != message[2]
return "LengthBytes differ" + string.hex(message[1..2])
end
if message[100] != 0x16
return "wrong message EndByte:" + string.hex(message[100])
end
# Send ACK to get next message
self.ser.write(bytes('E5'))
# decode the message
print(self.tddecode(message))
return
end
print("unknown state: " + str(self.ser.available()) + ":" + self.ser.read().tohex())
return
end
#- display values in the web UI -#
def web_sensor()
if !self.ser return nil end
import string
var msg = string.format(
"{s}Z1 Zeit{m}%s"..
"{s}Z1 Datum{m}%s"..
"{s}Z1 1.7.0(Wirk-L. P+){m} %.0f W{e}"..
"{s}Z1 2.7.0(Wirk-L. P-){m} %.0f W{e}"..
"{s}Z1 1.7.0 - 2.7.0(Saldo-L.){m} %.0f W{e}"..
"{s}Z1 1.8.0(Energie A+){m} %9.3f kWh{e}"..
"{s}Z1 2.8.0(Energie A-){m} %9.3f kWh{e}"..
"{s}Z1 3.7.0(Blind-L. Q+){m} %.0f var{e}"..
"{s}Z1 4.7.0(Blind-L. Q-){m} %.0f var{e}"..
"{s}Z1 3.8.1(Energie R+){m} %9.3f kvarh{e}"..
"{s}Z1 4.8.1(Energie R-){m} %9.3f kvarh{e}"..
"{s}Z1 1.128.0(Inkasso-Energie){m} %9.3f kWh{e}",
self.zeit,self.datum,
self.r_1_7_0,
self.r_2_7_0,
self.r_1_7_0 - self.r_2_7_0,
self.r_1_8_0,
self.r_2_8_0,
self.r_3_7_0,
self.r_4_7_0,
self.r_3_8_1,
self.r_4_8_1,
self.r_1_128_0
)
# replace Z1 with defined smart meter ID
msg = string.replace(msg,"Z1",self.sm_id)
# send to gui with decimal point as locale defined
tasmota.web_send_decimal(msg)
end
#- add smart meter value to teleperiod -#
def json_append()
import string
var msg = string.format(",\"%s\":{\"time\":\"%s\",\"date\":\"%s\",\"Total_in\":%9.3f,\"Total_out\":%9.3f,\"P_in\":%.0f,\"P_out\":%.0f,\"P_total\":%.0f}",
self.sm_id,
self.zeit, self.datum,
self.r_1_8_0,
self.r_2_8_0,
self.r_1_7_0,
self.r_2_7_0,
self.r_1_7_0 - self.r_2_7_0)
tasmota.response_append(msg)
end
# Called by tasmota every X ms
def every_100ms()
self.tdread()
end
end