-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwhr930.cpp
301 lines (269 loc) · 8.83 KB
/
whr930.cpp
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
#include "espMQTT.h"
#include "whr930.h"
#include <iostream>
#include <algorithm>
#define ZEHNDER_WHR930_COMMINTERVAL 5000
ZEHNDER_WHR930 zehnder_whr930;
using namespace std;
uint16_t whr930_uptime = 0;
uint16_t whr930_lastresponse = 0;
void ZEHNDER_WHR930::requestData(bool startSequence)
{
if (startSequence)
{
sendCounter = 1;
sendCleared = true;
}
while (sendCleared && (sendCounter > 0))
{
sendCleared = false;
switch (sendCounter)
{
case 1:
if (Debug.isActive(Debug.DEBUG)) Debug.printf(cF("Requesting data from WHR-930\n"));
putdatamap(cF("status"), F("querying"));
if (fanlevel.send)
{
uint8_t sendData[1] = {fanlevel.level};
if (fanlevel.level < 5) sendPacket(0x0099, sendData, 1);
fanlevel.send = false;
}
else sendCleared = true;
break;
case 2:
if (comfort.send)
{
uint8_t sendData[1] = {comfort.calculated_temperature};
sendPacket(0x00D3, sendData, 1);
comfort.send = false;
}
else sendCleared = true;
break;
case 3:
sendPacket(0x00CD);
break;
case 4:
sendPacket(0x000B);
break;
case 5:
sendPacket(0x00D1);
break;
case 6:
sendPacket(0x00D9);
break;
case 7:
sendPacket(0x00DF);
break;
case 8:
sendPacket(0x00E1);
break;
default:
sendCounter = 0;
sendCleared = true;
break;
}
if (sendCounter) sendCounter++;
}
}
void ZEHNDER_WHR930::sendPacket(uint16_t command, uint8_t *data, uint8_t length)
{
/*
Create a packet.
Data length and checksum are automatically calculated and added to the packet.
Start and end bits are added as well.
A packet is build up as follow:
Start : 2 bytes (0x07 0xF0)
Command : 2 bytes
Number of data bytes : 1 byte
Data bytes : 0-n bytes
Checksum : 1 byte
End : 2 bytes (0x07 0x0F)
*/
uint8_t *packet = (uint8_t *)malloc(length+8);
uint8_t packetpointer = 0;
uint8_t crc = 173;
bool found07 = false;
packet[packetpointer++] = 0x07; // default start bit
packet[packetpointer++] = 0xF0; // default start bit
crc += command >> 8;
packet[packetpointer++] = command >> 8;
crc += command & 0xFF;
packet[packetpointer++] = command & 0xFF;
packet[packetpointer++] = length;
crc += length & 0xFF;
for (uint8_t index = 0; index < length; index++)
{
packet[packetpointer++] = data[index];
if (((data[index] == 0x07) && !found07) || (data[index] != 0x07))
{
crc += data[index];
}
if (data[index] == 0x07) found07 = true;
}
packet[packetpointer++] = crc;
packet[packetpointer++] = 0x07;
packet[packetpointer++] = 0x0F;
String debugstring = "";
for (uint8_t i = 0; i < packetpointer; i++) debugstring += " 0x" + String(packet[i], HEX);
if (Debug.isActive(Debug.DEBUG)) Debug.printf(cF("Writing to serial:%s\n"), debugstring.c_str());
Serial.write(packet, packetpointer);
free(packet);
lastTXtime = millis();
}
void ZEHNDER_WHR930::setfanlevel(uint8_t level)
{
if (level <= 4)
{
if (Debug.isActive(Debug.DEBUG)) Debug.printf(cF("setfanlevel(%d)\n"), level);
fanlevel.level = level;
fanlevel.send = true;
sendCounter = 1;
}
}
void ZEHNDER_WHR930::setcomforttemperature(float temperature)
{
if (temperature >= 15 && temperature <= 27)
{
if (Debug.isActive(Debug.DEBUG)) Debug.printf(cF("setcomforttemperature(%f)\n"), temperature);
comfort.calculated_temperature = (float)((temperature + 20) * 2);
comfort.send = true;
sendCounter = 1;
}
}
void ZEHNDER_WHR930::setup()
{
Serial.begin(9600);
Serial.swap();
}
void ZEHNDER_WHR930::loop()
{
static uint8_t data[40];
static uint8_t datalength = 0;
static bool removed0x7 = false;
if (Serial.available())
{
if (datalength == 40) datalength = 0;
data[datalength++] = Serial.read();
String debugstring = "";
for (uint8_t i = 0; i < datalength; i++) debugstring += " 0x" + String(data[i], HEX);
if (Debug.isActive(Debug.DEBUG)) Debug.printf(cF("Received from serial:%s\n"), debugstring.c_str());
if ((datalength == 2) && (data[0] == 0x07) && (data[1] == 0xF3))
{
// ACK received
datalength = 0;
if ((sendCounter == 2) || (sendCounter == 3)) sendCleared = true;
}
if (data[0] != 0x07)
{
// Remove garbage
datalength = 0;
}
if ((datalength == 2) && (data[0] != 0x07) && (data[1] != 0xF0))
{
// Remove garbage
datalength = 0;
}
if ((datalength == 4) && (data[0] == 0x07) && (data[1] == 0x0F) && (data[2] == 0x07) && (data[3] == 0xF3) )
{
// ACK
datalength = 0;
}
if (datalength > 4)
{
uint8_t messagelength = data[4] + 8;
// When a 0x07 value appears in the dataset, another 0x07 is inserted, but not added to the length or the checksum so remove it
// but only if we did removed it allready last time to prevent 0x7 0x7 0x7 0x7 t become 0x7 instead of 0x7 0x7.
if ((data[0] == 0x7) && (data[1] == 0xF0) && (data[datalength-2] == 0x7) && (data[datalength-1] == 0x7) && !removed0x7)
{
datalength--;
removed0x7 = true;
}
else removed0x7 = false;
// Check if complete packet is received
if ((data[0] == 0x7) && (data[1] == 0xF0) && (data[datalength-2] == 0x7) && (data[datalength-1] == 0xF) && (datalength == messagelength))
{
uint16_t command = (data[2] << 8) + data[3];
if (Debug.isActive(Debug.DEBUG)) Debug.printf(cF("Command received:0x%x\n"), command);
switch (command)
{
case 0x00CE:
if (datalength >= 16+3)
{
putdatamap(cF("fanlevel"), data[13]);
putdatamap(cF("exhaust/fan/current"), data[11]);
putdatamap(cF("exhaust/fan/absent"), data[5]);
putdatamap(cF("exhaust/fan/low"), data[6]);
putdatamap(cF("exhaust/fan/medium"), data[7]);
putdatamap(cF("exhaust/fan/high"), data[15]);
putdatamap(cF("intake/fan/active"), data[14]);
putdatamap(cF("intake/fan/current"), data[12]);
putdatamap(cF("intake/fan/absent"), data[8]);
putdatamap(cF("intake/fan/low"), data[9]);
putdatamap(cF("intake/fan/medium"), data[10]);
putdatamap(cF("intake/fan/high"), data[16]);
}
break;
case 0x000C:
if (datalength >= 10+3)
{
putdatamap(cF("intake/fan/rpm"), 1875000 / ((data[7] << 8) + data[8]));
putdatamap(cF("exhaust/fan/rpm"), 1875000 / ((data[9] << 8) + data[10]));
}
break;
case 0x00D2:
if (datalength >= 9+3)
{
char floatvalue[5];
snprintf(floatvalue, 5, "%.1f", float(data[5])/ 2.0 - 20);
putdatamap(cF("comfort/temperature"), floatvalue);
snprintf(floatvalue, 5, "%.1f", float(data[6])/ 2.0 - 20);
putdatamap(cF("intake/temperature"), floatvalue);
snprintf(floatvalue, 5, "%.1f", float(data[7])/ 2.0 - 20);
putdatamap(cF("supply/temperature"), floatvalue);
snprintf(floatvalue, 5, "%.1f", float(data[8])/ 2.0 - 20);
putdatamap(cF("extract/temperature"), floatvalue);
snprintf(floatvalue, 5, "%.1f", float(data[9])/ 2.0 - 20);
putdatamap(cF("exhaust/temperature"), floatvalue);
}
break;
case 0x00DA:
if (datalength >= 13+3)
{
putdatamap(cF("filterfull"), data[13]);
}
break;
case 0x00E0:
if (datalength >= 11+3)
{
putdatamap(cF("bypass/factor"), data[7]);
putdatamap(cF("bypass/level"), data[8]);
putdatamap(cF("bypass/correction"), data[9]);
putdatamap(cF("bypass/summermode"), data[11]);
}
break;
case 0x00E2:
if (datalength >= 10+3)
{
putdatamap(cF("bypass/status"), data[5]);
putdatamap(cF("frostprotection/heater"), data[7]);
putdatamap(cF("frostprotection/status"), data[6]);
putdatamap(cF("frostprotection/minutes"), (data[8] << 8) + data[9]);
putdatamap(cF("frostprotection/level"), data[10]);
putdatamap(cF("status"), F("ready"));
}
break;
}
sendCleared = true;
datalength = 0;
}
}
}
else
{
}
this->requestData(false);
if(millis() - lastTXtime > ZEHNDER_WHR930_COMMINTERVAL)
{
this->requestData(true);
}
}