-
Notifications
You must be signed in to change notification settings - Fork 1
/
Multical402Meter.ino
349 lines (290 loc) · 8.63 KB
/
Multical402Meter.ino
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <SoftwareSerial.h>
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include "arduino_secrets.h"
//WIFI
const char* ssid = SECRET_SSID;
const char* password = SECRET_WIFI_PASSWORD;
//MQTT
#define mqtt_server "192.168.88.99"
#define mqtt_user ""
#define mqtt_password ""
#define base_topic "sensor/heater/"
WiFiClient wifiClient;
PubSubClient client(wifiClient);
//Kamstrup setup
// Kamstrup Multical 402
word const kregnums[] = { 0x003C,0x0050,0x0056,0x0057,0x0059,0x004a,0x0044 }; // The registers we want to get out of the meter
char* kregstrings[] = { "energy","power","temperature_t1","temperature_t2","temperature_diff", "flow", "volume" }; // The name of the registers we want to get out of the meter in the same order as above
#define NUMREGS 7 // Number of registers above
#define KAMBAUD 1200
/*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,
}*/
// Units
char* units[65] = {"","Wh","kWh","MWh","GWh","j","kj","Mj",
"Gj","Cal","kCal","Mcal","Gcal","varh","kvarh","Mvarh","Gvarh",
"VAh","kVAh","MVAh","GVAh","kW","kW","MW","GW","kvar","kvar","Mvar",
"Gvar","VA","kVA","MVA","GVA","V","A","kV","kA","C","K","l","m3",
"l/h","m3/h","m3xC","ton","ton/h","h","hh:mm:ss","yy:mm:dd","yyyy:mm:dd",
"mm:dd","","bar","RTC","ASCII","m3 x 10","ton xr 10","GJ x 10","minutes","Bitfield",
"s","ms","days","RTC-Q","Datetime"};
// Pin definitions
#define PIN_KAMSER_RX D5 // Kamstrup IR interface RX
#define PIN_KAMSER_TX D7 // Kamstrup IR interface TX
#define PIN_LED 13 // Standard Arduino LED
// Kamstrup optical IR serial
#define KAMTIMEOUT 800 // Kamstrup timeout after transmit
SoftwareSerial kamSer(PIN_KAMSER_RX, PIN_KAMSER_TX, false); // Initialize serial
void setup () {
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
pinMode(D8, OUTPUT);
digitalWrite(D3, HIGH);
digitalWrite(D4, HIGH);
digitalWrite(D8, LOW);
Serial.begin(115200);
Serial.println("BOOT");
if (connectWIFI()) {
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
client.setServer(mqtt_server,1883);
pinMode(PIN_LED, OUTPUT);
digitalWrite(PIN_LED, 0);
// setup kamstrup serial
pinMode(PIN_KAMSER_RX,INPUT);
pinMode(PIN_KAMSER_TX,OUTPUT);
kamSer.begin(KAMBAUD);
delay(200);
for (int kreg = 0; kreg < NUMREGS; kreg++) {
kamReadReg(kreg);
delay(100);
}
Serial.println("BOOT done.");
}
boolean connectWIFI()
{
Serial.println("Connecting to WIFI");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.hostname("Heater MQTT Sensor");
int count = 0;
while (count < 20) {
if (WiFi.status() == WL_CONNECTED) {
return (true);
}
delay(500);
count++;
}
Serial.println("Wifi Connection timed out.");
return false;
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
// If you do not want to use a username and password, change next line to
// if (client.connect("ESP8266Client")) {
if (client.connect("ESP8266 Heater Sensor", mqtt_user, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop () {
if (!client.connected()) {
reconnect();
}
client.loop();
for (int kreg = 0; kreg < NUMREGS; kreg++) {
kamReadReg(kreg);
delay(100);
}
digitalWrite(PIN_LED, digitalRead(PIN_KAMSER_RX));
delay(2000);
}
float kamReadReg(unsigned short kreg) {
byte recvmsg[40]; // buffer of bytes to hold the received data
float rval; // this will hold the final value
// prepare message to send and send it
byte sendmsg[] = { 0x3f, 0x10, 0x01, (kregnums[kreg] >> 8), (kregnums[kreg] & 0xff) };
kamSend(sendmsg, 5);
// listen if we get an answer
unsigned short rxnum = kamReceive(recvmsg);
// check if number of received bytes > 0
if(rxnum != 0){
// decode the received message
rval = kamDecode(kreg,recvmsg);
// print out received value to terminal (debug)
Serial.print(kregstrings[kreg]);
Serial.print(": ");
Serial.print(rval);
Serial.print(" ");
Serial.println();
char result[100]; // array to hold the result.
strcpy(result,base_topic); // copy string one into the result.
strcat(result,kregstrings[kreg]);
//Serial.println(result);
client.publish(result,String(rval).c_str(),true);
return rval;
}
}
// kamSend - send data to Kamstrup meter
void kamSend(byte const *msg, int msgsize) {
// append checksum bytes to message
byte newmsg[msgsize+2];
for (int i = 0; i < msgsize; i++) { newmsg[i] = msg[i]; }
newmsg[msgsize++] = 0x00;
newmsg[msgsize++] = 0x00;
int c = crc_1021(newmsg, msgsize);
newmsg[msgsize-2] = (c >> 8);
newmsg[msgsize-1] = c & 0xff;
// build final transmit message - escape various bytes
byte txmsg[20] = { 0x80 }; // prefix
int txsize = 1;
for (int i = 0; i < msgsize; i++) {
if (newmsg[i] == 0x06 or newmsg[i] == 0x0d or newmsg[i] == 0x1b or newmsg[i] == 0x40 or newmsg[i] == 0x80) {
txmsg[txsize++] = 0x1b;
txmsg[txsize++] = newmsg[i] ^ 0xff;
} else {
txmsg[txsize++] = newmsg[i];
}
}
txmsg[txsize++] = 0x0d; // EOF
// send to serial interface
for (int x = 0; x < txsize; x++) {
kamSer.write(txmsg[x]);
}
}
// kamReceive - receive bytes from Kamstrup meter
unsigned short kamReceive(byte recvmsg[]) {
byte rxdata[50]; // buffer to hold received data
unsigned long rxindex = 0;
unsigned long starttime = millis();
kamSer.flush(); // flush serial buffer - might contain noise
byte r;
// loop until EOL received or timeout
while(r != 0x0d){
// handle rx timeout
if(millis()-starttime > KAMTIMEOUT) {
Serial.println("Timed out listening for data");
return 0;
}
// handle incoming data
if (kamSer.available()) {
// receive byte
r = kamSer.read();
if(r != 0x40) { // don't append if we see the start marker
// append data
rxdata[rxindex] = r;
rxindex++;
}
}
}
// remove escape markers from received data
unsigned short j = 0;
for (unsigned short i = 0; i < rxindex -1; i++) {
if (rxdata[i] == 0x1b) {
byte v = rxdata[i+1] ^ 0xff;
if (v != 0x06 and v != 0x0d and v != 0x1b and v != 0x40 and v != 0x80){
Serial.print("Missing escape ");
Serial.println(v,HEX);
}
recvmsg[j] = v;
i++; // skip
} else {
recvmsg[j] = rxdata[i];
}
j++;
}
// check CRC
if (crc_1021(recvmsg,j)) {
Serial.println("CRC error: ");
return 0;
}
return j;
}
// kamDecode - decodes received data
float kamDecode(unsigned short const kreg, byte const *msg) {
// skip if message is not valid
if (msg[0] != 0x3f or msg[1] != 0x10) {
return false;
}
if (msg[2] != (kregnums[kreg] >> 8) or msg[3] != (kregnums[kreg] & 0xff)) {
return false;
}
// decode the mantissa
long x = 0;
for (int i = 0; i < msg[5]; i++) {
x <<= 8;
x |= msg[i + 7];
}
// decode the exponent
int i = msg[6] & 0x3f;
if (msg[6] & 0x40) {
i = -i;
};
float ifl = pow(10,i);
if (msg[6] & 0x80) {
ifl = -ifl;
}
// return final value
return (float )(x * ifl);
}
// crc_1021 - calculate crc16
long crc_1021(byte const *inmsg, unsigned int len){
long creg = 0x0000;
for(unsigned int i = 0; i < len; i++) {
int mask = 0x80;
while(mask > 0) {
creg <<= 1;
if (inmsg[i] & mask){
creg |= 1;
}
mask>>=1;
if (creg & 0x10000) {
creg &= 0xffff;
creg ^= 0x1021;
}
}
}
return creg;
}