-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandleSlimmeMeter.ino
196 lines (176 loc) · 6.74 KB
/
handleSlimmeMeter.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
/*
***************************************************************************
** Program : handleSlimmeMeter - part of DSMRloggerAPI
** Version : v2.0.1
**
** Copyright (c) 2020 Willem Aandewiel / Martijn Hendriks
**
** TERMS OF USE: MIT License. See bottom of file.
***************************************************************************
*/
#if !defined(HAS_NO_SLIMMEMETER)
//==================================================================================
void handleSlimmemeter()
{
//DebugTf("showRaw (%s)\r\n", showRaw ?"true":"false");
if (showRaw) {
//-- process telegrams in raw mode
processSlimmemeterRaw();
}
else
{
processSlimmemeter();
}
} // handleSlimmemeter()
//==================================================================================
void processSlimmemeterRaw()
{
char tlgrm[1200] = "";
DebugTf("handleSlimmerMeter RawCount=[%4d]\r\n", showRawCount);
showRawCount++;
showRaw = (showRawCount <= 20);
if (!showRaw)
{
showRawCount = 0;
return;
}
slimmeMeter.enable(true);
swSer1.setTimeout(5000); // 5 seconds must be enough ..
memset(tlgrm, 0, sizeof(tlgrm));
int l = 0;
// The terminator character is discarded from the serial buffer.
l = swSer1.readBytesUntil('/', tlgrm, sizeof(tlgrm));
// now read from '/' to '!'
// The terminator character is discarded from the serial buffer.
l = swSer1.readBytesUntil('!', tlgrm, sizeof(tlgrm));
swSer1.setTimeout(1000); // seems to be the default ..
// DebugTf("read [%d] bytes\r\n", l);
if (l == 0)
{
DebugTln(F("RawMode: Timerout - no telegram received within 5 seconds"));
return;
}
tlgrm[l++] = '!';
#if !defined( USE_PRE40_PROTOCOL )
// next 6 bytes are "<CRC>\r\n"
for (int i=0; ( i<6 && (i<(sizeof(tlgrm)-7)) ); i++)
{
tlgrm[l++] = (char)swSer1.read();
}
#else
tlgrm[l++] = '\r';
tlgrm[l++] = '\n';
#endif
tlgrm[(l +1)] = '\0';
// shift telegram 1 char to the right (make room at pos [0] for '/')
for (int i=strlen(tlgrm); i>=0; i--) { tlgrm[i+1] = tlgrm[i]; yield(); }
tlgrm[0] = '/';
//Post result to Debug
Debugf("Telegram (%d chars):\r\n/%s\r\n", strlen(tlgrm), tlgrm);
return;
} // processSlimmemeterRaw()
//==================================================================================
void processSlimmemeter()
{
slimmeMeter.loop();
if (slimmeMeter.available())
{
DebugTf("telegramCount=[%d] telegramErrors=[%d]\r\n", telegramCount, telegramErrors);
Debugln(F("\r\n[Time----][FreeHeap/mBlck][Function----(line):\r"));
// Voorbeeld: [21:00:11][ 9880/ 8960] loop ( 997): read telegram [28] => [140307210001S]
telegramCount++;
DSMRdata = {};
String DSMRerror;
if (slimmeMeter.parse(&DSMRdata, &DSMRerror)) // Parse succesful, print result
{
if (telegramCount > (UINT32_MAX - 10))
{
delay(1000);
ESP.reset();
delay(1000);
}
digitalWrite(LED_BUILTIN, LED_OFF);
if (DSMRdata.identification_present)
{
//--- this is a hack! The identification can have a backslash in it
//--- that will ruin javascript processing :-(
for(int i=0; i<DSMRdata.identification.length(); i++)
{
if (DSMRdata.identification[i] == '\\') DSMRdata.identification[i] = '=';
yield();
}
}
if (!settingSmHasFaseInfo)
{
if (DSMRdata.power_delivered_present && !DSMRdata.power_delivered_l1_present)
{
DSMRdata.power_delivered_l1 = DSMRdata.power_delivered;
DSMRdata.power_delivered_l1_present = true;
DSMRdata.power_delivered_l2_present = true;
DSMRdata.power_delivered_l3_present = true;
}
if (DSMRdata.power_returned_present && !DSMRdata.power_returned_l1_present)
{
DSMRdata.power_returned_l1 = DSMRdata.power_returned;
DSMRdata.power_returned_l1_present = true;
DSMRdata.power_returned_l2_present = true;
DSMRdata.power_returned_l3_present = true;
}
} // No Fase Info
#ifdef USE_NTP_TIME
if (!DSMRdata.timestamp_present) //USE_NTP
{ //USE_NTP
sprintf(cMsg, "%02d%02d%02d%02d%02d%02dW\0\0" //USE_NTP
, (year() - 2000), month(), day() //USE_NTP
, hour(), minute(), second()); //USE_NTP
DSMRdata.timestamp = cMsg; //USE_NTP
DSMRdata.timestamp_present = true; //USE_NTP
} //USE_NTP
#endif
processTelegram();
if (Verbose2)
{
DSMRdata.applyEach(showValues());
}
}
else // Parser error, print error
{
telegramErrors++;
#ifdef USE_SYSLOGGER
sysLog.writef("Parse error\r\n%s\r\n\r\n", DSMRerror.c_str());
#endif
DebugTf("Parse error\r\n%s\r\n\r\n", DSMRerror.c_str());
//--- set DTR to get a new telegram as soon as possible
slimmeMeter.enable(true);
slimmeMeter.loop();
}
if ( (telegramCount > 25) && (telegramCount % (2100 / (settingTelegramInterval + 1)) == 0) )
{
DebugTf("Processed [%d] telegrams ([%d] errors)\r\n", telegramCount, telegramErrors);
writeToSysLog("Processed [%d] telegrams ([%d] errors)", telegramCount, telegramErrors);
}
} // if (slimmeMeter.available())
} // handleSlimmeMeter()
#endif
/***************************************************************************
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
***************************************************************************/