-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoffeeMaker.ino
216 lines (200 loc) · 5.85 KB
/
CoffeeMaker.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
/*
CoffeeMaker: Arduino Uno Wifi REST Server for the Jura S95 CoffeeMaker
Acts as a backend for Homebridge / Siri
Gitub Repository: https://github.com/thomaswitt/CoffeeMaker
Written by Thomas Witt <https://github.com/thomaswitt>
CoffeeMaker is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CoffeeMaker 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 CoffeeMaker. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Wire.h>
#include <ArduinoWiFi.h>
#include <SoftwareSerial.h>
String uriPrefix = "/arduino/custom/";
int ledPin = 13;
/*
Jura S95 4-pin interface (from left to right):
X*** pin 4 (+5V) -> Empty
*X** pin 3 (RX) -> Arduino Pin 3
**X* pin 2 (GND) -> Arduino GND
***X pin 1 (TX) -> Arduino Pin 2
Use CONRAD Electronic, Part #1244004 (https://bit.ly/S95Connector)
*/
SoftwareSerial myCoffeemaker(2, 3); // RX (to S95 TX), TX (to S95 RX P3)
void blinkLED(int times = 1) {
for (int i = 1; i <= times; i++) {
digitalWrite(ledPin, HIGH);
delay(150);
digitalWrite(ledPin, LOW);
delay(150);
}
}
void setup() {
pinMode(ledPin, OUTPUT);
myCoffeemaker.begin(9600);
delay(1000);
myCoffeemaker.listen();
delay(1000);
Wifi.begin();
blinkLED(5);
}
void loop() {
while (Wifi.available()) {
process(Wifi);
}
delay(50);
}
void httpResult(String &dest, String result = "", bool success = true) {
if (success) {
blinkLED();
dest += F("HTTP/1.0 200 OK\n");
} else {
blinkLED(3);
dest += F("HTTP/1.0 404 Not Found\n");
}
dest += F("\n");
if (success) {
dest += result;
} else {
dest += F("ERROR: ");
dest += result;
}
dest += "\n";
dest += EOL;
}
void process(WifiData client) {
String httpReturn = "";
String request = client.readStringUntil(EOL);
if (request.startsWith(uriPrefix)) {
request.replace(uriPrefix, "");
if (request == F("turn_on")) {
toCoffeemaker(F("AN:01\r\n"));
httpResult(httpReturn, fromCoffeemaker());
} else if (request == F("turn_off")) {
toCoffeemaker(F("AN:02\r\n"));
httpResult(httpReturn, fromCoffeemaker());
} else if (request == F("flush")) {
toCoffeemaker(F("FA:02\r\n"));
httpResult(httpReturn, fromCoffeemaker());
} else if (request == F("make_coffee")) {
toCoffeemaker(F("FA:0C\r\n"));
httpResult(httpReturn, fromCoffeemaker());
} else if (request == F("status")) {
httpResult(httpReturn, getStatus());
} else if (request == F("status_binary")) {
httpResult(httpReturn, getStatus() != "off" ? "1" : "0");
} else if (request.startsWith("command")) {
// Execute ANY command via web service - use with caution!
request.replace("command/", "");
toCoffeemaker(request + "\r\n");
delay(100);
httpResult(httpReturn, fromCoffeemaker());
} else {
httpResult(httpReturn, "Unknown command " + request, false);
}
} else {
httpResult(httpReturn, "Invalid URI " + request, false);
}
client.println(httpReturn);
}
/*
S95-Commands:
AN:01 switches coffeemaker on
AN:02 switches coffeemaker off
FA:02 flush
FA:04 small cup
FA:05 two small cups
FA:06 large cup
FA:07 two large cups
FA:08 Steam portion
FA:09 Steam
FA:0C XXL cup
Thanks to:
- https://github.com/psct/sharespresso
- https://github.com/oliverk71/Coffeemaker-Payment-System
*/
void toCoffeemaker(String outputString)
{
for (byte a = 0; a < outputString.length(); a++) {
byte d0 = 255;
byte d1 = 255;
byte d2 = 255;
byte d3 = 255;
bitWrite(d0, 2, bitRead(outputString.charAt(a), 0));
bitWrite(d0, 5, bitRead(outputString.charAt(a), 1));
bitWrite(d1, 2, bitRead(outputString.charAt(a), 2));
bitWrite(d1, 5, bitRead(outputString.charAt(a), 3));
bitWrite(d2, 2, bitRead(outputString.charAt(a), 4));
bitWrite(d2, 5, bitRead(outputString.charAt(a), 5));
bitWrite(d3, 2, bitRead(outputString.charAt(a), 6));
bitWrite(d3, 5, bitRead(outputString.charAt(a), 7));
myCoffeemaker.write(d0);
delay(1);
myCoffeemaker.write(d1);
delay(1);
myCoffeemaker.write(d2);
delay(1);
myCoffeemaker.write(d3);
delay(7);
}
}
String fromCoffeemaker() {
delay(10);
String inputString = "";
byte d0, d1, d2, d3;
char d4 = 255;
while (myCoffeemaker.available() > 0) {
d0 = myCoffeemaker.read();
delay (1);
d1 = myCoffeemaker.read();
delay (1);
d2 = myCoffeemaker.read();
delay (1);
d3 = myCoffeemaker.read();
delay (7);
bitWrite(d4, 0, bitRead(d0, 2));
bitWrite(d4, 1, bitRead(d0, 5));
bitWrite(d4, 2, bitRead(d1, 2));
bitWrite(d4, 3, bitRead(d1, 5));
bitWrite(d4, 4, bitRead(d2, 2));
bitWrite(d4, 5, bitRead(d2, 5));
bitWrite(d4, 6, bitRead(d3, 2));
bitWrite(d4, 7, bitRead(d3, 5));
inputString += d4;
}
inputString.trim();
if (inputString != "") {
return (inputString);
}
}
String getStatus() {
String input = "";
String onOff = "";
String status = "";
toCoffeemaker(F("RR:03\r\n"));
delay(100);
input = fromCoffeemaker();
onOff = input.substring(4, 5);
status = input.substring(6, 7);
status += input.substring(25, 28);
if (onOff == "0") {
return F("off");
} else if (status == "C045") {
return F("no_tray");
} else if (status == "C404") {
return F("no_water");
} else if (status == "C444") {
return F("no_tray_and_no_water");
} else if (status == "4005") {
return F("on");
} else {
return "Unknown (" + status + " / " + input + ")";
}
}