forked from philibertc/micronova_controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
micronova_controller.ino
486 lines (461 loc) · 13.4 KB
/
micronova_controller.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
#define mqtt_server "192.168.X.X"
#define mqtt_port 1883
#define mqtt_topic "micronova"
#define mqtt_user "usrname"
#define mqtt_pass "passwd"
#define hydro_mode 0
#include <SoftwareSerial.h>
SoftwareSerial StoveSerial;
#define SERIAL_MODE SWSERIAL_8N2 //8 data bits, parity none, 2 stop bits
#define RESET_PIN D5
#define RX_PIN D3
#define TX_PIN D4
#define ENABLE_RX D2
#include <ArduinoOTA.h>
#include <WiFiManager.h>
#include <PubSubClient.h>
WiFiClient espClient;
PubSubClient client(espClient);
WiFiManager wm;
int deepSleep = 0;
long previousMillis;
#define pong_topic mqtt_topic "/pong"
#define state_topic mqtt_topic "/state"
//#define tempset_topic mqtt_topic "/tempset"
#define onoff_topic mqtt_topic "/onoff"
#define ambtemp_topic mqtt_topic "/ambtemp"
#define fumetemp_topic mqtt_topic "/fumetemp"
#define flame_topic mqtt_topic "/flamepower"
#define watertemp_topic mqtt_topic "/watertemp"
//#define waterset_topic mqtt_topic "/waterset"
#define waterpres_topic mqtt_topic "/waterpres"
#define in_topic mqtt_topic "/intopic"
//0 - OFF, 1 - Starting, 2 - Pellet loading, 3 - Ignition, 4 - Work, 5 - Brazier cleaning, 6 - Final cleaning, 7 - Standby, 8 - Pellet missing alarm, 9 - Ignition failure alarm, 10 - Alarms (to be investigated)
//Checksum: Code+Address+Value on hexadecimal calculator
const char stoveOn[4] = {0x80, 0x21, 0x01, 0xA2};
const char stoveOff[4] = {0x80, 0x21, 0x06, 0xA7};
const char forceOff[4] = {0x80, 0x21, 0x00, 0xA1};
#define stoveStateAddr 0x21
#define ambTempAddr 0x01
//#define tempSetAddr 0x7D
#define fumesTempAddr 0x3E
#define flamePowerAddr 0x34
#define waterTempAddr 0x03
//#define waterSetAddr 0x36
#define waterPresAddr 0x3C
uint8_t stoveState, /*tempSet, */fumesTemp, flamePower, waterTemp /*, waterSet*/;
float ambTemp, waterPres;
char stoveRxData[2]; //When the heater is sending data, it sends two bytes: a checksum and the value
void setup_wifi() //Setup WiFiManager and connect to WiFi
{
ArduinoOTA.setHostname(mqtt_topic);
ArduinoOTA.setPassword("micronova");
ArduinoOTA.begin();
WiFi.mode(WIFI_STA);
wm.setConnectTimeout(30);
wm.autoConnect(mqtt_topic);
}
void reconnect() //Connect to MQTT server
{
//Loop until we're reconnected
while (!client.connected())
{
Serial.println(mqtt_user);
Serial.println(mqtt_pass);
Serial.print("Attempting MQTT connection...");
String clientId = "ESPClient-";
clientId += String(random(0xffff), HEX); //Random client ID
if (client.connect(clientId.c_str(), mqtt_user, mqtt_pass))
{
Serial.println("connected");
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
//Wait 5 seconds before retrying
delay(5000);
}
}
}
void IRAM_ATTR fullReset() //Reset all the settings but without erasing the program
{
Serial.println("Resetting…");
wm.resetSettings();
ESP.restart();
}
void callback(char *topic, byte *payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
if ((char)payload[1] == 'N')
{
for (int i = 0; i < 4; i++)
{
if (stoveState > 5)
{
StoveSerial.write(stoveOn[i]);
delay(1);
}
else if (stoveState == 0)
{
StoveSerial.write(stoveOn[i]);
delay(1);
}
}
client.publish(onoff_topic, "ON", true);
delay(1000);
getStates();
}
else if ((char)payload[1] == 'F')
{
for (int i = 0; i < 4; i++)
{
if (stoveState < 6)
{
if (stoveState > 0)
{
StoveSerial.write(stoveOff[i]);
delay(1);
}
}
}
client.publish(onoff_topic, "OFF", true);
delay(1000);
getStates();
}
else if ((char)payload[0] == '0')
{
for (int i = 0; i < 4; i++)
{
if (stoveState < 6)
{
if (stoveState > 0)
{
StoveSerial.write(stoveOff[i]);
delay(1);
}
}
}
client.publish(onoff_topic, "OFF", true);
delay(1000);
getStates();
}
else if ((char)payload[0] == '1')
{
for (int i = 0; i < 4; i++)
{
if (stoveState > 5)
{
StoveSerial.write(stoveOn[i]);
delay(1);
}
else if (stoveState == 0)
{
StoveSerial.write(stoveOn[i]);
delay(1);
}
client.publish(onoff_topic, "ON", true);
delay(1000);
getStates();
}
}
else if ((char)payload[0] == 'f')
{
if ((char)payload[1] == 'o')
{
for (int i = 0; i < 4; i++)
{
StoveSerial.write(forceOff[i]);
delay(1);
}
client.publish(onoff_topic, "OFF", true);
delay(1000);
getStates();
}
}
else if ((char)payload[0] == 'S')
{
deepSleep = 1;
}
else if ((char)payload[0] == 'W')
{
deepSleep = 0;
}
else if ((char)payload[2] == 's')
{
fullReset();
}
}
void checkStoveReply() //Works only when request is RAM
{
uint8_t rxCount = 0;
stoveRxData[0] = 0x00;
stoveRxData[1] = 0x00;
while (StoveSerial.available()) //It has to be exactly 2 bytes, otherwise it's an error
{
stoveRxData[rxCount] = StoveSerial.read();
rxCount++;
}
digitalWrite(ENABLE_RX, HIGH);
if (rxCount == 2)
{
byte val = stoveRxData[1];
byte checksum = stoveRxData[0];
byte param = checksum - val;
Serial.printf("Param=%01x value=%01x ", param, val);
switch (param)
{
case stoveStateAddr:
stoveState = val;
switch (stoveState)
{
case 0:
client.publish(state_topic, "Off", true);
delay(1000);
client.publish(onoff_topic, "OFF", true);
break;
case 1:
client.publish(state_topic, "Starting", true);
delay(1000);
client.publish(onoff_topic, "ON", true);
break;
case 2:
client.publish(state_topic, "Pellet loading", true);
delay(1000);
client.publish(onoff_topic, "ON", true);
break;
case 3:
client.publish(state_topic, "Ignition", true);
delay(1000);
client.publish(onoff_topic, "ON", true);
break;
case 4:
client.publish(state_topic, "Working", true);
delay(1000);
client.publish(onoff_topic, "ON", true);
break;
case 5:
client.publish(state_topic, "Brazier cleaning", true);
break;
case 6:
client.publish(state_topic, "Final cleaning", true);
delay(1000);
client.publish(onoff_topic, "OFF", true);
break;
case 7:
client.publish(state_topic, "Standby", true);
delay(1000);
client.publish(onoff_topic, "OFF", true);
break;
case 8:
client.publish(state_topic, "Pellet missing", true);
break;
case 9:
client.publish(state_topic, "Ignition failure", true);
delay(1000);
client.publish(onoff_topic, "OFF", true);
break;
case 10:
client.publish(state_topic, "Alarm", true);
break;
}
Serial.printf("Stove %s\n", stoveState ? "ON" : "OFF");
break;
case ambTempAddr:
ambTemp = (float)val / 2;
client.publish(ambtemp_topic, String(ambTemp).c_str(), true);
Serial.print("T. amb. ");
Serial.println(ambTemp);
break;
/*case tempSetAddr:
tempSet = val;
client.publish(tempset_topic, String(tempSet).c_str(), true);
Serial.printf("T. set %d\n", tempSet);
break;*/
case fumesTempAddr:
fumesTemp = val;
client.publish(fumetemp_topic, String(fumesTemp).c_str(), true);
Serial.printf("T. fumes %d\n", fumesTemp);
break;
case flamePowerAddr:
if (stoveState < 6)
{
if (stoveState > 0)
{
flamePower = map(val, 0, 16, 10, 100);
}
}
else
{
flamePower = 0;
}
client.publish(flame_topic, String(flamePower).c_str(), true);
Serial.printf("Fire %d\n", flamePower);
break;
case waterTempAddr:
waterTemp = val;
client.publish(watertemp_topic, String(waterTemp).c_str(), true);
Serial.printf("T. water %d\n", waterTemp);
break;
/*case waterSetAddr:
waterSet = val;
client.publish(char_waterset_topic, String(waterSet).c_str(), true);
Serial.printf("T. water set %d\n", waterSet);
break;*/
case waterPresAddr:
waterPres = (float)val / 10;
client.publish(waterpres_topic, String(waterPres).c_str(), true);
Serial.print("Pressure ");
Serial.println(waterPres);
break;
}
}
}
void getStoveState() //Get detailed stove state
{
const byte readByte = 0x00;
StoveSerial.write(readByte);
delay(1);
StoveSerial.write(stoveStateAddr);
digitalWrite(ENABLE_RX, LOW);
delay(80);
checkStoveReply();
}
void getAmbTemp() //Get room temperature
{
const byte readByte = 0x00;
StoveSerial.write(readByte);
delay(1);
StoveSerial.write(ambTempAddr);
digitalWrite(ENABLE_RX, LOW);
delay(80);
checkStoveReply();
}
/*void getTempSet() //Get the thermostat setting
{
const byte readByte = 0x20;
StoveSerial.write(readByte);
delay(1);
StoveSerial.write(tempSetAddr);
digitalWrite(ENABLE_RX, LOW);
delay(60);
checkStoveReply();
}*/
void getFumeTemp() //Get flue gas temperature
{
const byte readByte = 0x00;
StoveSerial.write(readByte);
delay(1);
StoveSerial.write(fumesTempAddr);
digitalWrite(ENABLE_RX, LOW);
delay(80);
checkStoveReply();
}
void getFlamePower() //Get the flame power (0, 1, 2, 3, 4, 5)
{
const byte readByte = 0x00;
StoveSerial.write(readByte);
delay(1);
StoveSerial.write(flamePowerAddr);
digitalWrite(ENABLE_RX, LOW);
delay(80);
checkStoveReply();
}
void getWaterTemp() //Get the temperature of the water (if you have an hydro heater)
{
const byte readByte = 0x00;
StoveSerial.write(readByte);
delay(1);
StoveSerial.write(waterTempAddr);
digitalWrite(ENABLE_RX, LOW);
delay(80);
checkStoveReply();
}
/*void getWaterSet() //Get the temperature of the water (if you have an hydro heater)
{
const byte readByte = 0x00;
StoveSerial.write(readByte);
delay(1);
StoveSerial.write(waterSetAddr);
digitalWrite(ENABLE_RX, LOW);
delay(80);
checkStoveReply();
}*/
void getWaterPres() //Get the temperature of the water (if you have an hydro heater)
{
const byte readByte = 0x00;
StoveSerial.write(readByte);
delay(1);
StoveSerial.write(waterPresAddr);
digitalWrite(ENABLE_RX, LOW);
delay(80);
checkStoveReply();
}
void getStates() //Calls all the get…() functions
{
getStoveState();
delay(100);
getAmbTemp();
delay(100);
/*getTempSet();
delay(100);*/
getFumeTemp();
delay(100);
getFlamePower();
if (hydro_mode == 1)
{
delay(100);
getWaterTemp();
delay(100);
/*getWaterSet();
delay(100);*/
getWaterPres();
}
}
void setup()
{
pinMode(ENABLE_RX, OUTPUT);
digitalWrite(ENABLE_RX, HIGH); //The led of the optocoupler is off
pinMode(RESET_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(RESET_PIN), fullReset, FALLING); //We setup the reinit interrupt
Serial.begin(115200);
StoveSerial.begin(1200, SERIAL_MODE, RX_PIN, TX_PIN, false, 256);
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
client.subscribe(in_topic);
}
void loop()
{
if (!client.connected())
{
reconnect();
client.subscribe(in_topic);
}
client.loop();
ArduinoOTA.handle();
unsigned long currentMillis = millis();
if (previousMillis > currentMillis)
{
previousMillis = 0;
}
if (currentMillis - previousMillis >= 25000)
{
previousMillis = currentMillis;
getStates();
client.publish(pong_topic, "Connected");
}
if (deepSleep == 1) //Does not work without hardaware modification (a cable must be connected between RST and D0)
{
Serial.println("Deep Sleep");
ESP.deepSleepInstant(300e6);
}
}