-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSMRtelegram.ino
executable file
·209 lines (202 loc) · 7.48 KB
/
DSMRtelegram.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
void splitTelegram(String rawTelegram){
int eof = rawTelegram.lastIndexOf('\n');
//Serial.println(rawTelegram);
int delimStart = 0;
int delimEnd = 0;
while(delimEnd < eof){
delimEnd = rawTelegram.indexOf('\n', delimStart); //Get the location of the newline char at the end of the line
String s = rawTelegram.substring(delimStart, delimEnd); //Extract one line from the telegram
delimStart = delimEnd+1; //Set the start of the next line (for the next iteration)
/*Every line contains a key(value) pair, e.g. 1-0:2.7.0(01.100*kW)*/
byte valueStart = s.indexOf('('); //The key is the part before the first bracket, e.g. 1-0:2.7.0
String key = s.substring(0, valueStart);
byte valueEnd = s.lastIndexOf(')'); //The value is the part between the brackets, e.g. 01.100*kW
String value = s.substring(valueStart+1, valueEnd);
/*We now have every key(value) pair, so lets do something with them*/
float splitValue;
String splitUnit, splitString;
struct tm splitTime;
time_t splitTimestamp;
//Serial.println(key);
//Serial.println(value);
/*Consumed energy tariff 1*/
if(key == "1-0:1.8.1"){
splitWithUnit(value, splitValue, splitUnit);
meterData[0] = splitValue;
}
/*Consumed energy tariff 2*/
if(key == "1-0:1.8.2"){
splitWithUnit(value, splitValue, splitUnit);
meterData[1] = splitValue;
}
/*Injected energy tariff 1*/
if(key == "1-0:2.8.1"){
splitWithUnit(value, splitValue, splitUnit);
meterData[2] = splitValue;
}
/*Injected energy tariff 2*/
if(key == "1-0:2.8.2"){
splitWithUnit(value, splitValue, splitUnit);
meterData[3] = splitValue;
}
/*Total actual power consumption*/
if(key == "1-0:1.7.0"){
splitWithUnit(value, splitValue, splitUnit);
meterData[4] = splitValue;
}
/*Total actual power injection*/
if(key == "1-0:2.7.0"){
splitWithUnit(value, splitValue, splitUnit);
meterData[5] = splitValue;
}
/*Current average demand*/
if(key == "1-0:1.4.0"){
splitWithUnit(value, splitValue, splitUnit);
meterData[6] = splitValue;
}
/*Max average demand this month*/
if(key == "1-0:1.6.0"){
splitWithTimeAndUnit(value, splitValue, splitUnit, splitTime, splitTimestamp);
meterData[7] = splitValue;
}
/*Volt 1*/
if(key == "1-0:32.7.0"){
splitWithUnit(value, splitValue, splitUnit);
meterData[8] = splitValue;
}
/*Current 1*/
if(key == "1-0:31.7.0"){
splitWithUnit(value, splitValue, splitUnit);
meterData[9] = splitValue;
}
/*Natural gas consumption*/
if(key == "0-1:24.2.3"){
//splitWithTimeAndUnit(value, splitValue, splitUnit, splitTime, splitTimestamp);
meterData[10] = splitValue;
gasFound = true ;
}
/*Water consumption*/
if(key == "0-2:24.2.1"){
//splitWithTimeAndUnit(value, splitValue, splitUnit, splitTime, splitTimestamp);
meterData[11] = splitValue;
waterFound = true ;
}
/*Aactual power consumption 1*/
if(key == "1-0:21.7.0"){
splitWithUnit(value, splitValue, splitUnit);
meterData[12] = splitValue;
}
/*Aactual power consumption 2*/
if(key == "1-0:41.7.0"){
splitWithUnit(value, splitValue, splitUnit);
meterData[13] = splitValue;
}
/*Aactual power consumption 3*/
if(key == "1-0:61.7.0"){
splitWithUnit(value, splitValue, splitUnit);
meterData[14] = splitValue;
}
/*Aactual power injection 1*/
if(key == "1-0:22.7.0"){
splitWithUnit(value, splitValue, splitUnit);
meterData[15] = splitValue;
}
/*Aactual power injection 2*/
if(key == "1-0:42.7.0"){
splitWithUnit(value, splitValue, splitUnit);
meterData[16] = splitValue;
}
/*Aactual power injection 3*/
if(key == "1-0:62.7.0"){
splitWithUnit(value, splitValue, splitUnit);
meterData[17] = splitValue;
}
/*Volt 2*/
if(key == "1-0:52.7.0"){
splitWithUnit(value, splitValue, splitUnit);
meterData[18] = splitValue;
threePhase = true;
}
/*Volt 3*/
if(key == "1-0:72.7.0"){
splitWithUnit(value, splitValue, splitUnit);
meterData[19] = splitValue;
}
/*Current 2*/
if(key == "1-0:51.7.0"){
splitWithUnit(value, splitValue, splitUnit);
meterData[20] = splitValue;
}
/*Current 3*/
if(key == "1-0:71.7.0"){
splitWithUnit(value, splitValue, splitUnit);
meterData[21] = splitValue;
}
}
if(threePhase){
delayType = 1;
meterType = 3;
if(gasFound) meterType = 31;
if(waterFound) meterType = 32;
if(gasFound && waterFound) meterType = 33;
}
}
void splitWithUnit(String &value, float &splitValue, String &splitUnit){
/*Split a value containing a unit*/
int unitStart = value.indexOf('*');
splitValue = value.substring(0, unitStart).toFloat();
splitUnit = value.substring(unitStart+1);
}
void splitNoUnit(String &value, float &splitValue){
/*Split a value containing no unit*/
splitValue = value.toFloat();
}
void splitMeterTime(String &value, struct tm &splitTime, time_t &splitTimestamp){
/*Split a timestamp, returning a tm struct and a time_t object*/
splitTime.tm_year = (2000 + value.substring(0, 2).toInt()) - 1900; //Time.h years since 1900, so deduct 1900
splitTime.tm_mon = (value.substring(2, 4).toInt()) - 1; //Time.h months start from 0, so deduct 1
splitTime.tm_mday = value.substring(4, 6).toInt();
splitTime.tm_hour = value.substring(6, 8).toInt();
splitTime.tm_min = value.substring(8, 10).toInt();
splitTime.tm_sec = value.substring(10, 12).toInt();
splitTimestamp = mktime(&splitTime);
/*Metertime is in local time. DST shows the difference in hours to UTC*/
if(value.substring(12) == "S"){
splitTime.tm_isdst = 1;
splitTimestamp = splitTimestamp - (2*3600);
}
else if(value.substring(12) == "W"){
splitTime.tm_isdst = 0;
splitTimestamp = splitTimestamp - (1*3600);
}
else splitTime.tm_isdst = -1;
}
void splitWithTimeAndUnit(String &value, float &splitValue, String &splitUnit, struct tm &splitTime, time_t &splitTimestamp){
/*Split a timestamped value containing a unit (e.g. Mbus value)*/
String buf = value;
int timeEnd = value.indexOf(')');
int valueStart = value.indexOf('(')+1;
value = buf.substring(0, timeEnd);
splitMeterTime(value, splitTime, splitTimestamp);
value = buf.substring(valueStart);
splitWithUnit(value, splitValue, splitUnit);
}
unsigned long parseTimestampToEpoch(String timestamp) {
int yr = timestamp.substring(0, 2).toInt() + 2000; // Adjust for YY to YYYY
int month = timestamp.substring(2, 4).toInt();
int day = timestamp.substring(4, 6).toInt();
int hr = timestamp.substring(6, 8).toInt();
int minute = timestamp.substring(8, 10).toInt();
int sec = timestamp.substring(10, 12).toInt();
char season = timestamp.charAt(12);
struct tm tm;
tm.tm_year = yr - 1900; // Set date
tm.tm_mon = month - 1;
tm.tm_mday = day;
tm.tm_hour = hr; // Set time
tm.tm_min = minute;
tm.tm_sec = sec;
tm.tm_isdst = -1; // Let mktime() determine whether DST is in effect
time_t t = mktime(&tm); // Convert to time_t (Unix time)
return (unsigned long)t;
}