-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdsmrTelegramActions.ino
executable file
·318 lines (309 loc) · 12.9 KB
/
dsmrTelegramActions.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
/*Add your custom functions to process meter readings here*/
void onTelegram(){
/*This function is executed whenever a meter telegram containing a meterID and meterTimestamp is received.
* Use this function if you want the most recent meter updates
*/
//printTelegramValues();
controlHA();
mqttPushTelegramValues();
}
void onTelegramCrc(){
/*This function is executed whenever a meter telegram containing a meterID, a meterTimestamp and a valid CRC is received.
* Sometimes a meter telegram containing valid measurements can be reveived, but the CRC can be off.
* Use this function if you want absolute assurance about the telegram contents.
*/
}
void printTelegramValues(){
/*Example function which loops over both the configured DSMR keys and the detected Mbus keys and, if found, prints their value.
* This function shows you how you can access these keys and their values.
*/
Serial.print("Meter ID: ");
Serial.println(meterId);
Serial.print("Meter time: ");
Serial.print(meterTimestamp);
Serial.print(" ");
struct tm meterTime;
localtime_r(&meterTimestamp , &meterTime);
char timeStringBuff[30];
strftime(timeStringBuff,sizeof(timeStringBuff),"%A, %B %d %Y %H:%M:%S", &meterTime);
Serial.println(timeStringBuff);
/*Loop over the configured DSMR keys and, if present in the received meter telegram, print them*/
for(int i = 0; i < sizeof(dsmrKeys)/sizeof(dsmrKeys[0]); i++){
if(*dsmrKeys[i].keyFound == true){
Serial.print(dsmrKeys[i].keyName);
Serial.print(": ");
if(dsmrKeys[i].keyType > 0 && dsmrKeys[i].keyType < 4){
if(fmodf(*dsmrKeys[i].keyValueFloat, 1.0) == 0) Serial.print(int(*dsmrKeys[i].keyValueFloat));
else Serial.print(round2(*dsmrKeys[i].keyValueFloat));
Serial.print(" ");
}
if(dsmrKeys[i].keyType == 2 || dsmrKeys[i].keyType == 3){
if(dsmrKeys[i].deviceType == "energy") Serial.print("kWh ");
else if(dsmrKeys[i].deviceType == "power") Serial.print("kW ");
else if(dsmrKeys[i].deviceType == "power") Serial.print("kW ");
else if(dsmrKeys[i].deviceType == "voltage") Serial.print("V ");
else if(dsmrKeys[i].deviceType == "current") Serial.print("A ");
else Serial.print(" ");
}
if(dsmrKeys[i].keyType == 3){
Serial.print("(");
Serial.print(*dsmrKeys[i].keyValueLong);
Serial.print(")");
}
if(dsmrKeys[i].keyType == 4){
Serial.print(*dsmrKeys[i].keyValueString);
}
if(dsmrKeys[i].keyType == 5){
Serial.print(*dsmrKeys[i].keyValueLong);
}
Serial.println("");
}
}
/*Loop over the Mbus keys found in the meter telegram and print their value*/
for(int i = 0; i < sizeof(mbusMeter)/sizeof(mbusMeter[0]); i++){
if(mbusMeter[i].keyFound == true){
if(mbusMeter[i].type == 3) Serial.print("Natural gas consumption: ");
else if (mbusMeter[i].type == 4) Serial.print("Heat consumption: ");
else if (mbusMeter[i].type == 7) Serial.print("Water consumption: ");
if(fmodf(mbusMeter[i].keyValueFloat, 1.0) == 0) Serial.print(int(mbusMeter[i].keyValueFloat));
else Serial.print(round2(mbusMeter[i].keyValueFloat));
if(mbusMeter[i].type == 3 || mbusMeter[i].type == 7) Serial.print(" m³ (");
else if (mbusMeter[i].type == 7) Serial.print(" kWh (");
Serial.print(mbusMeter[i].keyTimeStamp);
Serial.println(")");
}
}
}
void mqttPushTelegramValues(){
if(_mqtt_en){
if(sinceLastUpload > _upload_throttle*1000){
Serial.println(ESP.getFreeHeap()/1000.0);
if(mqttDebug) Serial.println("Performing MQTT push");
String availabilityTopic = _mqtt_prefix.substring(0, _mqtt_prefix.length()-1);
pubMqtt(availabilityTopic, "online", false);
/*Loop over the configured DSMR keys and, if found in the meter telegram and enabled, push their value over MQTT*/
for(int i = 0; i < sizeof(dsmrKeys)/sizeof(dsmrKeys[0]); i++){
if(*dsmrKeys[i].keyFound == true){
/*Check if the key,value pair needs to be pushed
* The unsigned long _key_pushlist contains 32 bits indicating if the value needs to be pushed (1) or not (0)
* E.g. if _key_pushlist is 329, its binary representation is 00000000000000000000000101001001
* The LSB (at the rightmost side) represents dsmrKeys[0], the bit to the left of it dsmrKeys[1] etc.
* We boolean AND the _key_pushlist with a binary mask 1, which is shifted to the left according to which key in dsmrKeys[] we are comparing
* If the result is 1, this means the key,value pair must be pushed
* E.g. if _key_pushlist is 329, dsmrKeys[0] must be pushed, dsmrKeys[1] not, dsmrKeys[2] not, dsmrKeys[3] must, etc.
* Sounds complicated, but this way we only need one variable in NVS to store up to 32 options.
*/
unsigned long mask = 1;
mask <<= i;
unsigned long test = _key_pushlist & mask;
if(test > 0){
if(mqttDebug){
Serial.print(dsmrKeys[i].keyName);
Serial.print(" ");
Serial.println(dsmrKeyPayload(i));
}
pushDSMRKey(dsmrKeys[i].keyName, dsmrKeyPayload(i), "");
}
}
}
/*Loop over the Mbus keys found in the meter telegram and push their value over MQTT*/
for(int i = 0; i < sizeof(mbusMeter)/sizeof(mbusMeter[0]); i++){
String friendlyName;
if(mbusMeter[i].keyFound == true && mbusMeter[i].enabled == true){
String tempJson = mbusKeyPayload(i);
for(int j = 0; j < sizeof(mbusKeys)/sizeof(mbusKeys[0]); j++){
if(mbusKeys[j].keyType == mbusMeter[i].type){
friendlyName = mbusKeys[j].keyName;
}
}
if(mqttDebug){
Serial.print(friendlyName);
Serial.print(" ");
Serial.println(tempJson);
}
pushDSMRKey(friendlyName, tempJson, "");
}
}
sinceLastUpload = 0;
}
}
}
String httpTelegramValues(String option){
String jsonOutput = "[";
/*Get the most important measurements*/
if(option == "basic"){
int keys[] = {0, 1, 2, 10, 11};
for(int i = 0; i < 5; i++){
String tempJson = dsmrKeyPayload(keys[i]);
tempJson += ",";
jsonOutput += tempJson;
}
for(int i = 0; i < sizeof(mbusMeter)/sizeof(mbusMeter[0]); i++){
if(mbusMeter[i].keyFound == true){
String tempJson = mbusKeyPayload(i);
tempJson += ",";
jsonOutput += tempJson;
}
}
}
/*Otherwise, return all enabled keys found in the telegram*/
else{
for(int i = 0; i < sizeof(dsmrKeys)/sizeof(dsmrKeys[0]); i++){
if(option == "all"){
String tempJson = dsmrKeyPayload(i);
tempJson += ",";
jsonOutput += tempJson;
}
else{
if(*dsmrKeys[i].keyFound == true){
unsigned long mask = 1;
mask <<= i;
unsigned long test = _key_pushlist & mask;
if(test > 0){
String tempJson = dsmrKeyPayload(i);
tempJson += ",";
jsonOutput += tempJson;
}
}
}
}
for(int i = 0; i < sizeof(mbusMeter)/sizeof(mbusMeter[0]); i++){
if(mbusMeter[i].keyFound == true){
String tempJson = mbusKeyPayload(i);
tempJson += ",";
jsonOutput += tempJson;
}
}
}
jsonOutput = jsonOutput.substring(0, jsonOutput.length()-1);
jsonOutput += "]";
return jsonOutput;
}
bool pushDSMRKey(String friendlyName, String payload, String mqttTopic){
String tempTopic = _mqtt_prefix;
String jsonOutput;
bool pushSuccess = false;
if(mqttTopic == ""){ //Use key_name as mqtt topic if keyTopic left empty
tempTopic += friendlyName;
tempTopic.replace(" ", "_");
tempTopic.toLowerCase();
}
else{
tempTopic += mqttTopic;
tempTopic.replace(" ", "_");
tempTopic.toLowerCase();
}
pushSuccess = pubMqtt(tempTopic, payload, false);
if(mqttDebug && pushSuccess){
Serial.print(tempTopic);
Serial.print(" ");
Serial.println(payload);
Serial.println("");
}
else{
if(mqttDebug) Serial.println("Could not make MQTT push");
}
return pushSuccess;
}
String formatPayload(String key, float measurementValue, String measurementUnit, time_t measurementTimestamp, String deviceType, String friendlyName, String mqttTopic, String rawKey){
String jsonOutput;
if(_payload_format > 0){ //minimal JSON payload format
DynamicJsonDocument doc(1024);
if(measurementValue == 0 && rawKey != ""){
doc["value"] = rawKey;
}
else{
if(fmodf(measurementValue, 1.0) == 0) doc["value"] = int(measurementValue);
else doc["value"] = round2(measurementValue);
}
if(measurementUnit != "") doc["unit"] = measurementUnit;
doc["timestamp"] = measurementTimestamp;
if(_payload_format > 1){ //standard JSON payload format
//friendlyName.toLowerCase();
String sensorId = _ha_device + "." + friendlyName;
friendlyName = friendlyName;
doc["friendly_name"] = friendlyName;
sensorId.toLowerCase();
sensorId.replace(" ", "_");
doc["sensorId"] = sensorId;
if(_payload_format > 2){ //COFY payload format
doc["entity"] = _ha_device;
if(friendlyName == "Total energy consumed"){
doc["metric"] = "GridElectricityImport";
doc["metricKind"] = "cumulative";
}
else if(friendlyName == "Total energy injected"){
doc["metric"] = "GridElectricityExport";
doc["metricKind"] = "cumulative";
}
else if(friendlyName == "Total active power"){
doc["metric"] = "GridElectricityPower";
doc["metricKind"] = "gauge";
}
else{
for(int k = 0; k < sizeof(cofyKeys)/sizeof(cofyKeys[0]); k++){
if(key == cofyKeys[k][0]){
if(cofyKeys[k][1] != "") doc["metric"] = cofyKeys[k][1];
doc["metricKind"] = cofyKeys[k][2];
}
}
}
}
}
serializeJson(doc, jsonOutput);
}
else{
if(fmodf(measurementValue, 1.0) == 0) jsonOutput = String(int(measurementValue));
else jsonOutput = String(round2(measurementValue));
}
return jsonOutput;
}
String dsmrKeyPayload(int i){
if(dsmrKeys[i].keyType == 0 || dsmrKeys[i].keyType == 4){
/*Other or string value (currently handled the same)*/
return formatPayload(dsmrKeys[i].dsmrKey, 0, "", meterTimestamp, dsmrKeys[i].deviceType, dsmrKeys[i].keyName, dsmrKeys[i].keyTopic, *dsmrKeys[i].keyValueString);
}
else if(dsmrKeys[i].keyType == 1 || dsmrKeys[i].keyType == 5){
/*Numeric value with no unit*/
if(dsmrKeys[i].keyType == 1) return formatPayload(dsmrKeys[i].dsmrKey, *dsmrKeys[i].keyValueFloat, "", meterTimestamp, dsmrKeys[i].deviceType, dsmrKeys[i].keyName, dsmrKeys[i].keyTopic, "");
if(dsmrKeys[i].keyType == 5) return formatPayload(dsmrKeys[i].dsmrKey, *dsmrKeys[i].keyValueLong, "", meterTimestamp, dsmrKeys[i].deviceType, dsmrKeys[i].keyName, dsmrKeys[i].keyTopic, "");
}
else if(dsmrKeys[i].keyType == 2){
/*Numeric value with unit*/
String unit;
if(dsmrKeys[i].deviceType == "energy") unit = "kWh";
else if(dsmrKeys[i].deviceType == "power") unit = "kW";
else if(dsmrKeys[i].deviceType == "voltage") unit = "V";
else if(dsmrKeys[i].deviceType == "current") unit = "A";
return formatPayload(dsmrKeys[i].dsmrKey, *dsmrKeys[i].keyValueFloat, unit, meterTimestamp, dsmrKeys[i].deviceType, dsmrKeys[i].keyName, dsmrKeys[i].keyTopic, "");
}
else if(dsmrKeys[i].keyType == 3){
/*timestamped (Mbus) message*/
String unit;
if(dsmrKeys[i].deviceType == "energy") unit = "kWh";
else if(dsmrKeys[i].deviceType == "power") unit = "kW";
else if(dsmrKeys[i].deviceType == "voltage") unit = "V";
else if(dsmrKeys[i].deviceType == "current") unit = "A";
return formatPayload(dsmrKeys[i].dsmrKey, *dsmrKeys[i].keyValueFloat, unit, *dsmrKeys[i].keyValueLong, dsmrKeys[i].deviceType, dsmrKeys[i].keyName, dsmrKeys[i].keyTopic, "");
}
else{
if(telegramDebug) Serial.print("undef");
return "";
}
}
String mbusKeyPayload(int i){
String measurementUnit;
String deviceType;
String friendlyName;
String mqttTopic;
for(int z = 0; z < sizeof(mbusKeys)/sizeof(mbusKeys[0]); z++){
if(mbusKeys[z].keyType == mbusMeter[i].type){
friendlyName = mbusKeys[z].keyName;
deviceType = mbusKeys[z].deviceType;
mqttTopic = mbusKeys[z].keyTopic;
}
}
if(mbusMeter[i].type == 3 || mbusMeter[i].type == 7) measurementUnit = "m³";
else if (mbusMeter[i].type == 7) measurementUnit = "kWh";
return formatPayload(mbusMeter[i].mbusKey, mbusMeter[i].keyValueFloat, measurementUnit, mbusMeter[i].keyTimeStamp, deviceType, friendlyName, mqttTopic, "");
}