-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsml-modbus-mqtt.tas
285 lines (248 loc) · 5.38 KB
/
sml-modbus-mqtt.tas
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
>D
; variables SML
pcurr=0
; variables Modbus TCP
res=0
cnt=0
xcnt=0
reg=0
nval=0
dform=0
ecnt=0
ctime=0
vsml1=0
vsml2=0
vsml3=0
M:req=0 12
M:resp=0 9
M:data=0 66
; variables Modbus serial
Ap=0
Ae=0
Bp=0
Be=0
Cp=0
Ce=0
Dp=0
De=0
Ep=0
Ee=0
Fp=0
Fe=0
Gp=0
Ge=0
Hp=0
He=0
; variables Domoticz
usage=0
ret=0
cons=0
prod=0
>B
=>sensor53 r
;disable publishing at MQTT teleperiod, on boot
smlj=0
>BS
; init LED
spinm(2 1)
; start TCP server
res=wso(502)
print res=%res%
; create task 1 every 10ms on core1, priority 3, stack 8kb
ct(1 10 1 3 8000)
; set Modbus register values
#dosel
data[0]=0
; default = word
dform=1
switch reg
case 768
data={0}
case 1706
data={0}
case 40000
data={21365 28243}
case 40002
data={1 65}
case 40004
data={70 114 111 110 105 117 115 0 0 0 0 0 0 0 0 0}
data={83 109 97 114 116 32 77 101 116 101 114 32 54 51 65 0}
data={0 0 0 0 0 0 0 0}
data={0 0 0 0 0 0 0 0}
data={48 48 48 48 48 48 48 49 0 0 0 0 0 0 0 0}
data={240 213}
case 40069
data={213 124}
case 40071
data={0 0 0 0 0}
data={0 0 0 0 0}
data={0 0 0 vsml1 0}
data={0 0 0 0 0}
data={0 0 0 0 0}
data={0 0 0 0 0}
dform=3
nval=nval/2
case 40129
data={vsml3 0 0 0 vsml2}
data={0 0 0 0 0}
data={0 0 0 0 0}
data={0}
dform=3
nval=nval/2
case 40193
data={0 0}
case 40195
data={65535 0}
case 50000
data={0 0}
;current power in W 40097 sml[1]
;total Wh exp 40129 sml[3]
;total Wh Imp 40137 sml[2]
ends
>t1
; check incomming data
;ctime=millis
xcnt =wsa()
;print %xcnt%
if xcnt>=12 {
; check if sml reading are valid (>100kWh), else discard request
if (vsml2>100000 and vsml3>100000 and xcnt<=120) {
; for cnt 1 xcnt 12
res=wsra(req)
reg=(req[9]<<8)|req[10]
nval=req[12]
; print register %reg%, count %nval%
; transaction id
resp[1]=req[1]
resp[2]=req[2]
; protocol id
resp[3]=0
resp[4]=0
; length
resp[5]=0
resp[6]=req[12]*2+3
;device address
resp[7]=req[7]
; function code
resp[8]=req[8]
; payload len
resp[9]=req[12]*2
; write response header
wswa(resp 9)
; write response data
=#dosel
wswa(data nval dform)
; next
} else {
ecnt=ecnt+1
print clear TCP buffer %xcnt% ecnt %ecnt%
wsf()
}
; print time %(millis-ctime)%
}
>F
vsml1=sml[1]
vsml2=sml[2]*1000
vsml3=sml[3]*1000
>T
usage=sml[2]*1000
ret=sml[3]*1000
cons=sml[1]
prod=0
if cons<0 {
prod=abs(cons)
cons=0
}
Ap=MOD#A_power
Ae=MOD#A_energy*1000
Bp=MOD#B_power
Be=MOD#B_energy*1000
Cp=MOD#C_power
Ce=MOD#C_energy*1000
Dp=MOD#D_power
De=MOD#D_energy*1000
Ep=MOD#E_power
Ee=MOD#E_energy*1000
Fp=MOD#F_power
Fe=MOD#F_energy*1000
Gp=MOD#G_power
Ge=MOD#G_energy*1000
Hp=MOD#H_power
He=MOD#H_energy*1000
>S
;re-enable publishing at MQTT teleperiod if data is valid
if (usage>1000 and ret>1000) {
smlj|=1
}
; update Domoticz MQTT
if upsecs%tper==0 {
if (usage>1000 and ret>1000) {
=>publish domoticz/in {"idx":2162,"svalue":"%usage%;0;%ret%;0;%cons%;%prod%"}
}
if Ae>1000 {
=>publish domoticz/in {"idx":770,"svalue":"%Ap%;%Ae%"}
}
if Be>1000 {
=>publish domoticz/in {"idx":771,"svalue":"%Bp%;%Be%"}
}
if Ce>1000 {
=>publish domoticz/in {"idx":772,"svalue":"%Cp%;%Ce%"}
}
if De>1000 {
=>publish domoticz/in {"idx":2156,"svalue":"%Dp%;%De%"}
}
if Ee>1000 {
=>publish domoticz/in {"idx":725,"svalue":"%Ep%;%Ee%"}
}
if Fe>1000 {
=>publish domoticz/in {"idx":2157,"svalue":"%Fp%;%Fe%"}
}
if Ge>1000 {
=>publish domoticz/in {"idx":2158,"svalue":"%Gp%;%Ge%"}
}
if He>1000 {
=>publish domoticz/in {"idx":2153,"svalue":"%Hp%;%He%"}
}
}
; onbaord LED on if Power_curr < 0
pcurr=sml[1]
if pcurr>0 {
spin(2 0)
}
if pcurr<=0 {
spin(2 1)
}
>R
; close server
res=wsc()
>M 2
+1,16,s,0,9600,SML
1,77070100100700ff@1,Leistung,W,Power_curr,0
1,77070100010800ff@1000,Verbrauch,kWh,Total_in,3
1,77070100020800ff@1000,Netzeinspeisung,kWh,Total_out,3
1,=h ----------------------------
+2,3,M,0,9600,MOD,1,2,01030025,0103001D,02030025,0203001D,03030025,0303001D,04030025,0403001D,05030025,0503001D,06030025,0603001D,0704000C,07040048,0804000C,08040048
2,010304UUuu@i0:0.1,A Power,W,A_power,0
2,010304UUuuUUuu@i1:100,A Energy,kWh,A_energy,2
2,=h ----------------------------
2,020304UUuu@i2:0.1,B Power,W,B_power,0
2,020304UUuuUUuu@i3:100,B Energy,kWh,B_energy,2
2,=h ----------------------------
2,030304UUuu@i4:0.1,C Power,W,C_power,0
2,030304UUuuUUuu@i5:100,C Energy,kWh,C_energy,2
2,=h ----------------------------
2,040304UUuu@i6:0.1,D Power,W,D_power,0
2,040304UUuuUUuu@i7:100,D Energy,kWh,D_energy,2
2,=h ----------------------------
2,050304UUuu@i8:0.1,E Power,W,E_power,0
2,050304UUuuUUuu@i9:100,E Energy,kWh,E_energy,2
2,=h ----------------------------
2,060304UUuu@i10:0.1,F Power,W,F_power,0
2,060304UUuuUUuu@i11:100,F Energy,kWh,F_energy,2
2,=h ----------------------------
2,070404ffffffff@i12:1,G Power,W,G_power,1
2,070404ffffffff@i13:1,G Energy,kWh,G_energy,3
2,=h ----------------------------
2,080404ffffffff@i14:1,H Power,W,H_power,1
2,080404ffffffff@i15:1,H Energy,kWh,H_energy,3
#