forked from hellgelino/bezzera-bb005-digital-timer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bezzera-bb005-digital-timer.ino
471 lines (381 loc) · 12.6 KB
/
bezzera-bb005-digital-timer.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
/********************************************************
*
* bezzera-bb005-digital-timer based on ESP8266
*
*****************************************************/
#include <U8g2lib.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
#include <Wire.h>
#include <sstream>
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
//===============================================================
//SSID and Password to connect to your ESP Access Point
//===============================================================
const char* ssid = "Grinder-Configuration";
const char* password = "12345678";
ESP8266WebServer server(80); //Server on port 80
String cup1;
String cup2;
bool servermode = false;
int serverLed = 16;
int clientLed = 2;
//===============================================================
// SETUP
//===============================================================
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
EEPROM.begin(512);
u8g2.begin();
pinMode(serverLed, OUTPUT);
pinMode(clientLed, OUTPUT);
digitalWrite(serverLed, HIGH); // turn the LED off
digitalWrite(clientLed, HIGH); // turn the LED off
pinMode(10, INPUT_PULLUP); // grind
pinMode(12, OUTPUT);
digitalWrite(12, HIGH);
if (digitalRead(10) == LOW) {servermode = true;}
if (servermode){
//===============================================================
// SERVERMODE
//===============================================================
Serial.println("servermode-setup");
WiFi.mode(WIFI_AP); //Only Access point
WiFi.softAP(ssid, password); //Start HOTspot removing password will disable security
IPAddress myIP = WiFi.softAPIP(); //Get IP address
Serial.print("HotSpt IP: ");
Serial.println(myIP);
server.on("/", handleRoot); //Which routine to handle at root location
server.on("/action_page", handleForm); //form action is handled here
server.on("/reset_page", handleReset); //form action is handled here
server.begin(); //Start server
Serial.println("HTTP server started");
digitalWrite(serverLed, LOW); //LED on!
std::string ssid2(ssid);
std::string password2(password);
u8g2.setFont(u8g2_font_crox1h_tf);
u8g2.drawStr(0,10,"Servermode");
u8g2.drawStr(0,23,myIP.toString().c_str());
u8g2.setCursor(0, 36);
u8g2.print("ssid: ");
u8g2.print(ssid2.c_str());
u8g2.setCursor(0, 49);
u8g2.print("password: ");
u8g2.print(password2.c_str());
u8g2.sendBuffer();
} else {
//===============================================================
// CLIENTMODE
//===============================================================
Serial.println("clientmode-setup");
WiFi.mode(WIFI_OFF);
//Read strings from eeprom
cup1=readEprom(0, 100);
cup2=readEprom(100, 200);
greeting();
}
}
//===============================================================
// LOOP
//===============================================================
void loop() {
// put your main code here, to run repeatedly:
if (servermode){
//===============================================================
// SERVERMODE
//===============================================================
server.handleClient(); //Handle client requests
} else {
int number;
if (digitalRead(10) == LOW)
{
u8g2.clearBuffer();
delay(300);
u8g2.setFont(u8g2_font_inb27_mf);
u8g2.drawStr(9,40,"1");
u8g2.setFont(u8g2_font_8x13B_tf);
u8g2.drawStr(9,55,"Cup");
u8g2.sendBuffer();
number=1;
while (digitalRead(10) == LOW){
yield();
}
unsigned long mytime;
mytime = millis();
while (millis()-mytime<1000){
yield();
if (digitalRead(10) == LOW)
{
number=2;
u8g2.setFont(u8g2_font_inb27_mf);
u8g2.drawStr(9,40,"2");
u8g2.setFont(u8g2_font_8x13B_tf);
u8g2.drawStr(9,55,"Cups");
u8g2.sendBuffer();
while (digitalRead(10) == LOW){
yield();
}
delay(1000);
break;
}
}
Serial.print("Number: ");
Serial.println(number);
if (number==1){
grind(cup1.toInt(), 1);
} else if (number==2){
grind(cup2.toInt(), 2);
}
}
else
{
u8g2.setFont(u8g2_font_6x13B_tf);
u8g2.drawStr(0,10,"Coffee Grind Timer");
u8g2.setFont(u8g2_font_6x10_tf);
u8g2.drawStr(100,30,"V1.1");
u8g2.sendBuffer();
u8g2.setCursor(15, 45);
u8g2.print(" cup1: ");
u8g2.print(cup1.c_str());
u8g2.setCursor(15, 55);
u8g2.print(" cup2: ");
u8g2.print(cup2.c_str());
u8g2.sendBuffer();
}
}
}
//===============================================================
// GREETING
//===============================================================
void greeting(){
// Greeting display
u8g2.setFont(u8g2_font_6x13B_tf);
u8g2.drawStr(0,10,"Coffee Grind Timer");
u8g2.setFont(u8g2_font_6x10_tf);
u8g2.drawStr(100,30,"V1.1");
u8g2.sendBuffer();
u8g2.setCursor(15, 45);
u8g2.print(" cup1: ");
u8g2.print(cup1.c_str());
u8g2.setCursor(15, 55);
u8g2.print(" cup2: ");
u8g2.print(cup2.c_str());
u8g2.sendBuffer();
delay(3000);
u8g2.clearBuffer();
u8g2.sendBuffer();
}
//===============================================================
// GRIND
//===============================================================
void grind(int millisec, int cups) {
String zeit=String(float(millisec)/1000);
u8g2.clearBuffer();
u8g2.sendBuffer();
u8g2.setFont(u8g2_font_6x10_tf);
u8g2.setCursor(5, 10);
u8g2.print(zeit.c_str());
u8g2.print(" sec");
u8g2.setFont(u8g2_font_7x13_tf);
u8g2.setCursor(5, 23);
u8g2.print("Cups to grind: ");
u8g2.print((String(cups)).c_str());
u8g2.sendBuffer();
String temp;
unsigned long mytime;
mytime = millis();
while (millis()-mytime<millisec){
yield();
digitalWrite(12, LOW);
float counter=(millis()-mytime)/1000.00;
if (counter<=9.99){
temp=" "+String(counter);
} else {
temp=String(counter);
}
u8g2.setFont(u8g2_font_inb27_mf);
u8g2.drawStr(9,60,temp.c_str());
u8g2.sendBuffer();
//Abort?
if (digitalRead(10) == LOW)
{
doreset();
u8g2.setFont(u8g2_font_8x13B_tf);
u8g2.drawStr(40,40,"Aborted!");
u8g2.sendBuffer();
delay(2500);
u8g2.clearBuffer();
return;
}
}
//Done!
doreset();
u8g2.setFont(u8g2_font_6x13B_tf);
u8g2.drawStr(4,30,"Done...");
u8g2.drawStr(4,55,"Enjoy your coffee!");
u8g2.sendBuffer();
delay(5000);
u8g2.clearBuffer();
}
//===============================================================
// Relay off - Clear Display Text
//===============================================================
void doreset(){
digitalWrite(12, HIGH);
u8g2.clearBuffer();
u8g2.sendBuffer();
}
//===============================================================
// Write string to eeprom
//===============================================================
void writeEprom(String val, int von){
for(int i=von;i<val.length()+von;i++) //loop upto string lenght
{
EEPROM.write(0x0F+i,val[i-von]); //Write one by one with starting address of 0x0F
}
}
//===============================================================
// Read string from eeprom
//===============================================================
String readEprom(int von, int bis){
String val = "";
for(int i=von;i<bis;i++)
{
val = val + char(EEPROM.read(0x0F+i)); //Read one by one with starting address of 0x0F
}
int position = val.indexOf("#|#|");
val = val.substring(0, position);
return val;
}
//==============================================================
// This is the webpage for the Server Mode
//==============================================================
const char MAIN_page[] = R"=====(
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<style>
html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}
body{margin-top: 50px;}
h1 {margin: 50px auto 30px;}
h3 {margin-bottom: 50px;}
p {font-size: 20px;margin-bottom: 10px;}
input {font-size: 20px;}
</style>
</head>
<body>
<h1>Grinder-Configuration</h1>
<h3>Please Setup your Time:</h3>
<form action="/action_page">
<p>All Parameters in Milliseconds!<br>
<br>
<br>
<p>Time for one Cup:<br>
<input type="text" name="cup1" value="6400" size="10" maxlength="5">
<br>
<p>Time for two Cups:<br>
<input type="text" name="cup2" value="9600" size="10" maxlength="5">
<br>
<br>
<input type="submit" value="Safe Data">
</form>
</body>
</html>
)=====";
//==============================================================
// This ist the webpage two for the Server Mode
//==============================================================
const char ACTION_page[] = R"=====(
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<style>
html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}
body{margin-top: 50px;}
h1 {margin: 50px auto 30px;}
h3 {margin-bottom: 50px;}
input {font-size: 20px;}
</style>
</head>
<body>
<h1>Data safed</h1>
<h3>Insert Data again:</h3>
<form action="/">
<input type="submit" value="Back">
</form>
<br>
<br>
<h3>Restart Device with new Data:</h3>
<form action="/reset_page">
<input type="submit" value="Start">
</form>
</body>
</html>
)=====";
//==============================================================
// This ist the webpage after reset for the Server Mode
//==============================================================
const char RESET_page[] = R"=====(
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<style>
html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}
body{margin-top: 50px;}
h1 {margin: 50px auto 30px;}
h3 {margin-bottom: 50px;}
</style>
</head>
<body>
<h1>RESET</h1>
<h3>The Device is restarting know</h3>
</body>
</html>
)=====";
//==============================================================
// This rutine is executed when you open its IP in browser
//==============================================================
void handleRoot() {
server.send_P(200, "text/html", MAIN_page, sizeof(MAIN_page) ); //Send web page
}
//===============================================================
// This routine is executed when you press submit
//===============================================================
void handleForm() {
cup1 = server.arg("cup1") + "#|#|";
cup2 = server.arg("cup2") + "#|#|";
// #|#| Its for the Identifier at the end of the string
EEPROM.begin(512); //Initialize EEPROM
writeEprom(cup1, 0);
writeEprom(cup2, 100);
EEPROM.commit(); //Store data to EEPROM
EEPROM.end();
server.send_P(200, "text/html", ACTION_page, sizeof(ACTION_page) ); //Send web page
}
//==============================================================
// This rutine is executed when you restart
//==============================================================
void handleReset() {
server.send_P(200, "text/html", RESET_page, sizeof(RESET_page) );
Serial.println("Reset...");
//delay(5000);
blink(5, 10, 240, clientLed);
blink(5, 10, 240, serverLed);
ESP.restart(); //Reset esp!
}
//===============================================================
// blink LED
//===============================================================
void blink(int count, int mytime, int mypause, uint32_t colour){
for (int i = 0; i < count; i++){
digitalWrite(colour, LOW); // turn the LED on
delay(mytime); // wait for the given time
digitalWrite(colour, HIGH); // turn the LED off
delay(mypause); // wait for half a second
}
}