forked from bodgery/esp32_doorbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32_doorbot.ino
551 lines (472 loc) · 15.6 KB
/
esp32_doorbot.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
542
543
544
545
546
547
548
549
550
551
/*
Copyright (c) 2021, Timm Murray
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <Tone32.h>
#include <WiFi.h>
#include <Wiegand.h>
#include "config.h"
const char* version = "4";
const char* root_ca = \
"-----BEGIN CERTIFICATE-----\n" \
"MIIDSjCCAjKgAwIBAgIQRK+wgNajJ7qJMDmGLvhAaz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" \
"JDGFoqgCWjBH4d1QB7wCCZAA62RjYJsWvIjJEubSfZGL+T0yjWW06XyxV3bqxbYo\n" \
"Ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ\n" \
"-----END CERTIFICATE-----\n";
const int door_open_sec = 30;
const int reader_led_pin = 35;
const int reader_24_32_switch = 34;
const int reader_buzzer = 32;
const String dump_keys_request = "https://rfid-prod.shop.thebodgery.org/secure/dump_active_tags";
const char* check_key_request = "https://rfid-prod.shop.thebodgery.org/entry/";
// Time to rebuild cache
const unsigned long cache_rebuild_time_ms = 60 * 60 * 1000;
// We're going to store a lot of keys, and this is our main thing, so
// make a lot of room.
const int dict_size = 21600;
DynamicJsonDocument key_cache( dict_size );
unsigned long ms_since_cache = 0;
const unsigned int CACHE_BUILD_TRIES = 3;
// Pins for Wiegand reads. These must be able to handle interrupts. All GPIO
// pins on the ESP32 can handle it, but that may be different on other chips
const int DATA0 = 22;
const int DATA1 = 23;
const int WIEGAND_BIT_LENGTH = Wiegand::LENGTH_ANY;
//const int WIEGAND_BIT_LENGTH = 8;
Wiegand wiegand;
// Door state management
const int door_open_time_ms = 30 * 1000;
bool is_door_open = false;
unsigned long door_opened_at = 0;
const int DOOR_PIN = 13;
// Tones to play when scan is successful or not
const unsigned int success_tone = NOTE_FS2;
const unsigned int fail_tone = NOTE_C4;
const unsigned long tone_time_ms = 2000;
const unsigned long buzzer_channel = 0;
void setup()
{
Serial.begin( 115200 );
init_wifi();
rebuild_cache();
init_wiegand();
pinMode( DOOR_PIN, OUTPUT );
digitalWrite( DOOR_PIN, LOW );
}
void loop()
{
check_wiegand();
check_serial_commands();
check_cache_build_time();
check_door_status();
}
void init_wifi()
{
Serial.print( "Connecting to " );
Serial.print( ssid );
Serial.print( " " );
WiFi.setHostname( hostname );
WiFi.begin( ssid, psk );
while( WiFi.status() != WL_CONNECTED ) {
delay( 1000 );
Serial.print( "." );
Serial.flush();
}
Serial.println( "Connected!" );
Serial.print( "IP: " );
Serial.println( WiFi.localIP() );
Serial.flush();
}
void init_wiegand()
{
Serial.println( "[WIEGAND.INIT] Startup Wiegand" );
Serial.flush();
pinMode( reader_24_32_switch, OUTPUT );
digitalWrite( reader_24_32_switch, LOW );
pinMode( reader_led_pin, OUTPUT );
digitalWrite( reader_led_pin, LOW );
pinMode( reader_buzzer, OUTPUT );
digitalWrite( reader_buzzer, LOW );
pinMode( DATA0, INPUT );
pinMode( DATA1, INPUT );
wiegand.onReceive( wiegand_receive, "[WIEGAND] Card read: " );
wiegand.onReceiveError( wiegand_error, "[WIEGAND] Card read error: " );
wiegand.onStateChange( wiegand_state_change, "[WIEGAND] State changed: " );
wiegand.begin( WIEGAND_BIT_LENGTH, true );
Serial.println( "[WIEGAND.INIT] Wiegand has started" );
Serial.flush();
}
void check_serial_commands()
{
if( Serial.available() ) {
String cmd = Serial.readString();
cmd.trim();
handle_serial_command( cmd );
}
}
void handle_serial_command( String cmd )
{
if( cmd.equals( "open" ) ) {
Serial.println( "[CMD] Opening door by serial command" );
Serial.flush();
open_door();
}
else if( cmd.equals( "stats" ) ) {
Serial.print( "[STATS] Bodgery Doorbot v" );
Serial.println( version );
Serial.print( "[STATS] IP: " );
Serial.println( WiFi.localIP() );
Serial.print( "[STATS] Hostname: " );
Serial.println( hostname );
Serial.print( "[STATS] MAC Address: " );
Serial.println( WiFi.macAddress() );
Serial.print( "[STATS] Dump keys URL: " );
Serial.println( dump_keys_request );
Serial.print( "[STATS] Check keys URL: " );
Serial.println( check_key_request );
Serial.print( "[STATS] Keys in cache: " );
Serial.println( key_cache.size() );
}
else if( cmd.equals( "newcache" ) ) {
Serial.println( "[CMD] Manually starting cache rebuild" );
rebuild_cache();
}
else if( cmd.startsWith( "check" ) ) {
if( int space_index = cmd.indexOf( ' ' ) ) {
String fob_id = cmd.substring( space_index + 1 );
Serial.print( "[CHECK.CMD] Checking fob ID <" );
Serial.print( fob_id );
Serial.println( ">" );
Serial.flush();
bool found_tag = key_cache.containsKey( fob_id );
Serial.print( "[CHECK.CMD] Tag in cache: " );
Serial.println( found_tag );
Serial.flush();
bool is_remote = check_tag_remote( fob_id );
Serial.print( "[CHECK.CMD] Tag on remote server: " );
Serial.println( is_remote );
Serial.flush();
}
else {
Serial.println( "[CMD] 'check' command needs a parameter" );
Serial.flush();
}
}
else if( cmd.equals( "help" ) ) {
Serial.println( "[HELP] Commands:" );
Serial.println( "[HELP] check <ID> - Check if a keyfob is valid" );
Serial.println( "[HELP] newcache - Rebuild the cache" );
Serial.println( "[HELP] open - Open the door" );
Serial.println( "[HELP] stats - Dump info about this doorbot" );
}
else {
Serial.print( "[CMD] Unrecognized command: {" );
Serial.print( cmd );
Serial.println( "}" );
Serial.flush();
}
}
void check_wiegand()
{
wiegand.flush();
wiegand.setPin0State( digitalRead( DATA0 ) );
wiegand.setPin1State( digitalRead( DATA1 ) );
}
void check_tag( String tag )
{
Serial.print( "[CHECK] Checking tag: " );
Serial.println( tag );
bool found_tag = key_cache.containsKey( tag );
Serial.print( "[CHECK] Tag in cache: " );
Serial.println( found_tag );
Serial.flush();
if( found_tag ) {
Serial.println( "[CHECK.CACHE] Tag valid in local cache" );
Serial.flush();
do_success();
log_tag_remote( tag );
}
else if( check_tag_remote( tag ) ) {
do_success();
}
else {
Serial.println( "[CHECK] Tag is not valid" );
Serial.flush();
do_fail();
}
}
bool check_tag_remote( String tag )
{
Serial.print( "[CHECK.REMOTE] Checking if tag is valid: " );
Serial.println( tag );
Serial.flush();
HTTPClient http;
String request = "";
request.concat( check_key_request );
request.concat( tag );
request.concat( "/" );
request.concat( location );
Serial.print( "[CHECK.REMOTE] Sending request to: " );
Serial.println( request );
Serial.flush();
http.begin( request.c_str(), root_ca );
http.setAuthorization( auth_user, auth_passwd );
int status = http.GET();
bool result = false;
if( HTTP_CODE_OK == status ) {
Serial.println( "[CHECK.REMOTE] Key is OK" );
Serial.flush();
result = true;
}
else {
Serial.print( "[CHECK.REMOTE] Could not verify key: " );
Serial.println( status );
Serial.flush();
}
http.end();
return result;
}
bool log_tag_remote( String tag )
{
Serial.println( "[ENTRY.LOG] Logging entry" );
Serial.flush();
check_tag_remote( tag );
return true;
}
void open_door()
{
Serial.println( "[DOOR] Instructed to open" );
Serial.flush();
if( is_door_open ) {
Serial.println( "[DOOR] Door already open, ignoring" );
Serial.flush();
}
else {
door_opened_at = millis();
is_door_open = true;
digitalWrite( DOOR_PIN, HIGH );
Serial.println( "[DOOR] Opening door" );
Serial.flush();
}
}
void close_door()
{
Serial.println( "[DOOR] Closing door" );
Serial.flush();
is_door_open = false;
digitalWrite( DOOR_PIN, LOW );
}
void check_door_status()
{
if( is_door_open
&& ( millis() >= door_open_time_ms + door_opened_at )
) {
Serial.println( "[DOOR] Door open time has elapsed, closing" );
close_door();
}
}
void do_success()
{
open_door();
play_success_tone();
}
void do_fail()
{
play_fail_tone();
}
void play_success_tone()
{
Serial.println( "[TONE.SUCCESS] Playing" );
play_tone( success_tone );
}
void play_fail_tone()
{
Serial.println( "[TONE.FAIL] Playing" );
play_tone( fail_tone );
}
void play_tone( unsigned int note )
{
Serial.println( "[TONE] Playing tone" );
Serial.flush();
// Disabled. This seems to block while it does its thing, which is bad.
//
//tone( reader_buzzer, success_tone, tone_time_ms, buzzer_channel );
}
void wiegand_pin_state_change()
{
wiegand.setPin0State( digitalRead( DATA0 ) );
wiegand.setPin1State( digitalRead( DATA1 ) );
}
void wiegand_state_change( bool plugged, const char* message )
{
Serial.print(message);
Serial.println(plugged ? "CONNECTED" : "DISCONNECTED");
Serial.flush();
}
void wiegand_receive(
uint8_t* data
,uint8_t bits
,const char* message
)
{
uint8_t bytes = (bits+7)/8;
Serial.print(message);
Serial.print( "{ length: " );
Serial.print( bytes );
Serial.print( " } { bits: " );
Serial.print( bits );
Serial.print( " }" );
Serial.flush();
unsigned long long_data = 0;
for( int i = 0; i < bytes; i++ ) {
long_data <<= 8;
long_data |= data[i];
}
String str_data_no_prefix = String( long_data );
String str_data = "";
// Original database stores numbers with 0's prefix padding to a length of
// 10, so correct for this
while( (str_data_no_prefix.length() + str_data.length()) < 10 ) {
str_data.concat( "0" );
}
str_data.concat( str_data_no_prefix );
Serial.print( " { formatted: " );
Serial.print( str_data );
Serial.println( " }" );
Serial.flush();
check_tag( str_data );
}
void wiegand_error(
Wiegand::DataError error
,uint8_t* rawData
,uint8_t rawBits
,const char* message
)
{
Serial.print( "[WIEGAND.ERROR] " );
Serial.print(message);
Serial.print(Wiegand::DataErrorStr(error));
Serial.print(" - Raw data: ");
Serial.print(rawBits);
Serial.print("bits / ");
//Print value in HEX
uint8_t bytes = (rawBits+7)/8;
for( int i = 0; i < bytes; i++ ) {
Serial.print(rawData[i] >> 4, 16);
Serial.print(rawData[i] & 0xF, 16);
}
Serial.println();
Serial.flush();
}
void check_cache_build_time()
{
unsigned long ms_since = millis() - ms_since_cache;
if( cache_rebuild_time_ms <= ms_since ) {
Serial.print( "[CACHE] " );
Serial.print( ms_since );
Serial.println( "ms has passed since cache built, rebuilding" );
rebuild_cache();
}
}
void rebuild_cache()
{
unsigned long mem_before = ESP.getFreeHeap();
Serial.print( "[CACHE] Heap free before rebuilding cache: " );
Serial.println( mem_before );
rebuild_cache( CACHE_BUILD_TRIES );
unsigned long mem_after = ESP.getFreeHeap();
long extra_usage = mem_before - mem_after;
Serial.print( "[CACHE] Heap free after rebuilding cache: " );
Serial.print( mem_after );
Serial.print( " (" );
Serial.print( extra_usage );
Serial.println( " more bytes)" );
}
void rebuild_cache(
unsigned int tries_left
)
{
if( 0 == tries_left ) {
// Give up. Have to set time since cache here, or else it will try
// to run the cache rebuild again on the next loop.
ms_since_cache = millis();
return;
}
HTTPClient http;
Serial.println( "[CACHE] Rebuild cached keys" );
http.begin( dump_keys_request, root_ca );
http.addHeader( "Accept", "application/json" );
http.setAuthorization( auth_user, auth_passwd );
int status = http.GET();
if( HTTP_CODE_OK == status ) {
Serial.println( "[CACHE] Fetched new key database" );
Serial.flush();
String body = http.getString();
int len = body.length();
Serial.print( "[CACHE] Received " );
Serial.print( len );
Serial.println( " bytes" );
Serial.println( "[CACHE] Rebuilding dictionary" );
Serial.flush();
key_cache.clear();
DeserializationError error = deserializeJson( key_cache, body );
if( error ) {
Serial.print( "[CACHE] Deserialization error: " );
Serial.println( error.f_str() );
rebuild_cache( tries_left - 1 );
return;
}
Serial.print( "[CACHE] Processed " );
Serial.print( key_cache.size() );
Serial.println( " keys" );
Serial.flush();
// Reset time since rebuild
ms_since_cache = millis();
Serial.println( "[CACHE] Cache rebuild successful" );
Serial.flush();
}
else {
Serial.print( "[CACHE] Error fetching new key database: " );
Serial.println( status );
Serial.print( "[CACHE] HTTP status: " );
Serial.println( http.getString() );
Serial.print( "[CACHE] Trying cache rebuild again, tries left: " );
Serial.println( tries_left - 1 );
Serial.flush();
rebuild_cache( tries_left - 1 );
}
}