-
Notifications
You must be signed in to change notification settings - Fork 8
/
cmd2jura.ino
158 lines (118 loc) · 4.08 KB
/
cmd2jura.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
/*
cmd2jura.ino V1.00
ESP8266 IP Gateway for Jura coffee machines
Usage:
Open "http://jura/" or "curl -d 'TY:' http://jura/api"
(C) 2017 Hajo Noerenberg
http://www.noerenberg.de/
https://github.com/hn/jura-coffee-machine
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3.0 as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <SoftwareSerial.h>
#define WIFINAME "JURA"
#define GPIORX 12
#define GPIOTX 13
#define GPIOLEDIO 4
#define GPIOLEDPULSE 0
SoftwareSerial softserial(GPIORX, GPIOTX);
ESP8266WebServer webserver(80);
String cmd2jura(String outbytes) {
String inbytes;
int w = 0;
while (softserial.available()) {
softserial.read();
}
outbytes += "\r\n";
for (int i = 0; i < outbytes.length(); i++) {
for (int s = 0; s < 8; s += 2) {
char rawbyte = 255;
bitWrite(rawbyte, 2, bitRead(outbytes.charAt(i), s + 0));
bitWrite(rawbyte, 5, bitRead(outbytes.charAt(i), s + 1));
softserial.write(rawbyte);
}
delay(8);
}
int s = 0;
char inbyte;
while (!inbytes.endsWith("\r\n")) {
if (softserial.available()) {
byte rawbyte = softserial.read();
bitWrite(inbyte, s + 0, bitRead(rawbyte, 2));
bitWrite(inbyte, s + 1, bitRead(rawbyte, 5));
if ((s += 2) >= 8) {
s = 0;
inbytes += inbyte;
}
} else {
delay(10);
}
if (w++ > 500) {
return "";
}
}
return inbytes.substring(0, inbytes.length() - 2);
}
void handle_api() {
String cmd;
String result;
if (webserver.method() != HTTP_POST) {
webserver.send(405, "text/plain", "Method Not Allowed");
return;
}
cmd = webserver.arg("plain");
if (cmd.length() < 3) {
webserver.send(400, "text/plain", "Bad Request");
return;
}
digitalWrite(GPIOLEDIO, HIGH);
result = cmd2jura(cmd);
digitalWrite(GPIOLEDIO, LOW);
if (result.length() < 3) {
webserver.send(503, "text/plain", "Service Unavailable");
return;
}
webserver.send(200, "text/plain", result);
}
void handle_web() {
String html;
html = "<!DOCTYPE html><html><body><h1>☕ Jura Coffee Machine Gateway</h1>";
html += "<form onsubmit=\"return s(this)\"><input type=\"text\" placeholder=\"enter command\" id=\"c\" list=\"l\" />";
html += "<input type=\"submit\" /><datalist id=\"l\"><option value=\"TY:\"><option value=\"AN:01\">";
html += "<option value=\"AN:02\"><option value=\"RT:00\"><option value=\"RR:00\"></datalist></form>";
html += "<ul style=\"font-family: monospace\" id=\"r\"></ul><script>function s(f) { var x = new XMLHttpRequest();";
html += "x.open('POST', '/api', true); x.onreadystatechange = function() { if(x.readyState === XMLHttpRequest.DONE && ";
html += "x.status === 200) { var r = document.getElementById('r'); r.innerHTML = '<li>' + Date().toLocaleString() + ";
html += "': ' + f.c.value + ' → ' + x.responseText + '</li>' + r.innerHTML; }}; x.send(f.c.value);";
html += "return false;}</script></body></html>";
webserver.send(200, "text/html", html);
}
void setup() {
// put your setup code here, to run once:
wifi_station_set_hostname(WIFINAME);
WiFiManager wifimanager;
wifimanager.autoConnect(WIFINAME);
webserver.on("/", handle_web);
webserver.on("/api", handle_api);
webserver.begin();
softserial.begin(9600);
pinMode(GPIOLEDIO, OUTPUT);
pinMode(GPIOLEDPULSE, OUTPUT);
}
int i = 0;
void loop() {
// put your main code here, to run repeatedly:
webserver.handleClient();
i = (i + 1) % 200000;
digitalWrite(GPIOLEDPULSE, (i < 100000) ? LOW : HIGH);
}