-
Notifications
You must be signed in to change notification settings - Fork 7
/
4_INDUKTION.ino
637 lines (601 loc) · 15.6 KB
/
4_INDUKTION.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
class induction
{
int8_t PIN_WHITE = D7; // D7 Relay white
int8_t PIN_YELLOW = D6; // D6 Command channel yellow AUSGABE AN PLATTE
int8_t PIN_INTERRUPT = D5; // D5 Back channel blue EINGABE VON PLATTE
uint8_t power = 0;
uint8_t newPower = 0;
uint8_t oldPower = 0;
int8_t CMD_CUR = 0; // Aktueller Befehl
boolean isRelayon = false; // Systemstatus: ist das Relais in der Platte an?
boolean oldisRelayon = false; // Systemstatus: ist das Relais in der Platte an?
boolean isInduon = false; // Systemstatus: ist Power > 0?
boolean oldisInduon = false;
boolean isPower = false;
String mqtttopic = "";
boolean isEnabled = false;
uint8_t powerLevelOnError = 100; // 100% schaltet das Event handling für Induktion aus
uint8_t powerLevelBeforeError = 0; // in error event save last power state
bool induction_state = true; // Error state induction
unsigned long timeTurnedoff;
unsigned long lastInterrupt;
unsigned long lastCommand;
bool inputStarted = false;
uint8_t inputCurrent = 0;
unsigned char inputBuffer[33];
long powerSampletime = 20000;
unsigned long powerLast;
long powerHigh = powerSampletime; // Dauer des "HIGH"-Anteils im Schaltzyklus
long powerLow = 0;
// Induktion Signallaufzeiten
const int16_t SIGNAL_HIGH = 5120;
const int16_t SIGNAL_HIGH_TOL = 1500;
const int16_t SIGNAL_LOW = 1280;
const int16_t SIGNAL_LOW_TOL = 500;
const int16_t SIGNAL_START = 25;
const int16_t SIGNAL_START_TOL = 10;
const int16_t SIGNAL_WAIT = 10;
const int16_t SIGNAL_WAIT_TOL = 5;
/* Binäre Signale für Induktionsplatte */
int16_t CMD[6][33] = {
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, // Aus
{1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0}, // P1
{1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0}, // P2
{1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0}, // P3
{1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0}, // P4
{1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0}}; // P5
uint8_t PWR_STEPS[6] = {0, 20, 40, 60, 80, 100};
public:
induction()
{
setupCommands();
}
void change(const int8_t &pinwhite, const int8_t &pinyellow, const int8_t &pinblue, const String &topic, const bool &is_enabled, const uint8_t &powerLevel)
{
if (isEnabled)
{
// aktuelle PINS deaktivieren
if (isPin(PIN_WHITE))
{
digitalWrite(PIN_WHITE, HIGH);
pins_used[PIN_WHITE] = false;
}
if (isPin(PIN_YELLOW))
{
digitalWrite(PIN_YELLOW, HIGH);
pins_used[PIN_YELLOW] = false;
}
if (isPin(PIN_INTERRUPT))
{
detachInterrupt(PIN_INTERRUPT);
pinMode(PIN_INTERRUPT, OUTPUT);
// digitalWrite(PIN_INTERRUPT, HIGH);
pins_used[PIN_INTERRUPT] = false;
}
mqtt_unsubscribe();
}
// Neue Variablen Speichern
mqtttopic = topic;
powerLevelOnError = powerLevel;
induction_state = true;
isEnabled = is_enabled;
inductionStatus = isEnabled;
if (isEnabled)
{
// neue PINS aktiveren
if (isPin(PIN_WHITE))
{
PIN_WHITE = pinwhite;
pinMode(PIN_WHITE, OUTPUT);
digitalWrite(PIN_WHITE, HIGH);
pins_used[PIN_WHITE] = true;
}
if (isPin(PIN_YELLOW))
{
PIN_YELLOW = pinyellow;
pinMode(PIN_YELLOW, OUTPUT);
digitalWrite(PIN_YELLOW, HIGH);
pins_used[PIN_YELLOW] = true;
}
PIN_INTERRUPT = pinblue; // off possible
if (isPin(PIN_INTERRUPT)) // D7
{
pinMode(PIN_INTERRUPT, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PIN_INTERRUPT), readInputWrap, CHANGE);
pins_used[PIN_INTERRUPT] = true;
}
mqtt_subscribe();
}
}
void mqtt_subscribe()
{
if (isEnabled)
{
if (pubsubClient.connected())
{
char subscribemsg[50];
mqtttopic.toCharArray(subscribemsg, 50);
DEBUG_VERBOSE("IND", "Subscribing to %s", subscribemsg);
pubsubClient.subscribe(subscribemsg);
}
}
}
void mqtt_unsubscribe()
{
if (pubsubClient.connected())
{
char subscribemsg[50];
mqtttopic.toCharArray(subscribemsg, 50);
DEBUG_VERBOSE("IND", "Unsubscribing from %s", subscribemsg);
pubsubClient.unsubscribe(subscribemsg);
}
}
void handlemqtt(unsigned char *payload, unsigned int length)
{
JsonDocument doc;
JsonDocument filter;
filter["state"] = true;
filter["power"] = true;
DeserializationError error = deserializeJson(doc, (const char *)payload, DeserializationOption::Filter(filter));
if (error)
{
DEBUG_ERROR("IND", "handlemqtt deserialize Json error %s", error.c_str());
return;
}
if (doc["state"] == "off")
newPower = 0;
else
newPower = doc["power"];
}
void setupCommands()
{
for (uint8_t i = 0; i < 33; i++)
{
for (uint8_t j = 0; j < 6; j++)
{
if (CMD[j][i] == 1)
CMD[j][i] = SIGNAL_HIGH;
else
CMD[j][i] = SIGNAL_LOW;
}
}
}
bool updateRelay()
{
if (isInduon == true && isRelayon == false)
{ /* Relais einschalten */
digitalWrite(PIN_WHITE, HIGH);
return true;
}
if (isInduon == false && isRelayon == true)
{ /* Relais ausschalten */
if (millis() > timeTurnedoff + DEF_DELAY_IND)
{
digitalWrite(PIN_WHITE, LOW);
return false;
}
}
if (isInduon == false && isRelayon == false)
{ /* Ist aus, bleibt aus. */
return false;
}
return true; /* Ist an, bleibt an. */
}
void Update()
{
updatePower();
isRelayon = updateRelay();
if (isInduon && power > 0)
{
if (millis() > powerLast + powerSampletime)
{
powerLast = millis();
}
if (millis() > powerLast + powerHigh)
{
sendCommand(CMD[CMD_CUR - 1]);
isPower = false;
DEBUG_VERBOSE("IND", "off: %d level: %d time: %lu ms", power, CMD_CUR - 1, powerLast);
}
else
{
sendCommand(CMD[CMD_CUR]);
isPower = true;
DEBUG_VERBOSE("IND", "on: %d level: %d time: %lu ms", power, CMD_CUR, powerLast);
}
}
else if (isRelayon)
{
sendCommand(CMD[0]);
}
}
void inductionNewPower(int16_t val)
{
newPower = constrain(val, 0, 100);
}
void updatePower()
{
if (power != newPower) // Neuer Befehl empfangen
{
power = newPower;
timeTurnedoff = 0;
isInduon = true;
if (power == 0)
{
CMD_CUR = 0;
timeTurnedoff = millis();
isInduon = false;
/* Wie lange "HIGH" oder "LOW" */
powerHigh = powerSampletime;
powerLow = 0;
}
else
{
for (uint8_t i = 1; i < 7; i++)
{
if (power <= PWR_STEPS[i])
{
CMD_CUR = i;
/* Wie lange "HIGH" oder "LOW" */
powerLow = powerSampletime * (PWR_STEPS[i] - power) / 20L;
powerHigh = powerSampletime - powerLow;
return;
}
}
}
}
}
void sendCommand(int16_t command[33])
{
digitalWrite(PIN_YELLOW, HIGH);
millis2wait(SIGNAL_START);
digitalWrite(PIN_YELLOW, LOW);
millis2wait(SIGNAL_WAIT);
// PIN_YELLOW := Ausgabe an IDS2
for (uint8_t i = 0; i < 33; i++)
{
digitalWrite(PIN_YELLOW, HIGH);
micros2wait(command[i]);
digitalWrite(PIN_YELLOW, LOW);
micros2wait(SIGNAL_LOW);
}
}
void readInput()
{
if (PIN_INTERRUPT == -100)
return;
// Variablen sichern
bool ishigh = digitalRead(PIN_INTERRUPT);
unsigned long newInterrupt = micros();
long signalTime = newInterrupt - lastInterrupt;
// Glitch rausfiltern
if (signalTime > 10)
{
if (ishigh) // PIN ist auf Rising, Bit senden hat gestartet
{
lastInterrupt = newInterrupt;
}
else // Bit ist auf Falling, Bit Übertragung fertig. Auswerten.
{
if (!inputStarted) // suche noch nach StartBit.
{
if (signalTime < 35000L && signalTime > 15000L)
{
inputStarted = true;
inputCurrent = 0;
}
}
else // Start Bit gefunden. Aufnahme
{
if (inputCurrent < 34) // nur bis 33 aufnehmen.
{
if (signalTime < (SIGNAL_HIGH + SIGNAL_HIGH_TOL) && signalTime > (SIGNAL_HIGH - SIGNAL_HIGH_TOL))
{
// HIGH BIT erkannt
inputBuffer[inputCurrent] = 1;
inputCurrent += 1;
}
if (signalTime < (SIGNAL_LOW + SIGNAL_LOW_TOL) && signalTime > (SIGNAL_LOW - SIGNAL_LOW_TOL))
{
// LOW BIT erkannt
inputBuffer[inputCurrent] = 0;
inputCurrent += 1;
}
}
else // Aufnahme beendet
{
uint8_t errorIDS = inputBuffer[13] * 8 + inputBuffer[14] * 4 + inputBuffer[15] * 2 + inputBuffer[16] * 1;
if (newError != errorIDS)
newError = errorIDS;
inputCurrent = 0;
inputStarted = false;
}
}
}
}
}
void indERR()
{
if (isInduon && powerLevelOnError < 100 && induction_state) // powerlevelonerror == 100 -> kein event handling
{
powerLevelBeforeError = power;
DEBUG_VERBOSE("IND", "MQTT event handling induction - power level: %d event power level: %d", power, powerLevelOnError);
if (powerLevelOnError == 0)
isInduon = false;
else
newPower = powerLevelOnError;
newPower = powerLevelOnError;
induction_state = false;
Update();
}
}
int8_t getPinWhite()
{
return PIN_WHITE;
}
int8_t getPinYellow()
{
return PIN_YELLOW;
}
int8_t getPinInterrupt()
{
return PIN_INTERRUPT;
}
void setPinWhite(int8_t val)
{
PIN_WHITE = val;
}
void setPinYellow(int8_t val)
{
PIN_YELLOW = val;
}
void setPinInterrupt(int8_t val)
{
PIN_INTERRUPT = val;
}
String getTopic()
{
return mqtttopic;
}
uint8_t getPower()
{
return power;
}
uint8_t getOldPower()
{
return oldPower;
}
void setOldPower()
{
oldPower = power;
}
int8_t getCMD_CUR()
{
return CMD_CUR;
}
uint8_t getNewPower()
{
return newPower;
}
void setNewPower(int16_t val)
{
newPower = constrain(val, 0, 100);
}
bool getisRelayon()
{
return isRelayon;
}
bool getoldisRelayon()
{
return oldisRelayon;
}
void setoldisRelayon()
{
oldisRelayon = isRelayon;
}
bool getisInduon()
{
return isInduon;
}
void setisInduon(bool val)
{
isInduon = val;
}
bool getoldisInduon()
{
return oldisInduon;
}
void setoldisInduon()
{
oldisInduon = isInduon;
}
bool getisPower()
{
return isPower;
}
bool getIsEnabled()
{
return isEnabled;
}
void setIsEnabled(bool val)
{
isEnabled = val;
}
bool getInductionState()
{
return induction_state;
}
void setInductionState(bool val)
{
induction_state = val;
}
uint8_t getPowerLevelOnError()
{
return powerLevelOnError;
}
uint8_t getPowerLevelBeforeError()
{
return powerLevelBeforeError;
}
};
induction inductionCooker = induction();
#ifdef ESP32
void ARDUINO_ISR_ATTR readInputWrap()
{
inductionCooker.readInput();
}
#elif ESP8266
ICACHE_RAM_ATTR void readInputWrap()
{
inductionCooker.readInput();
}
#endif
void handleInduction()
{
inductionCooker.Update();
}
void handleRequestInduction()
{
JsonDocument doc;
doc["enabled"] = inductionCooker.getIsEnabled();
doc["power"] = 0;
if (inductionCooker.getIsEnabled())
{
doc["relayOn"] = inductionCooker.getisRelayon();
doc["power"] = inductionCooker.getPower();
doc["state"] = inductionCooker.getInductionState();
if (inductionCooker.getisPower())
{
doc["powerLevel"] = inductionCooker.getCMD_CUR();
}
else
{
doc["powerLevel"] = max(0, inductionCooker.getCMD_CUR() - 1);
}
}
doc["topic"] = inductionCooker.getTopic();
doc["pl"] = inductionCooker.getPowerLevelOnError();
char response[measureJson(doc) + 2];
serializeJson(doc, response, sizeof(response));
replyResponse(response);
}
void handleRequestIndu()
{
String request = server.arg(0);
String message;
if (request == "pins")
{
int8_t id = server.arg(1).toInt();
int8_t pinswitched;
uint8_t tempNUMBEROFPINS = NUMBEROFPINS;
switch (id)
{
case 0:
pinswitched = inductionCooker.getPinWhite();
tempNUMBEROFPINS = NUMBEROFPINS - 1; // ohne aus (-)
break;
case 1:
pinswitched = inductionCooker.getPinYellow();
tempNUMBEROFPINS = NUMBEROFPINS - 1;
break;
case 2:
pinswitched = inductionCooker.getPinInterrupt();
tempNUMBEROFPINS = NUMBEROFPINS; // mit aus (-)
break;
}
if (isPin(pinswitched))
{
message += OPTIONSTART;
message += PinToString(pinswitched);
message += OPTIONDISABLED;
}
for (int i = 0; i < tempNUMBEROFPINS; i++)
{
if (pins_used[pins[i]] == false)
{
message += OPTIONSTART;
message += pin_names[i];
message += OPTIONEND;
}
yield();
}
}
replyResponse(message.c_str());
}
void handleSetIndu()
{
JsonDocument doc;
DeserializationError error = deserializeJson(doc, server.arg(0));
if (error)
{
DEBUG_ERROR("IND", "error deserializeJson %s", error.c_str());
replyServerError("Server error set induction");
return;
}
inductionCooker.change(StringToPin(doc["pinw"]), StringToPin(doc["piny"]), StringToPin(doc["pinb"]), doc["topic"], doc["enabled"], doc["pl"]);
saveConfig();
replyOK();
inductionSSE(true);
}
void checkIDSstate()
{
// uint8_t errorIDS = inputBuffer[13] * 8 + inputBuffer[14] * 4 + inputBuffer[15] * 2 + inputBuffer[16] * 1;
// 2^3 + 2^2 + 2^1 + 2^0
// 8 4 2 1
// portENTER_CRITICAL(&errCode);
if (newError > 0 && oldError != newError)
{
oldError = newError;
if (inductionCooker.getIsEnabled())
{
#ifdef ESP32
switch (newError)
{
case 0:
break;
case 1:
case 2: // E0
DEBUG_ERROR("IND", "GGM IDS Fehler E0: %d kein/leerer Kessel", newError);
break;
case 3: // E1
DEBUG_ERROR("IND", "GGM IDS Fehler E1: %d Stromkreisfehler", newError);
break;
case 4: // E2
// E2 unbelegt
// break;
case 5: // E3
DEBUG_ERROR("IND", "GGM IDS Fehler E3: %d Überhitzung", newError);
break;
case 6: // E4
DEBUG_ERROR("IND", "GGM IDS Fehler E4: %d Temperatursensor", newError);
break;
case 7: // E5
// E5 unbelegt
// break;
case 8: // E6
// E5 unbelegt
// break;
case 9: // E7
DEBUG_ERROR("IND", "GGM IDS Fehler E7: %d Niederspannungsschutz", newError);
break;
case 10: // E8
DEBUG_ERROR("IND", "GGM IDS Fehler E8: %d Überspannungsschutz", newError);
break;
case 11: // E9
// E9 unbelegt
// break;
case 12: // EA
// EA unbelagt
// break;
case 13: // EB
// EB unbelegt
// break;
case 14: // EC
DEBUG_ERROR("IND", "GGM IDS Fehler EC: %d Bedienfeld", newError);
break;
}
#endif
}
} // if (newError > 0 && oldError != newError)
// portEXIT_CRITICAL(&errCode);
}