-
Notifications
You must be signed in to change notification settings - Fork 3
/
withWebserver_EP3200_Philips.ino
541 lines (481 loc) · 17 KB
/
withWebserver_EP3200_Philips.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
#include <Arduino.h>
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define GET_CHIPID() (ESP.getChipId())
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#include <SPIFFS.h>
#include <HTTPClient.h>
#define GET_CHIPID() ((uint16_t)(ESP.getEfuseMac()>>32))
#endif
#include <FS.h>
#include <ArduinoOTA.h>
#include <SoftwareSerial.h>
#include <PubSubClient.h>
#include <AutoConnect.h>
#if defined(ESP8266) && !defined(D5)
#define D5 (14) //Rx from display
#define D6 (12) //Tx not used
#define D7 (13) //Gnd for display (to switch it on and off)
#endif
//SoftwareSerial to read Display TX (D5 = RX, D6 = unused)
SoftwareSerial swSer (D5, D6);
#define PARAM_FILE "/param.json"
#define AUX_SETTING_URI "/mqtt_setting"
#define AUX_SAVE_URI "/mqtt_save"
#define AUX_TEST_URI "/test"
// Adjusting WebServer class with between ESP8266 and ESP32.
#if defined(ARDUINO_ARCH_ESP8266)
typedef ESP8266WebServer WiFiWebServer;
#elif defined(ARDUINO_ARCH_ESP32)
typedef WebServer WiFiWebServer;
#endif
AutoConnect portal;
AutoConnectConfig config;
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
//Definition of functions:
char convertCharToHex(char ch);
void runCustomCommand(String customCmd, int length);
void callback(String topic, byte* message, int length);
void serialReadPublish(char newStatus[]);
void serialSend(byte command[], int sendCount);
void redirect(String uri);
//MQTT Settings
String mqttServer = "192.168.1.99";
String mqttPort = "1883";
String mqttUser = "admin";
String mqttPW = "admin";
//serial Input
char serInCommand[39];
char serInCommand_old[39];
unsigned long timestampLastSerialMsg;
byte serInIdx = 0;
//commands
byte powerOff[] = {0xD5, 0x55, 0x00, 0x01, 0x03, 0x00, 0x0E, 0x01, 0x00, 0x00, 0x39, 0x39};
byte powerOn[] = {0xD5, 0x55, 0x0A, 0x01, 0x03, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x2A, 0x10};
byte powerOn1[] = {0xD5, 0x55, 0x02, 0x01, 0x03, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x1C, 0x17};
byte espresso[] = {0xD5, 0x55, 0x00, 0x01, 0x03, 0x00, 0x0E, 0x02, 0x00, 0x00, 0x2D, 0x2F};
byte coffee[] = {0xd5, 0x55, 0x00, 0x01, 0x03, 0x00, 0x0e, 0x08, 0x00, 0x00, 0x1d, 0x1e};
byte americano[] = {0xD5, 0x55, 0x00, 0x01, 0x03, 0x00, 0x0E, 0x20, 0x00, 0x00, 0x04, 0x15};
byte cappuccino[] = {0xD5, 0x55, 0x00, 0x01, 0x03, 0x00, 0x0E, 0x04, 0x00, 0x00, 0x05, 0x03};
byte latteMacchiato[] = {0xD5, 0x55, 0x00, 0x01, 0x03, 0x00, 0x0E, 0x10, 0x00, 0x00, 0x2D, 0x24};
byte hotWater[] = {0xD5, 0x55, 0x00, 0x01, 0x03, 0x00, 0x0E, 0x00, 0x01, 0x00, 0x39, 0x38};
byte coffeePulver[] = {0xD5, 0x55, 0x00, 0x01, 0x03, 0x00, 0x0E, 0x00, 0x02, 0x00, 0x2D, 0x2D};
byte coffeeWater[] = {0xD5, 0x55, 0x00, 0x01, 0x03, 0x00, 0x0E, 0x00, 0x04, 0x00, 0x04, 0x07};
byte coffeeMilk[] = {0xD5, 0x55, 0x00, 0x01, 0x03, 0x00, 0x0E, 0x00, 0x08, 0x00, 0x1F, 0x16};
byte aquaClean[] = {0xD5, 0x55, 0x00, 0x01, 0x03, 0x00, 0x0E, 0x00, 0x10, 0x00, 0x29, 0x34};
byte calcNclean[] = {0xD5, 0x55, 0x00, 0x01, 0x03, 0x00, 0x0E, 0x00, 0x20, 0x00, 0x0C, 0x35};
byte startPause[] = {0xD5, 0x55, 0x00, 0x01, 0x03, 0x00, 0x0E, 0x00, 0x00, 0x01, 0x3D, 0x30};
byte requestInfo[] = {0xD5, 0x55, 0x00, 0x01, 0x03, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x35, 0x34};
char oldStatus[19] = "";
// Declare AutoConnectElements for the page asf /mqtt_setting
ACStyle(style, "label+input,label+select{position:sticky;left:120px;width:230px!important;box-sizing:border-box;}");
ACText(header, "<h2>MQTT Settings</h2>", "text-align:center;color:#2f4f4f;padding:10px;");
ACText(caption, "MQTT Server Settings. The following MQTT commands are availabe:<br><br>coffee/command/powerOn<br>coffee/command/powerOff<br>coffee/command/hotWater<br>coffee/command/espresso<br>coffee/command/coffee<br>coffee/command/steam<br>coffee/command/coffeePulver<br>coffee/command/coffeeWater<br>coffee/command/calcNclean<br>coffee/command/aquaClean<br>coffee/command/startPause<br><br>Send the command with a count of repeats as value (typical 5).", "font-family:serif;color:#4682b4;");
ACInput(inMqttserver, mqttServer.c_str(), "MQTT-Server", "", "e.g. 192.168.172.99");
ACInput(inMqttport, mqttPort.c_str(), "MQTT Port", "", "e.g. 1883 or 1884");
ACInput(inMqttuser, mqttUser.c_str(), "MQTT User", "", "default: admin");
ACInput(inMqttpw, mqttPW.c_str(), "MQTT Password", "", "default: admin");
ACText(mqttState, "MQTT-State: none", "");
ACSubmit(save, "Save", AUX_SAVE_URI);
ACSubmit(discard, "Discard", "/");
ACElement(newline, "<hr>");
// Declare the custom Web page as /mqtt_setting and contains the AutoConnectElements
AutoConnectAux mqtt_setting(AUX_SETTING_URI, "MQTT Settings", true, {
style,
header,
caption,
newline,
inMqttserver,
inMqttport,
newline,
inMqttuser,
inMqttpw,
newline,
mqttState,
newline,
save,
discard
});
// Declare AutoConnectElements for the page as /mqtt_save
ACText(caption2, "<h4>Parameters available as:</h4>", "text-align:center;color:#2f4f4f;padding:10px;");
ACText(parameters);
ACSubmit(back2config, "Back", AUX_SETTING_URI);
// Declare the custom Web page as /mqtt_save and contains the AutoConnectElements
AutoConnectAux mqtt_save(AUX_SAVE_URI, "MQTT Settings", false, {
caption2,
parameters,
back2config
});
// Declare AutoConnectElements for the page for testing
ACText(testHeader, "<h2>Coffee Machine Test</h2>", "text-align:center;color:#2f4f4f;padding:10px;");
ACSubmit(testOn, "ON", "/on");
ACSubmit(testOff, "OFF", "/off");
// Declare the custom Web page as /mqtt_setting and contains the AutoConnectElements
AutoConnectAux coffee_test(AUX_TEST_URI, "Coffee TEST", true, {
style,
testHeader,
testOn,
testOff
});
bool mqttConnect() {
String clientId = "CoffeeMachine-" + String(GET_CHIPID(), HEX);
uint8_t retry = 3;
while (!mqttClient.connected()) {
if (mqttServer.length() <= 0)
break;
mqttClient.setServer(mqttServer.c_str(), atoi(mqttPort.c_str()));
mqttClient.setCallback(callback);
mqttClient.publish("coffee/status", "ESP_STARTUP");
Serial1.println(String("Attempting MQTT broker:") + mqttServer);
if (mqttClient.connect(clientId.c_str(), mqttUser.c_str(), mqttPW.c_str())) {
Serial1.println("Established:" + clientId);
mqttClient.subscribe("coffee/command/restart");
mqttClient.subscribe("coffee/command/powerOn");
mqttClient.subscribe("coffee/command/powerOff");
mqttClient.subscribe("coffee/command/requestInfo");
mqttClient.subscribe("coffee/command/hotWater");
mqttClient.subscribe("coffee/command/espresso");
mqttClient.subscribe("coffee/command/coffee");
mqttClient.subscribe("coffee/command/coffeePulver");
mqttClient.subscribe("coffee/command/coffeeMilk");
mqttClient.subscribe("coffee/command/cappuccino");
mqttClient.subscribe("coffee/command/latteMacchiato");
mqttClient.subscribe("coffee/command/americano");
mqttClient.subscribe("coffee/command/coffeeWater");
mqttClient.subscribe("coffee/command/calcNclean");
mqttClient.subscribe("coffee/command/aquaClean");
mqttClient.subscribe("coffee/command/startPause");
mqttClient.subscribe("coffee/command/custom");
mqttState.value = "MQTT-State: <b style=\"color: green;\">Connected</b>";
return true;
}
else {
Serial1.println("Connection failed:" + String(mqttClient.state()));
mqttState.value = "MQTT-State: <b style=\"color: red;\">Disconnected</b>";
if (!--retry){
break;
}
//delay(3000);
}
}
return false;
}
// This functions is executed when some device publishes a message to a topic that your ESP8266 is subscribed to
// Change the function below to add logic to your program, so when a device publishes a message to a topic that
// your ESP8266 is subscribed you can actually do something
void callback(String topic, byte* message, int length) {
String messageTemp;
for (int i = 0; i < length; i++) {
messageTemp += (char)message[i];
}
messageTemp += '\n';
if (topic == "coffee/command/custom") {
//For custom hex commands
runCustomCommand(messageTemp, length);
} else {
//For predefined commands
int count = messageTemp.toInt();
if (count < 0 || count > 99) {
Serial1.println("Count out of range");
} else if (topic == "coffee/command/powerOn") {
serialSend(powerOn, count);
serialSend(powerOn1, count);
//Workaround: D7 is connected to a NPN Transistor that cuts the ground from the display
//--> MC of the display reboots
digitalWrite(D7, LOW);
delay(500);
digitalWrite(D7, HIGH);
} else if (topic == "coffee/command/powerOff") {
serialSend(powerOff, count);
} else if (topic == "coffee/command/hotWater") {
serialSend(hotWater, count);
} else if (topic == "coffee/command/espresso") {
serialSend(espresso, count);
} else if (topic == "coffee/command/coffee") {
serialSend(coffee, count);
} else if (topic == "coffee/command/coffeePulver") {
serialSend(coffeePulver, count);
} else if (topic == "coffee/command/coffeeWater") {
serialSend(coffeeWater, count);
} else if (topic == "coffee/command/calcNclean") {
serialSend(calcNclean, count);
} else if (topic == "coffee/command/coffeeMilk") {
serialSend(coffeeMilk, count);
} else if (topic == "coffee/command/cappuccino") {
serialSend(cappuccino, count);
} else if (topic == "coffee/command/americano") {
serialSend(americano, count);
} else if (topic == "coffee/command/latteMacchiato") {
serialSend(latteMacchiato, count);
} else if (topic == "coffee/command/aquaClean") {
serialSend(aquaClean, count);
} else if (topic == "coffee/command/startPause") {
serialSend(startPause, count);
}
}
}
//Executes a custom command (hex string) received via mqtt
void runCustomCommand(String customCmd, int length) {
byte data2send[length];
for (int i = 0; i < length / 2; i++) {
byte extract;
char a = (char)customCmd[2 * i];
char b = (char)customCmd[2 * i + 1];
extract = convertCharToHex(a) << 4 | convertCharToHex(b);
data2send[i] = extract;
}
Serial.write(data2send, length / 2);
}
//Sends a command via serial to the coffee machine
void serialSend(byte command[], int sendCount) {
for (int i = 0; i <= sendCount; i++) {
Serial.write(command, 12);
}
}
//Checks if the status of the coffee machine had changed. If so, the new status is published via mqtt
void serialReadPublish(char newStatus[]) {
if (strcmp(newStatus, oldStatus) != 0) {
mqttClient.publish("coffee/status", newStatus);
strncpy(oldStatus, newStatus, 19);
}
}
String loadParams() {
File param = SPIFFS.open(PARAM_FILE, "r");
if (param) {
if(mqtt_setting.loadElement(param)){
mqttServer = mqtt_setting["inMqttserver"].value;
mqttServer.trim();
mqttPort = mqtt_setting["inMqttport"].value;
mqttPort.trim();
mqttUser = mqtt_setting["inMqttuser"].value;
mqttUser.trim();
mqttPW = mqtt_setting["inMqttpw"].value;
mqttPW.trim();
}
param.close();
}
else
Serial.println(PARAM_FILE " open failed");
return String("");
}
// Retreive the value of each element entered by '/mqtt_setting'.
String saveParams(AutoConnectAux& aux, PageArgument& args) {
mqttServer = inMqttserver.value;
mqttServer.trim();
mqttPort = inMqttport.value;
mqttPort.trim();
mqttUser = inMqttuser.value;
mqttUser.trim();
mqttPW = inMqttpw.value;
mqttPW.trim();
// The entered value is owned by AutoConnectAux of /mqtt_setting.
// To retrieve the elements of /mqtt_setting, it is necessary to get
// the AutoConnectAux object of /mqtt_setting.
File param = SPIFFS.open(PARAM_FILE, "w");
mqtt_setting.saveElement(param, { "inMqttserver", "inMqttport", "inMqttuser", "inMqttpw"});
param.close();
// Echo back saved parameters to AutoConnectAux page.
String echo = "Server: " + mqttServer + "<br>";
echo += "Port: " + mqttPort + "<br>";
echo += "User: " + mqttUser + "<br>";
echo += "Password: " + mqttPW + "<br>";
parameters.value = echo;
mqttClient.disconnect();
return String("");
}
void sendPowerOff(){
serialSend(powerOff, 5);
redirect(AUX_TEST_URI);
}
void sendPowerOn(){
serialSend(powerOn, 5);
serialSend(powerOn1, 5);
redirect(AUX_TEST_URI);
}
void handleRoot() {
redirect("/_ac");
}
void redirect(String uri) {
WiFiWebServer& webServer = portal.host();
webServer.sendHeader("Location", String("http://") + webServer.client().localIP().toString() + uri);
webServer.send(302, "text/plain", "");
webServer.client().flush();
webServer.client().stop();
}
void serialInput2Mqtt(){
while (Serial.available() > 0) {
char b = Serial.read();
//Serial1.print(b);
sprintf(&serInCommand[serInIdx], "%02x", b);
serInIdx += 2;
//Skip input if it doesn't start with 0xd5
if (serInIdx == 2 && b != 0xd5) {
serInIdx = 0;
}
if (serInIdx > 37) {
serInCommand[38] = '\0';
timestampLastSerialMsg = millis();
if (strcmp(serInCommand, serInCommand_old) != 0) {
mqttClient.publish("coffee/status", serInCommand, 38);
// Serial1.println(serInCommand);
// Serial1.println(serInCommand_old);
memcpy( serInCommand_old, serInCommand, 39);
}
serInIdx = 0;
}
}
//Send signal that the coffee machine is off if there is no incomming message for more than 3 seconds
if(timestampLastSerialMsg != 0 && millis() - timestampLastSerialMsg > 3000){
mqttClient.publish("coffee/status", "d5550100000000000000000000000000000626", 38);
timestampLastSerialMsg = 0;
}
}
void setup() {
delay(1000);
// Serial1 port for debugging purposes
Serial1.begin(115200);
// Serial communication with coffee machine (Main Controller + ESP)
Serial.begin(115200);
// Serial communication to read display TX and give it over to Serial
swSer.begin(115200);
SPIFFS.begin();
config.title = "Philips MQTT Coffee Machine";
config.apid = "esp-coffee-machine";
config.psk = "";
config.portalTimeout = 100; // Not sure the parameter to set
config.retainPortal = true;
portal.config(config);
loadParams();
// Join the custom Web pages and register /mqtt_save handler
portal.join({ mqtt_setting, mqtt_save, coffee_test });
portal.on(AUX_SAVE_URI, saveParams);
Serial1.print("WiFi ");
if (portal.begin()) {
Serial1.println("connected:" + WiFi.SSID());
Serial1.println("IP:" + WiFi.localIP().toString());
//Setup OTA Update
ArduinoOTA.onStart([]() {
Serial1.println("Start OTA");
});
ArduinoOTA.onEnd([]() {
Serial1.println("End OTA");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial1.printf("Progress: %u%%\n", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial1.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial1.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial1.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial1.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial1.println("End Failed");
});
ArduinoOTA.begin();
}
else {
Serial1.println("connection failed:" + String(WiFi.status()));
}
WiFiWebServer& webServer = portal.host();
webServer.on("/", handleRoot);
webServer.on("/on", sendPowerOn);
webServer.on("/off", sendPowerOff);
pinMode(D7, OUTPUT);
digitalWrite(D7, HIGH);
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
//The following things can only be handled if wifi is connected
ArduinoOTA.handle();
if (!mqttClient.connected()) {
mqttConnect();
}
mqttClient.loop();
serialInput2Mqtt();
}
while (swSer.available() > 0) {
Serial.write(swSer.read());
}
portal.handleClient();
}
//Return hex value to char
char convertCharToHex(char ch)
{
char returnType;
switch (ch)
{
case '0':
returnType = 0;
break;
case '1' :
returnType = 1;
break;
case '2':
returnType = 2;
break;
case '3':
returnType = 3;
break;
case '4' :
returnType = 4;
break;
case '5':
returnType = 5;
break;
case '6':
returnType = 6;
break;
case '7':
returnType = 7;
break;
case '8':
returnType = 8;
break;
case '9':
returnType = 9;
break;
case 'A':
returnType = 10;
break;
case 'B':
returnType = 11;
break;
case 'C':
returnType = 12;
break;
case 'D':
returnType = 13;
break;
case 'E':
returnType = 14;
break;
case 'F' :
returnType = 15;
break;
case 'a':
returnType = 10;
break;
case 'b':
returnType = 11;
break;
case 'c':
returnType = 12;
break;
case 'd':
returnType = 13;
break;
case 'e':
returnType = 14;
break;
case 'f' :
returnType = 15;
break;
default:
returnType = 0;
break;
}
return returnType;
}