-
Notifications
You must be signed in to change notification settings - Fork 17
/
_P145_Itho.ino
450 lines (399 loc) · 13.8 KB
/
_P145_Itho.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//#######################################################################################################
//############################## Plugin 145: Itho ventilation unit 868Mhz remote ########################
//#######################################################################################################
// author :jodur, 13-1-2018
// List of commands:
// 1111 to join ESP8266 with Itho ventilation unit
// 9999 to leaveESP8266 with Itho ventilation unit
// 0 to set Itho ventilation unit to standby
// 1 - set Itho ventilation unit to low speed
// 2 - set Itho ventilation unit to medium speed
// 3 - set Itho ventilation unit to high speed
// 4 - set Itho ventilation unit to full speed
// 13 - set itho to high speed with hardware timer (10 min)
// 23 - set itho to high speed with hardware timer (20 min)
// 33 - set itho to high speed with hardware timer (30 min)
//List of States:
// 1 - Itho ventilation unit to lowest speed
// 2 - Itho ventilation unit to medium speed
// 3 - Itho ventilation unit to high speed
// 4 - Itho ventilation unit to full speed
// 13 -Itho to high speed with hardware timer (10 min)
// 23 -Itho to high speed with hardware timer (20 min)
// 33 -Itho to high speed with hardware timer (30 min)
// Usage for http (not case sensitive):
// http://ip/control?cmd=STATE,1111
// http://ip/control?cmd=STATE,1
// http://ip/control?cmd=STATE,2
// http://ip/control?cmd=STATE,3
// usage for example mosquito MQTT
// mosquitto_pub -t /Fan/cmd -m 'state 1111'
// mosquitto_pub -t /Fan/cmd -m 'state 1'
// mosquitto_pub -t /Fan/cmd -m 'state 2'
// mosquitto_pub -t /Fan/cmd -m 'state 3'
// This code needs the library made by 'supersjimmie': https://github.com/supersjimmie/IthoEcoFanRFT/tree/master/Master/Itho
// A CC1101 868Mhz transmitter is needed
// See https://gathering.tweakers.net/forum/list_messages/1690945 for more information
// code/idea was inspired by first release of code from 'Thinkpad'
#include <SPI.h>
#include "IthoCC1101.h"
#include "IthoPacket.h"
#include <Ticker.h>
//This extra settings struct is needed because the default settingsstruct doesn't support strings
struct PLUGIN_145_ExtraSettingsStruct
{ char ID1[24];
char ID2[24];
char ID3[24];
} PLUGIN_145_ExtraSettings;
IthoCC1101 PLUGIN_145_rf;
void PLUGIN_145_ITHOinterrupt() ICACHE_RAM_ATTR;
// extra for interrupt handling
bool PLUGIN_145_ITHOhasPacket = false;
Ticker PLUGIN_145_ITHOticker;
int PLUGIN_145_State=1; // after startup it is assumed that the fan is running low
int PLUGIN_145_OldState=1;
int PLUGIN_145_Timer=0;
int PLUGIN_145_LastIDindex = 0;
int PLUGIN_145_OldLastIDindex = 0;
long PLUGIN_145_LastPublish=0;
int8_t Plugin_145_IRQ_pin=-1;
bool PLUGIN_145_InitRunned = false;
#define PLUGIN_145
#define PLUGIN_ID_145 145
#define PLUGIN_NAME_145 "Itho ventilation remote"
#define PLUGIN_VALUENAME1_145 "State"
#define PLUGIN_VALUENAME2_145 "Timer"
#define PLUGIN_VALUENAME3_145 "LastIDindex"
// Timer values for hardware timer in Fan
#define PLUGIN_145_Time1 10*60
#define PLUGIN_145_Time2 20*60
#define PLUGIN_145_Time3 30*60
boolean Plugin_145(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_145;
Device[deviceCount].Type = DEVICE_TYPE_SINGLE;
Device[deviceCount].VType = SENSOR_TYPE_TRIPLE;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = false;
Device[deviceCount].ValueCount = 3;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_145);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_145));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_145));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[2], PSTR(PLUGIN_VALUENAME3_145));
break;
}
case PLUGIN_INIT:
{
//If configured interrupt pin differs from configured, release old pin first
if ((Settings.TaskDevicePin1[event->TaskIndex]!=Plugin_145_IRQ_pin) && (Plugin_145_IRQ_pin!=-1))
{
addLog(LOG_LEVEL_DEBUG, F("IO-PIN changed, deatachinterrupt old pin"));
detachInterrupt(Plugin_145_IRQ_pin);
}
LoadCustomTaskSettings(event->TaskIndex, (byte*)&PLUGIN_145_ExtraSettings, sizeof(PLUGIN_145_ExtraSettings));
addLog(LOG_LEVEL_INFO, F("Extra Settings PLUGIN_145 loaded"));
PLUGIN_145_rf.init();
Plugin_145_IRQ_pin = Settings.TaskDevicePin1[event->TaskIndex];
pinMode(Plugin_145_IRQ_pin, INPUT);
attachInterrupt(Plugin_145_IRQ_pin, PLUGIN_145_ITHOinterrupt, RISING);
addLog(LOG_LEVEL_INFO, F("CC1101 868Mhz transmitter initialized"));
PLUGIN_145_rf.initReceive();
PLUGIN_145_InitRunned=true;
success = true;
break;
}
case PLUGIN_EXIT:
{
addLog(LOG_LEVEL_INFO, F("EXIT PLUGIN_145"));
//remove interupt when plugin is removed
detachInterrupt(Plugin_145_IRQ_pin);
success = true;
break;
}
case PLUGIN_ONCE_A_SECOND:
{
//decrement timer when timermode is running
if (PLUGIN_145_State>=10) PLUGIN_145_Timer--;
//if timer has elapsed set Fan state to low
if ((PLUGIN_145_State>=10) && (PLUGIN_145_Timer<=0))
{ PLUGIN_145_State=1;
PLUGIN_145_Timer=0;
}
//Publish new data when vars are changed or init has runned or timer is running (update every 2 sec)
if ((PLUGIN_145_OldState!=PLUGIN_145_State) || ((PLUGIN_145_Timer>0) && (PLUGIN_145_Timer % 2==0)) || (PLUGIN_145_OldLastIDindex!=PLUGIN_145_LastIDindex)|| PLUGIN_145_InitRunned)
{
addLog(LOG_LEVEL_DEBUG, F("UPDATE by PLUGIN_ONCE_A_SECOND"));
PLUGIN_145_Publishdata(event);
sendData(event);
//reset flag set by init
PLUGIN_145_InitRunned=false;
}
//Remeber current state for next cycle
PLUGIN_145_OldState=PLUGIN_145_State;
PLUGIN_145_OldLastIDindex =PLUGIN_145_LastIDindex;
success = true;
break;
}
case PLUGIN_READ: {
// This ensures that even when Values are not changing, data is send at the configured interval for aquisition
addLog(LOG_LEVEL_DEBUG, F("UPDATE by PLUGIN_READ"));
PLUGIN_145_Publishdata(event);
success = true;
break;
}
case PLUGIN_WRITE: {
String tmpString = string;
String cmd = parseString(tmpString, 1);
String param1 = parseString(tmpString, 2);
noInterrupts();
if (cmd.equalsIgnoreCase(F("STATE")))
{
if (param1.equalsIgnoreCase(F("1111")))
{
PLUGIN_145_rf.sendCommand(IthoJoin);
addLog(LOG_LEVEL_INFO, F("Sent command for 'join' to Itho unit"));
printWebString += F("Sent command for 'join' to Itho unit");
success = true;
}
if (param1.equalsIgnoreCase(F("9999")))
{
PLUGIN_145_rf.sendCommand(IthoLeave);
addLog(LOG_LEVEL_INFO, F("Sent command for 'leave' to Itho unit"));
printWebString += F("Sent command for 'leave' to Itho unit");
success = true;
}
if (param1.equalsIgnoreCase(F("0")))
{
PLUGIN_145_rf.sendCommand(IthoStandby);
addLog(LOG_LEVEL_INFO, F("Sent command for 'standby' to Itho unit"));
printWebString += F("Sent command for 'standby' to Itho unit");
PLUGIN_145_State=0;
PLUGIN_145_Timer=0;
PLUGIN_145_LastIDindex = 0;
success = true;
}
if (param1.equalsIgnoreCase(F("1")))
{
PLUGIN_145_rf.sendCommand(IthoLow);
addLog(LOG_LEVEL_INFO, F("Sent command for 'low speed' to Itho unit"));
printWebString += F("Sent command for 'low speed' to Itho unit");
PLUGIN_145_State=1;
PLUGIN_145_Timer=0;
PLUGIN_145_LastIDindex = 0;
success = true;
}
if (param1.equalsIgnoreCase(F("2")))
{
PLUGIN_145_rf.sendCommand(IthoMedium);
addLog(LOG_LEVEL_INFO, F("Sent command for 'medium speed' to Itho unit"));
printWebString += F("Sent command for 'medium speed' to Itho unit");
PLUGIN_145_State=2;
PLUGIN_145_Timer=0;
PLUGIN_145_LastIDindex = 0;
success = true;
}
if (param1.equalsIgnoreCase(F("3")))
{
PLUGIN_145_rf.sendCommand(IthoHigh);
addLog(LOG_LEVEL_INFO, F("Sent command for 'full speed' to Itho unit"));
printWebString += F("Sent command for 'full speed' to Itho unit");
PLUGIN_145_State=3;
PLUGIN_145_Timer=0;
PLUGIN_145_LastIDindex = 0;
success = true;
}
if (param1.equalsIgnoreCase(F("4")))
{
PLUGIN_145_rf.sendCommand(IthoFull);
addLog(LOG_LEVEL_INFO, F("Sent command for 'full speed' to Itho unit"));
printWebString += F("Sent command for 'full speed' to Itho unit");
PLUGIN_145_State=4;
PLUGIN_145_Timer=0;
PLUGIN_145_LastIDindex = 0;
success = true;
}
if (param1.equalsIgnoreCase(F("13")))
{
PLUGIN_145_rf.sendCommand(IthoTimer1);
addLog(LOG_LEVEL_INFO, F("Sent command for 'timer 1' to Itho unit"));
printWebString += F("Sent command for 'timer 1' to Itho unit");
PLUGIN_145_State=13;
PLUGIN_145_Timer=PLUGIN_145_Time1;
PLUGIN_145_LastIDindex = 0;
success = true;
}
if (param1.equalsIgnoreCase(F("23")))
{
PLUGIN_145_rf.sendCommand(IthoTimer2);
addLog(LOG_LEVEL_INFO, F("Sent command for 'timer 2' to Itho unit"));
printWebString += F("Sent command for 'timer 2' to Itho unit");
PLUGIN_145_State=23;
PLUGIN_145_Timer=PLUGIN_145_Time2;
PLUGIN_145_LastIDindex = 0;
success = true;
}
if (param1.equalsIgnoreCase(F("33")))
{
PLUGIN_145_rf.sendCommand(IthoTimer3);
addLog(LOG_LEVEL_INFO, F("Sent command for 'timer 3' to Itho unit"));
printWebString += F("Sent command for 'timer 3' to Itho unit");
PLUGIN_145_State=33;
PLUGIN_145_Timer=PLUGIN_145_Time3;
PLUGIN_145_LastIDindex = 0;
success = true;
}
}
interrupts();
break; }
case PLUGIN_WEBFORM_LOAD:
{
addFormSubHeader(string, F("Remote RF Controls"));
addFormTextBox(string, F("Unit ID remote 1"), F("PLUGIN_145_ID1"), PLUGIN_145_ExtraSettings.ID1, 23);
addFormTextBox(string, F("Unit ID remote 2"), F("PLUGIN_145_ID2"), PLUGIN_145_ExtraSettings.ID2, 23);
addFormTextBox(string, F("Unit ID remote 3"), F("PLUGIN_145_ID3"), PLUGIN_145_ExtraSettings.ID3, 23);
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
strcpy(PLUGIN_145_ExtraSettings.ID1, WebServer.arg(F("PLUGIN_145_ID1")).c_str());
strcpy(PLUGIN_145_ExtraSettings.ID2, WebServer.arg(F("PLUGIN_145_ID2")).c_str());
strcpy(PLUGIN_145_ExtraSettings.ID3, WebServer.arg(F("PLUGIN_145_ID3")).c_str());
SaveCustomTaskSettings(event->TaskIndex, (byte*)&PLUGIN_145_ExtraSettings, sizeof(PLUGIN_145_ExtraSettings));
success = true;
break;
}
}
return success;
}
void PLUGIN_145_ITHOinterrupt()
{
PLUGIN_145_ITHOticker.once_ms(10, PLUGIN_145_ITHOcheck);
}
void PLUGIN_145_ITHOcheck()
{
noInterrupts();
addLog(LOG_LEVEL_DEBUG, "RF signal received\n");
if (PLUGIN_145_rf.checkForNewPacket())
{
IthoCommand cmd = PLUGIN_145_rf.getLastCommand();
String Id = PLUGIN_145_rf.getLastIDstr();
String log = F("device-ID remote: ");
log += Id;
log += F(" ,Command received=");
int index = PLUGIN_145_RFRemoteIndex(Id);
// IF id is know index should be >0
if (index>0)
{
switch (cmd)
{
case IthoUnknown:
log += F("unknown\n");
break;
case IthoStandby:
log += F("standby\n");
PLUGIN_145_State = 0;
PLUGIN_145_Timer = 0;
PLUGIN_145_LastIDindex = index;
break;
case IthoLow:
log += F("low\n");
PLUGIN_145_State = 1;
PLUGIN_145_Timer = 0;
PLUGIN_145_LastIDindex = index;
break;
case IthoMedium:
log += F("medium\n");
PLUGIN_145_State = 2;
PLUGIN_145_Timer = 0;
PLUGIN_145_LastIDindex = index;
break;
case IthoHigh:
log += F("high\n");
PLUGIN_145_State = 3;
PLUGIN_145_Timer = 0;
PLUGIN_145_LastIDindex = index;
break;
case IthoFull:
log += F("full\n");
PLUGIN_145_State = 4;
PLUGIN_145_Timer = 0;
PLUGIN_145_LastIDindex = index;
break;
case IthoTimer1:
log += +F("timer1\n");
PLUGIN_145_State = 13;
PLUGIN_145_Timer = PLUGIN_145_Time1;
PLUGIN_145_LastIDindex = index;
break;
case IthoTimer2:
log += F("timer2\n");
PLUGIN_145_State = 23;
PLUGIN_145_Timer = PLUGIN_145_Time2;
PLUGIN_145_LastIDindex = index;
break;
case IthoTimer3:
log += F("timer3\n");
PLUGIN_145_State = 33;
PLUGIN_145_Timer = PLUGIN_145_Time3;
PLUGIN_145_LastIDindex = index;
break;
case IthoJoin:
log += F("join\n");
break;
case IthoLeave:
log += F("leave\n");
break;
}
addLog(LOG_LEVEL_DEBUG, log);
}
else {
log = F("Device-ID:");
log += Id;
log += F(" IGNORED");
addLog(LOG_LEVEL_DEBUG, log);
}
}
interrupts();
}
void PLUGIN_145_Publishdata(struct EventStruct *event)
{
UserVar[event->BaseVarIndex]=PLUGIN_145_State;
UserVar[event->BaseVarIndex+1]=PLUGIN_145_Timer;
UserVar[event->BaseVarIndex+2]=PLUGIN_145_LastIDindex;
PLUGIN_145_LastPublish=millis();
String log = F("State: ");
log += UserVar[event->BaseVarIndex];
addLog(LOG_LEVEL_DEBUG, log);
log = F("Timer: ");
log += UserVar[event->BaseVarIndex+1];
addLog(LOG_LEVEL_DEBUG, log);
log = F("LastIDindex: ");
log += UserVar[event->BaseVarIndex+2];
addLog(LOG_LEVEL_DEBUG, log);
}
int PLUGIN_145_RFRemoteIndex(String rfremoteid)
{
if (rfremoteid == PLUGIN_145_ExtraSettings.ID1) return 1;
else if (rfremoteid == PLUGIN_145_ExtraSettings.ID2) return 2;
else if (rfremoteid == PLUGIN_145_ExtraSettings.ID3) return 3;
else return -1;
}