-
Notifications
You must be signed in to change notification settings - Fork 144
/
NibeHeatpumpIR.cpp
331 lines (279 loc) · 10.3 KB
/
NibeHeatpumpIR.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
Info about NIBE IR remote code:
Control Message:
Consists of 90 Bits
Message is split into
| 10x8 Data Bits | 2 Static Bit (01) | CRC 8 bit |
Data Bits are split up the following way:
| Never Change | Never Change | Op Mode and Temp | Temp and Fan Mode | Vertical Direction and Timer | Timer | Timer | Timer and Time | Time and Special Functions | Special Functions |
iFeel Message:
Consists of 32 Bits
| 3x8 Data Bits | CRC 8 bit|
Last 5 Bits of the Data Bits contain the sensed temperature
from 4-35 deg C. Temperature is transmitted with an offset of 4 deg.
Data are being send every 5 min or when the sensed temperature value changes.
*/
#include <stdint.h>
#include "NibeHeatpumpIR.h"
#ifdef NIBE_USE_TIME_H
#include <time.h>
#endif
#ifdef NIBE_IR_SEND_TIME
int nibeSendHour = 12;
int nibeSendMinute = 42;
#endif
// Reverses bit order for a uint8_t. Can modify the bitlength that needs to be reversed (needed 8, 5 bit or 2 bit reverse)
static uint8_t reverseBits8(uint8_t value, int bitLength) {
uint8_t reversedValue = 0;
for (int i = 0; i < bitLength; i++) {
// Extract the i-th bit from the original value
uint8_t bit = (value >> i) & 1;
// Set the (bitLength - 1 - i)-th bit in the reversed value
reversedValue |= bit << (bitLength - 1 - i);
}
return reversedValue;
}
// Reverses bit order for a uint16_t. Can modify the bitlength that needs to be reversed (needed 11 bit reverse)
static uint16_t reverseBits16(uint16_t value, int bitLength) {
uint16_t reversedValue = 0;
for (int i = 0; i < bitLength; i++) {
// Extract the i-th bit from the original value
uint16_t bit = (value >> i) & 1;
// Set the (bitLength - 1 - i)-th bit in the reversed value
reversedValue |= bit << (bitLength - 1 - i);
}
return reversedValue;
}
NibeHeatpumpIR::NibeHeatpumpIR() : HeatpumpIR()
{
static const char model[] PROGMEM = "nibe";
static const char info[] PROGMEM = "{\"mdl\":\"nibe\",\"dn\":\"Nibe\",\"mT\":10,\"xT\":32,\"fs\":5}";
_model = model;
_info = info;
}
void NibeHeatpumpIR::send(IRSender& IR, uint8_t powerModeCmd, uint8_t operatingModeCmd, uint8_t fanSpeedCmd, uint8_t temperatureCmd, uint8_t swingVCmd, uint8_t swingHCmd)
{
send(IR, powerModeCmd, operatingModeCmd, fanSpeedCmd, temperatureCmd, swingVCmd, swingHCmd, false, true);
}
void NibeHeatpumpIR::send(IRSender& IR, uint8_t powerModeCmd, uint8_t operatingModeCmd, uint8_t fanSpeedCmd, uint8_t temperatureCmd, uint8_t swingVCmd, uint8_t swingHCmd, bool turboModeCmd, bool iFeelModeCmd)
{
(void)swingHCmd;
// Sensible defaults for the heat pump mode
uint8_t powerMode = NIBE_POWER_ON;
uint8_t operatingMode = NIBE_MODE_HEAT_ONDEMAND; // Default run unit until temp is reached, fan stops
uint8_t fanSpeed = NIBE_MODE_FAN_AUTO;
uint8_t temperature = 21;
uint8_t swingV = NIBE_VDIR_AUTO; // Set auto mode since that one is allowed for all modes
uint8_t iFeelMode = 0x00;
uint8_t filter = 0x00;
uint8_t nightMode = 0x00;
uint8_t turboMode = 0x00;
bool filterCmd = true; // Default enable air filter
if (powerModeCmd == POWER_OFF)
{
powerMode = NIBE_POWER_OFF;
}
else
{
powerMode = NIBE_POWER_ON;
switch (operatingModeCmd)
{
case MODE_AUTO:
operatingMode = NIBE_MODE_AUTO_HEAT;
//operatingMode = NIBE_MODE_AUTO_COOL; // This heatpump can be set from heating or cooling into auto mode. Effect is the same.
break;
case MODE_HEAT:
operatingMode = NIBE_MODE_HEAT_ONDEMAND; // Heats until temp is reached, turns off fan when target is reached
//operatingMode = NIBE_MODE_HEAT_CONTINOUS; // Heats until temp is reached, but keeps the fan running
break;
case MODE_COOL:
operatingMode = NIBE_MODE_COOL;
break;
case MODE_DRY:
operatingMode = NIBE_MODE_DRY;
break;
case MODE_FAN:
operatingMode = NIBE_MODE_FAN;
break;
}
}
// NOTE Fan speed Auto can not be used in NIBE_MODE_FAN
switch (fanSpeedCmd)
{
case FAN_AUTO:
if (operatingMode == NIBE_MODE_FAN)
fanSpeed = NIBE_MODE_FAN_HIGH;
else
fanSpeed = NIBE_MODE_FAN_AUTO;
break;
case FAN_4: // FAN_4 = CLIMATE_FAN_HIGH - ESPHOME
fanSpeed = NIBE_MODE_FAN_HIGH;
break;
case FAN_3: // FAN_3 = CLIMATE_FAN_MEDIUM - ESPHOME
fanSpeed = NIBE_MODE_FAN_MED;
break;
case FAN_2: // FAN_2 = CLIMATE_FAN_LOW - ESPHOME
fanSpeed = NIBE_MODE_FAN_LOW;
break;
case FAN_SILENT:
nightMode = 0x01;
break;
}
// NOTE Operating Mode Auto allows all positions
// NOTE Operating Mode Cooling and Dry allows only pos 1-4
// NOTE Operating Mode Heating allows only pos 3-6
// NOTE Position 6 is not implemented!
switch (swingVCmd)
{
case VDIR_AUTO:
swingV = NIBE_VDIR_AUTO;
break;
case VDIR_SWING:
swingV = NIBE_VDIR_ALL;
break;
case VDIR_UP:
if ((operatingMode == NIBE_MODE_HEAT_CONTINOUS) || (operatingMode == NIBE_MODE_HEAT_ONDEMAND))
swingV = NIBE_VDIR_POS3;
else
swingV = NIBE_VDIR_POS1;
break;
case VDIR_MUP:
if ((operatingMode == NIBE_MODE_HEAT_CONTINOUS) || (operatingMode == NIBE_MODE_HEAT_ONDEMAND))
swingV = NIBE_VDIR_POS3;
else
swingV = NIBE_VDIR_POS2;
break;
case VDIR_MIDDLE:
swingV = NIBE_VDIR_POS3;
break;
case VDIR_MDOWN:
swingV = NIBE_VDIR_POS4;
break;
case VDIR_DOWN:
if ((operatingMode == NIBE_MODE_COOL) || (operatingMode == NIBE_MODE_DRY))
swingV = NIBE_VDIR_POS4;
else
swingV = NIBE_VDIR_POS5;
break;
}
if (iFeelModeCmd)
{
iFeelMode = 0x01;
}
if (filterCmd)
{
filter = 0x01;
}
if (turboModeCmd)
{
turboMode = 0x01;
}
// Allowed temp range 10-32 deg
if ((temperatureCmd > 9) && (temperatureCmd < 33))
{
temperature = temperatureCmd - 4; // Temp is reported as: Actual Temp - 4
}
sendNibe(IR, powerMode, operatingMode, fanSpeed, temperature, swingV, iFeelMode, filter, turboMode, nightMode);
}
// Send the Gree code
void NibeHeatpumpIR::sendNibe(IRSender& IR, uint8_t powerMode, uint8_t operatingMode, uint8_t fanSpeed, uint8_t temperature, uint8_t swingV, uint8_t iFeelMode, uint8_t filter, uint8_t turboMode, uint8_t nightMode)
{
// Setting some default values that never change!
uint8_t NibeTemplate[] = { 0x35, 0xAF, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 };
// 0 1 2 3 4 5 6 7 8 9 10 11
#ifdef NIBE_USE_TIME_H
time_t now;
struct tm * timeinfo;
#endif
uint16_t currentTime = 0; // If no USE_TIME_H or IR_SEND_TIME defined, send always time 00:00
// Convert Temperature
uint8_t temp_swap = reverseBits8(temperature, 5);
// Byte 2 contains Operation Mode and part of temperature
NibeTemplate[2] |= operatingMode << 2;
NibeTemplate[2] |= (temp_swap >> 3);
// Byte 3 contains part of temperature and fan mode
NibeTemplate[3] |= (temp_swap & 0x07) << 5;
NibeTemplate[3] |= fanSpeed << 3;
// NOTE Part of Byte 3 and 4 (total of 5 bits) contains some data that are not decoded. They seem to change randomly.
// Same commands have been send with the remote control, while part of these 5 bits are changing.
// Observed Codes:
// 00001, 10001, 01001, 00101, 10101, 01101
// We always use 00001 in the hope that it doesnt affect the usage of the heatpump! Maybe someone will figure it out when to change these!
// During testing no issues have been observed!
// Byte 4 - vertical air flow direction
NibeTemplate[4] |= swingV << 3;
// NOTE Timer functionality is not implemented here, when disabled timer fields always have the same value
// Part of Byte 4 and Byte 5 is for start timer function -> needs to be set to 00000000111
NibeTemplate[5] |= 0x7;
// Byte 6 and part of Byte 7 is for stop timer function -> needs to be set to 00000000111
NibeTemplate[7] |= (0x7 << 5);
// Part of Byte 7 and part of Byte 8 is for actual time (needed for timer and vacation function)
#ifdef NIBE_USE_TIME_H
time(&now);
timeinfo = localtime(&now);
currentTime = (uint16_t)(timeinfo->tm_hour * 60 + timeinfo->tm_min);
#endif
#ifdef NIBE_IR_SEND_TIME
currentTime = (uint16_t)(nibeSendHour * 60 + nibeSendMinute);
#endif
currentTime = reverseBits16(currentTime, 11);
NibeTemplate[7] |= ((uint8_t) ((currentTime & 0x7C0) >> 6));
NibeTemplate[8] |= ((uint8_t) (currentTime & 0x3F)) << 2;
// Part of Byte 8 and part of Byte 9 is for special functions
NibeTemplate[9] |= iFeelMode;
NibeTemplate[9] |= (powerMode << 2);
NibeTemplate[9] |= (filter << 3);
NibeTemplate[9] |= (turboMode << 4);
NibeTemplate[9] |= (nightMode << 5);
// Calculate CRC
uint8_t checksum = 0;
for (int i = 0; i < 11; i++)
{
if (i == 10)
checksum += reverseBits8(NibeTemplate[i], 2); // Byte 10 only has 2 bits
else
checksum += reverseBits8(NibeTemplate[i], 8);
}
NibeTemplate[11] = reverseBits8(checksum, 8);
// 38 kHz PWM frequency
IR.setFrequency(38);
// Send Header mark
IR.mark(NIBE_HDR_MARK);
IR.space(NIBE_HDR_SPACE);
for (int i=0; i<12; i++)
{
if (i == 10)
IR.sendIRbyte(reverseBits8(NibeTemplate[i],2), NIBE_BIT_MARK, NIBE_ZERO_SPACE, NIBE_ONE_SPACE, 2); // Byte 10 only has 2 bits
else
IR.sendIRbyte(reverseBits8(NibeTemplate[i],8), NIBE_BIT_MARK, NIBE_ZERO_SPACE, NIBE_ONE_SPACE);
}
// End mark
IR.mark(NIBE_BIT_MARK);
IR.space(NIBE_MSG_SPACE);
}
//Should send current sensed temperatures every 5 min or on temperature change
void NibeHeatpumpIR::send(IRSender& IR, uint8_t currentTemperature)
{
// Example: 0011 0101 1010 1111 0100 0101 1100 0010
uint8_t NibeTemplate[] = { 0x35, 0xAF, 0x40, 0x00 };
uint8_t temp_swap = reverseBits8(currentTemperature - 4, 5);
NibeTemplate[2] |= temp_swap;
uint8_t checksum = 0;
for (int i = 0; i < 3; i++)
{
checksum += reverseBits8(NibeTemplate[i], 8);
}
NibeTemplate[3] = reverseBits8(checksum, 8);
// 38 kHz PWM frequency
IR.setFrequency(38);
// Send Header mark
IR.mark(NIBE_HDR_MARK);
IR.space(NIBE_HDR_SPACE);
for (int i=0; i<4; i++)
{
IR.sendIRbyte(reverseBits8(NibeTemplate[i], 8), NIBE_BIT_MARK, NIBE_ZERO_SPACE, NIBE_ONE_SPACE);
}
// End mark
IR.mark(NIBE_BIT_MARK);
IR.space(NIBE_MSG_SPACE);
}