forked from letscontrolit/ESPEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_P071_Kamstrup401.ino
294 lines (246 loc) · 7.53 KB
/
_P071_Kamstrup401.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
#include "_Plugin_Helper.h"
#ifdef USES_P071
//#######################################################################################################
//############################# Plugin 071: Kamstrup Multical 401 #######################################
//#######################################################################################################
//IR RX/TX sensor based on http://wiki.hal9k.dk/projects/kamstrup
//schematic http://wiki.hal9k.dk/_media/projects/kamstrup/schematic.pdf
//software based on http://elektronikforumet.com/forum/viewtopic.php?f=2&t=79853
//Device pin 1 = RX
//Device pin 2 = TX
#include <ESPeasySerial.h>
#define PLUGIN_071
#define PLUGIN_ID_071 71
#define PLUGIN_NAME_071 "Communication - Kamstrup Multical 401 [TESTING]"
#define PLUGIN_VALUENAME1_071 "Heat"
#define PLUGIN_VALUENAME2_071 "Volume"
boolean Plugin_071_init = false;
byte PIN_KAMSER_RX = 0;
byte PIN_KAMSER_TX = 0;
boolean Plugin_071(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_071;
Device[deviceCount].Type = DEVICE_TYPE_SERIAL;
Device[deviceCount].VType = Sensor_VType::SENSOR_TYPE_DUAL;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 2;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_071);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_071));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_071));
break;
}
case PLUGIN_GET_DEVICEGPIONAMES:
{
serialHelper_getGpioNames(event);
break;
}
case PLUGIN_WEBFORM_SHOW_CONFIG:
{
string += serialHelper_getSerialTypeLabel(event);
success = true;
break;
}
case PLUGIN_INIT:
{
Plugin_071_init = true;
success = true;
break;
}
case PLUGIN_WEBFORM_LOAD:
{
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE: {
success = true;
break;
}
case PLUGIN_READ:
{
PIN_KAMSER_RX = CONFIG_PIN1;
PIN_KAMSER_TX = CONFIG_PIN2;
const ESPEasySerialPort port = static_cast<ESPEasySerialPort>(CONFIG_PORT);
ESPeasySerial kamSer(port, PIN_KAMSER_RX, PIN_KAMSER_TX, false); // Initialize serial
pinMode(PIN_KAMSER_RX,INPUT);
pinMode(PIN_KAMSER_TX,OUTPUT);
//read Kamstrup
byte sendmsg1[] = { 175,163,177 }; // /#1 with even parity
byte r = 0;
byte to = 0;
byte i;
char message[255];
int parityerrors;
kamSer.begin(300);
for (int x = 0; x < 3; x++) {
kamSer.write(sendmsg1[x]);
}
kamSer.flush();
//kamSer.end();
kamSer.begin(1200);
to = 0;
r = 0;
i = 0;
parityerrors = 0;
char *tmpstr;
double m_energy, m_volume;
float m_tempin, m_tempout, m_tempdiff, m_power;
long m_hours, m_flow;
while(r != 0x0A)
{
if (kamSer.available())
{
// receive byte
r = kamSer.read();
//serialPrintln(r);
if (parity_check(r))
{
parityerrors += 1;
}
r = r & 127; // Mask MSB to remove parity
message[i++] = char(r);
}
else
{
to++;
delay(25);
}
if (i>=79)
{
if ( parityerrors == 0 )
{
// serialPrint("OK: " );
// serialPrintln(message);
message[i] = 0;
tmpstr = strtok(message, " ");
if (tmpstr){
m_energy = atol(tmpstr)/3.6*1000;
}
else
m_energy = 0;
tmpstr = strtok(NULL, " ");
if (tmpstr)
m_volume = atol(tmpstr);
else
m_volume = 0;
tmpstr = strtok(NULL, " ");
if (tmpstr)
m_hours = atol(tmpstr);
else
m_hours = 0;
tmpstr = strtok(NULL, " ");
if (tmpstr)
m_tempin = atol(tmpstr)/100.0f;
else
m_tempin = 0;
tmpstr = strtok(NULL, " ");
if (tmpstr)
m_tempout = atol(tmpstr)/100.0f;
else
m_tempout = 0;
tmpstr = strtok(NULL, " ");
if (tmpstr)
m_tempdiff = atol(tmpstr)/100.0f;
else
m_tempdiff = 0;
tmpstr = strtok(NULL, " ");
if (tmpstr)
m_power = atol(tmpstr)/10.0f;
else
m_power = 0;
tmpstr = strtok(NULL, " ");
if (tmpstr)
m_flow = atol(tmpstr);
else
m_flow = 0;
String log = F("Kamstrup output: ");
log += m_energy;
log += F(" MJ; ");
log += m_volume;
log += F(" L; ");
log += m_hours;
log += F(" h; ");
log += m_tempin;
log += F(" C; ");
log += m_tempout;
log += F(" C; ");
log += m_tempdiff;
log += F(" C; ");
log += m_power;
log += ' ';
log += m_flow;
log += F(" L/H");
// addLog(LOG_LEVEL_INFO, log);
UserVar[event->BaseVarIndex] = m_energy; //gives energy in Wh
UserVar[event->BaseVarIndex+1] = m_volume; //gives volume in liters
log = F("Kamstrup : Heat value: ");
log += m_energy/1000;
log += F(" kWh");
addLog(LOG_LEVEL_INFO, log);
log = F("Kamstrup : Volume value: ");
log += m_volume;
log += F(" Liter");
addLog(LOG_LEVEL_INFO, log);
}
else
{
message[i] = 0;
String log = F("ERR(PARITY):" );
serialPrint("par");
log += message;
addLog(LOG_LEVEL_INFO, log);
//UserVar[event->BaseVarIndex] = NAN;
//UserVar[event->BaseVarIndex + 1] = NAN;
}
break;
}
if (to>100)
{
message[i] = 0;
String log = F("ERR(TIMEOUT):" );
log += message;
addLog(LOG_LEVEL_INFO, log);
//UserVar[event->BaseVarIndex] = NAN;
//UserVar[event->BaseVarIndex + 1] = NAN;
break;
}
}
//end read Kamstrup
success = true;
break;
}
}
return success;
}
bool parity_check(unsigned input) {
bool inputparity = input & 128;
int x = input & 127;
int parity = 0;
while(x != 0) {
parity ^= x;
x >>= 1;
}
if ( (parity & 0x1) != inputparity )
return(1);
else
return(0);
}
#endif // USES_P071