-
Notifications
You must be signed in to change notification settings - Fork 1
/
makerspaceleiden-payment-node.ino
463 lines (416 loc) · 12.2 KB
/
makerspaceleiden-payment-node.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
// [email protected], apache license, for the makerspaceleiden.nl
//
// https://sites.google.com/site/jmaathuis/arduino/lilygo-ttgo-t-display-esp32
// or
// https://www.otronic.nl/a-59613972/esp32/esp32-wroom-4mb-devkit-v1-board-met-wifi-bluetooth-en-dual-core-processor/
// with a tft 1.77 arduino 160x128 RGB display
// https://www.arthurwiz.com/software-development/177-inch-tft-lcd-display-with-st7735s-on-arduino-mega-2560
//
// Tools settings:
// Board ESP32 Dev module
// Upload Speed: 921600 (or half that on MacOSX!)
// CPU Frequency: 240Mhz (WiFi/BT)
// Flash Frequency: 80Mhz
// Flash Mode: QIO
// Flash Size: 4MB (32Mb)
// Partition Scheme: Default 4MB with spiffs (1.2MB APP/1.5 SPIFFS)
// Core Debug Level: None
// PSRAM: Disabled
// Port: [the COM port your board has connected to]
//
// Additional Librariries (via Sketch -> library manager):
/// TFT_eSPI
// Button2
// MFRC522-spi-i2c-uart-async
// Arduino_JSON
char terminalName[64];
#include <WiFi.h>
#include <WiFiUdp.h>
#include <HTTPClient.h> // for the HTTP error codes.
#include <ESPmDNS.h>
#include "esp_heap_caps.h"
#include <Button2.h>
#include "TelnetSerialStream.h"
#include "global.h"
#include "selfsign.h"
#include "display.h"
#include "rest.h"
#include "ota.h"
#include "rfid.h"
Button2 * btn1, * btn2;
int update = false;
int NA = 0;
char **amounts = NULL;
char **prices = NULL;
char **descs = NULL;
int amount = 0;
int default_item = -1;
double amount_no_ok_needed = AMOUNT_NO_OK_NEEDED;
const char * version = VERSION;
double paid = 0;
// Hardcode for Europe (ESP32/Espresifs defaults for CEST seem wrong)./
const char * cestTimezone = "CET-1CEST,M3.5.0/2,M10.5.0/3";
// values above this amount will get an extra 'OK' question.
state_t md = BOOT;
board_t BOARD = BOARD_V4;
// Updates the small clock in the top right corner; and
// will reboot the unit early mornings.
//
static void loop_RebootAtMidnight() {
static unsigned long lst = millis();
if (millis() - lst < 60 * 1000)
return;
lst = millis();
static unsigned long debug = 0;
if (millis() - debug > 60 * 60 * 1000) {
debug = millis();
time_t now = time(nullptr);
char * p = ctime(&now);
p[5 + 11 + 3] = 0;
}
time_t now = time(nullptr);
if (now < 3600)
return;
#ifdef AUTO_REBOOT_TIME
static unsigned long reboot_offset = random(3600);
char * q = ctime(&now);
now += reboot_offset;
char * p = ctime(&now);
p += 11;
p[5] = 0;
if (strncmp(p, AUTO_REBOOT_TIME, strlen(AUTO_REBOOT_TIME)) == 0 && millis() > 3600UL) {
Log.printf("Nightly reboot of %s has come - also to fetch new pricelist and fix any memory leaks.\n", q);
yield();
delay(1000);
yield();
ESP.restart();
}
#endif
}
void settupButtons()
{
if (BOARD != BOARD_V2) {
// buttons with pullup; wired to GND
btn1 = new Button2(BUTTON_1, INPUT_PULLUP);
btn2 = new Button2(BUTTON_2, INPUT_PULLUP);
} else {
// buttons wired to VCC.
btn1 = new Button2(BUTTON_1, INPUT, false, false /* active high, normal low */);
btn2 = new Button2(BUTTON_2, INPUT, false, false /* active high, normal low */);
};
btn1->setPressedHandler([](Button2 & b) {
if (md == SCREENSAVER) {
update = true;
return;
};
int l = amount;
if (md == ENTER_AMOUNT && NA)
#ifndef ENDLESS
if (amount + 1 < NA)
#endif
amount = (amount + 1) % NA;
if (md == OK_OR_CANCEL)
md = DID_OK;
update = update || (md != ENTER_AMOUNT) || (l != amount);
if (l != amount)
Debug.println("left");
});
btn2->setPressedHandler([](Button2 & b) {
if (md == SCREENSAVER) {
update = true;
return;
};
int l = amount;
if (md == ENTER_AMOUNT && NA)
#ifndef ENDLESS
if (amount > 0 )
#endif
amount = (amount + NA - 1) % NA;
if (md == OK_OR_CANCEL)
md = DID_CANCEL;
update = update || (md != ENTER_AMOUNT) || (l != amount);
if (l != amount)
Debug.println("right");
});
}
void button_loop()
{
btn1->loop();
btn2->loop();
}
static unsigned char normal_led_brightness = NORMAL_LED_BRIGHTNESS;
#ifdef LED_1
void setupLEDS()
{
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
digitalWrite(LED_1, (BOARD == BOARD_V2) ? HIGH : LOW);
digitalWrite(LED_2, (BOARD == BOARD_V2) ? HIGH : LOW);
delay(50);
ledcSetup(1, 2500, 8);
ledcAttachPin(LED_1, 1);
ledcSetup(2, 2500, 8);
ledcAttachPin(LED_1, 2);
led_loop(md);
}
#else
void setupLEDS() {}
#endif
void led_loop(state_t md) {
#ifdef LED_1
switch (md) {
case ENTER_AMOUNT:
#ifdef ENDLESS
// Visibly dim the buttons at the end of the strip; as current 'wrap around'
// is not endless.
ledcWrite(1, normal_led_brightness);
ledcWrite(2, normal_led_brightness);
#else
ledcWrite(1, amount + 1 == NA ? normal_led_brightness : (BOARD == BOARD_V2) ? HIGH : LOW);
ledcWrite(2, amount == 0 ? normal_led_brightness : (BOARD == BOARD_V2) ? HIGH : LOW);
#endif
break;
case OK_OR_CANCEL:
digitalWrite(LED_1, (BOARD == BOARD_V2) ? HIGH : LOW);
digitalWrite(LED_2, (BOARD == BOARD_V2) ? HIGH : LOW);
break;
default:
// Switch them off - nothing you can do here.
digitalWrite(LED_1, (BOARD == BOARD_V2) ? LOW : HIGH);
digitalWrite(LED_2, (BOARD == BOARD_V2) ? LOW : HIGH);
break;
};
#endif
}
static void setupWiFiConnectionOrReboot() {
while (millis() < 2000) {
delay(100);
if (WiFi.isConnected())
return;
};
// not going well - warn the user and try for a bit,
// while communicating a timeout with a progress bar.
//
updateDisplay_startProgressBar("Waiting for Wifi");
while ( millis() < WIFI_MAX_WAIT) {
if (WiFi.isConnected())
return;
updateDisplay_progressBar((float)millis() / WIFI_MAX_WAIT);
delay(HTTP_CODE_OK);
};
displayForceShowErrorModal((char *)"reboot", "WiFi problem");
delay(2500);
ESP.restart();
}
static board_t detectBoard() {
unsigned char mac[6];
WiFi.macAddress(mac);
// C8:C9:A3:CB:B6:7C - Grijpvoorraad - buttons aan '+'
if (mac[3] == 0xCB && mac[4] == 0xB6 && mac[5] == 0x7C) {
normal_led_brightness = 255;
return BOARD_V2;
};
// 80:7D:3A:D5:46:8C - Voorruimte - buttons aan GND
if (mac[3] == 0xD5 && mac[4] == 0x46 && mac[5] == 0x8C)
return BOARD_V3;
// test board - scherm 180 graden gedraaid.
normal_led_brightness = 255;
return BOARD_V4;
};
static const char * board2name(board_t x) {
switch (x) {
case BOARD_V2: return "v3: buttons VCC, LEDs on LOW";
case BOARD_V3: return "v4: buttons GND, LEDs on HIGH";
case BOARD_V4: return "v5: buttons GND, LEDs on HIGH, Screen flipped";
default: break;
}
return "Dunno";
}
bool isPaired = false;
void setup()
{
const char * p = __FILE__;
if (rindex(p, '/')) p = rindex(p, '/') + 1;
Serial.begin(115200);
byte mac[6];
WiFi.macAddress(mac);
snprintf(terminalName, sizeof(terminalName), "%s-%s-%02x%02x%02x", TERMINAL_NAME, VERSION, mac[3], mac[4], mac[5]);
BOARD = detectBoard();
Serial.printf( "File: %s\n", p);
Serial.println("Version : " VERSION);
Serial.println("Compiled: " __DATE__ " " __TIME__);
Serial.printf( "Revision: %s\n", board2name(BOARD));
Serial.printf( "Heap : %d Kb\n", (512 + heap_caps_get_free_size(MALLOC_CAP_INTERNAL)) / 1024UL);
Serial.print( "MacAddr: ");
Serial.println(WiFi.macAddress());
#ifdef LED_1
setupLEDS();
#endif
setupTFT();
md = BOOT;
updateDisplay(BOOT);
settupButtons();
isPaired = setupAuth(terminalName);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_NETWORK, WIFI_PASSWD);
WiFi.setHostname(terminalName);
setupWiFiConnectionOrReboot();
setupLog();
setupOTA();
// Do this late - so that this information also is visible
// in (remote) logging.
//
if (!setupRFID()) {
displayForceShowErrorModal("RFID", "Scanner not found");
log_loop();
yield();
delay(5000);
ESP.restart();
};
// try to get some reliable time; to stop my cert
// checking code complaining.
//
configTzTime(cestTimezone, NTP_POOL);
Log.printf( "File: %s\n", p);
Log.println("Firmware: " TERMINAL_NAME "-" VERSION);
Log.println("Build: " __DATE__ " " __TIME__ );
Log.print( "Unit: ");
Log.println(terminalName);
Log.printf( "Revision: %s\n", board2name(BOARD));
Log.print( "MacAddr: ");
Log.println(WiFi.macAddress());
Log.print( "IP: ");
Log.println(WiFi.localIP());
Log.printf("Paired: ", isPaired ? "yes" : "no");
Log.printf("Heap: %d Kb\n", (512 + heap_caps_get_free_size(MALLOC_CAP_INTERNAL)) / 1024UL);
Serial.println();
Log.println("Starting loop");
md = WAITING_FOR_NTP;
}
void loop()
{
static unsigned long lastchange = -1;
static state_t laststate = WAITING_FOR_NTP;
static int last_amount = -1;
unsigned int extra_show_delay = 0;
log_loop();
loop_RebootAtMidnight();
ota_loop();
button_loop();
#ifdef LED_1
led_loop(md);
#endif
switch (md) {
case WAITING_FOR_NTP:
if (lastchange == -1)
updateDisplay_progressText("waiting for time");
if (time(nullptr) > 3600)
md = FETCH_CA;
break;
case FETCH_CA:
if (fetchCA())
md = isPaired ? REGISTER_PRICELIST : REGISTER;
break;
case REGISTER:
if (registerDevice())
md = WAIT_FOR_REGISTER_SWIPE;
break;
case WAIT_FOR_REGISTER_SWIPE:
if (loopRFID())
if (registerDeviceWithSwipe(tag)) {
isPaired = true;
md = REGISTER_PRICELIST;
};
break;
case REGISTER_PRICELIST:
{ int httpCode = fetchPricelist();
if (httpCode = HTTP_CODE_OK)
md = ENTER_AMOUNT;
else if (httpCode = HTTP_CODE_BAD_REQUEST)
md = REGISTER; // something gone very wrong server side - simply reset/retry.
break;
};
case SCREENSAVER:
if (update) {
Log.println("Wakeup");
setTFTPower(true);
md = ENTER_AMOUNT;
lastchange = millis(); // prevent jump to default straight after wakeup.
};
break;
case ENTER_AMOUNT:
if (millis() - lastchange > SCREENSAVER_TIMEOUT) {
md = SCREENSAVER;
Log.println("Enabling screensaver (from enter)");
setTFTPower(false);
return;
};
if (default_item >= 0 && millis() - lastchange > DEFAULT_TIMEOUT && amount != default_item && amount == last_amount) {
last_amount = amount = default_item;
lastchange = millis();
Log.printf("Jumping back to default item %d: %s\n", default_item, amounts[default_item]);
update = true;
};
memset(tag, 0, sizeof(tag));
if (NA > 0) {
if (loopRFID()) {
md = (atof(prices[amount]) < AMOUNT_NO_OK_NEEDED) ? DID_OK : OK_OR_CANCEL;
update = true;
};
};
break;
case OK_OR_CANCEL:
// time out handled by generic timeout.
break;
case DID_CANCEL:
displayForceShowErrorModal("abort", "payment cancelled");
md = ENTER_AMOUNT;
memset(tag, 0, sizeof(tag));
break;
case DID_OK:
{
char buff[48], who[PBR_LEN];
displayForceShow("paying", "...");
int rc = payByREST(tag, prices[amount], descs[amount], who);
if (rc != HTTP_CODE_OK) {
snprintf(buff, sizeof(buff), "no payment - %03d", rc);
displayForceShowErrorModal("FAIL", buff);
md = ENTER_AMOUNT;
} else {
displayForceShowModal("PAID", who);
extra_show_delay = 1500;
md = ENTER_AMOUNT;
Log.printf("Paid %.2f\n", atof(prices[amount]));
paid += atof(prices[amount]);
};
memset(tag, 0, sizeof(tag));
};
break;
};
if (md != laststate || last_amount != amount) {
laststate = md;
lastchange = millis();
last_amount = amount;
update = true;
}
// generic time/error out if something takes longer than 1- seconds and we are in a state
// where one does not expect this.
//
if ((millis() - lastchange > 10 * 1000 && md > ENTER_AMOUNT )) {
if (md == OK_OR_CANCEL) {
displayForceShowModal("canceling", NULL);
md = ENTER_AMOUNT;
}
else {
displayForceShowErrorModal("Timeout", NULL);
};
update = true;
};
if (update) {
update = false;
updateDisplay(md);
} else {
updateClock(false);
};
delay(extra_show_delay);
}