-
Notifications
You must be signed in to change notification settings - Fork 2
/
smartlock.ino
538 lines (484 loc) · 11.9 KB
/
smartlock.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
#include "font.h"
#include "set.h"
void key_init()
{
pinMode(UP, INPUT_PULLUP);
pinMode(DOWN, INPUT_PULLUP);
pinMode(LEFT, INPUT_PULLUP);
pinMode(RIGHT, INPUT_PULLUP);
}
void Mg966r()
{
myServo.attach(18);//舵机接口
myServo.write(180);
myServo.write(0);
Blinker.delay(5000);
myServo.write(180);
Blinker.delay(400);
myServo.detach();
}
bool oState = false;
void Menu() //主页
{
int hours = timeClient.getHours();
int minu = timeClient.getMinutes();
float t = dht.readTemperature();
float h = dht.readHumidity();
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_gb16st_t_2);
u8g2.setCursor(40, 10);
u8g2.print("Indoor");
u8g2.setFont(u8g2_font_helvB14_tf);
u8g2.drawXBMP(27, 18, 17, 16, wendu);
u8g2.setCursor(43, 34);
u8g2.print(t);
u8g2.drawCircle(91, 22, 2, U8G2_DRAW_ALL); //圆
u8g2.setCursor(92, 35);
u8g2.print("C");
u8g2.drawXBMP(30, 33, 12, 16, WATER);
u8g2.setCursor(43, 50);
u8g2.print(h);
u8g2.print("%");
u8g2.drawHLine(0, 51, 128); //线
u8g2.setFont(u8g2_font_t0_16b_mn);
u8g2.setCursor(0, 64);
u8g2.print(hours);
u8g2.print(":");
if (minu < 10) {
u8g2.print("0");
u8g2.print(minu);
}
else {
u8g2.print(minu);
}
u8g2.setFont(u8g2_font_wqy12_t_gb2312);
u8g2.setCursor(40, 62);
u8g2.print("语音开门");
} while (u8g2.nextPage());
}
//APP
#define BUTTON_1 "ButtonKey"
BlinkerButton Button1(BUTTON_1); //点灯按键
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_BUTTON_TAP) {
BLINKER_LOG("Button tap!");
Button1.color("#FFFFFF");
Button1.text("开门");
Button1.print();
myServo.attach(18); //D4
myServo.write(0);
Blinker.delay(5000);
myServo.write(180);
Blinker.delay(400);
myServo.detach();
}
else {
BLINKER_LOG("Get user setting: ", state);
Button1.icon("icon_1");
Button1.color("#FFFFFF");
Button1.text("Your button name or describe");
// Button1.text("Your button name", "describe");
Button1.print();
}
}
void miotPowerState(const String & state) //点灯电源
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
myServo.attach(18); //D4
myServo.write(180);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
oState = true;
myServo.write(0);
Blinker.delay(5000);
myServo.write(180);
Blinker.delay(400);
myServo.detach();
oState = false;
}
else if (state == BLINKER_CMD_OFF) {
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
oState = false;
}
}
void miotQuery(int32_t queryCode) //状态回调
{
BLINKER_LOG("MIOT Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("MIOT Query All");
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("MIOT Query Power State");
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
}
}
void dataRead(const String & data) //点灯数据保存
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
}
void Add_FR() //添加指纹
{
int i, ensure, processnum = 0;
int ID_NUM = 0;
char str2[10];
while (1)
{
switch (processnum)
{
case 0:
i++;
u8g2.firstPage();
do
{
u8g2.drawXBMP(32, 24, 64, 16, State5); /* 字串 请按手指 64x16 */
}
while (u8g2.nextPage());
ensure = finger.getImage();
if (ensure == FINGERPRINT_OK)
{
ensure = finger.image2Tz(1); //生成特征
if (ensure == FINGERPRINT_OK)
{
u8g2.firstPage();
do
{
u8g2.drawXBMP(32, 24, 64, 16, State6); // 字串 指纹正常 64x16
}
while (u8g2.nextPage());
// Serial.println(" 000 is true");
i = 0;
processnum = 1; //跳到第二步
}
else {};
}
else {};
break;
case 1:
i++;
u8g2.firstPage();
do
{
u8g2.drawXBMP(32, 24, 64, 16, State7); //字串 再按一次 64x16
}
while (u8g2.nextPage());
ensure = finger.getImage();
if (ensure == FINGERPRINT_OK)
{
ensure = finger.image2Tz(2); //生成特征
if (ensure == FINGERPRINT_OK)
{
u8g2.firstPage();
do
{
u8g2.drawXBMP(32, 24, 64, 16, State6); // 字串 指纹正常 64x16
}
while (u8g2.nextPage());
i = 0;
processnum = 2; //跳到第三步
}
else {};
}
else {};
break;
case 2:
u8g2.firstPage();
do
{
u8g2.drawXBMP(32, 24, 64, 16, State8); // 字串 创建模板 64x16
}
while (u8g2.nextPage());
ensure = finger.createModel();
if (ensure == FINGERPRINT_OK)
{
u8g2.firstPage();
do
{
u8g2.drawXBMP(16, 24, 96, 16, State9); // 字串 模板创建成功 96x16
}
while (u8g2.nextPage());
processnum = 3; //跳到第四步
}
else
{
u8g2.firstPage();
do
{
u8g2.drawXBMP(16, 24, 96, 16, State10); // 字串 模板创建失败 96x16
}
while (u8g2.nextPage());
i = 0;
processnum = 0; //跳回第一步
}
delay(500);
break;
case 3:
u8g2.firstPage();
do
{
u8g2.drawXBMP(1, 0, 128, 48, State11);
// 字串 按K4加,按K2减 按K3保存 0=< ID <=99 128x48
u8g2.setFont(u8g2_font_gb16st_t_2); // 选择字体
u8g2.drawStr(40, 62, "ID=00");
}
while (u8g2.nextPage());
while (digitalRead(RIGHT) != 0)
{
if (digitalRead(DOWN) == 0)
{
//key_num = 0;
if (ID_NUM > 0)
ID_NUM--;
if (ID_NUM < 10)
sprintf(str2, "ID=0%d", ID_NUM);
else
sprintf(str2, "ID=%d", ID_NUM);
u8g2.firstPage();
do
{
u8g2.setFont(u8g2_font_gb16st_t_2); // 选择字体
u8g2.drawXBMP(1, 0, 128, 48, State11);
u8g2.drawStr(40, 62, str2);
}
while (u8g2.nextPage());
}
if (digitalRead(UP) == 0)
{
if (ID_NUM < 99)
ID_NUM++;
if (ID_NUM < 10)
sprintf(str2, "ID=0%d", ID_NUM);
else
sprintf(str2, "ID=%d", ID_NUM);
u8g2.firstPage();
do
{
u8g2.setFont(u8g2_font_gb16st_t_2);
u8g2.drawStr(40, 62, str2);
u8g2.drawXBMP(1, 0, 128, 48, State11);
}
while (u8g2.nextPage());
}
}
ensure = finger.storeModel(ID_NUM); //储存模板
if (ensure == 0x00)
{
u8g2.firstPage();
do
{
u8g2.drawXBMP(16, 24, 96, 16, State12); /* 字串 录入指纹成功 96x16 */
}
while (u8g2.nextPage());
Serial.println("FR receive OK");
delay(1500);
return ;
}
else
{
processnum = 0;
}
break;
}
delay(400);
if (i == 10) //超过5次没有按手指则退出
{
break;
}
}
}
void Del_FR() //删除指纹
{
int ensure;
int ID_NUM = 0;
char str2[10];
sprintf(str2, "ID=0%d", ID_NUM);
u8g2.firstPage();
do
{
u8g2.setFont(u8g2_font_gb16st_t_2);
u8g2.drawStr(44, 62, str2);
u8g2.drawXBMP(1, 0, 128, 48, State15); //显示字模汉字
}
while (u8g2.nextPage());
while (digitalRead(RIGHT) != 0)
{
if (digitalRead(DOWN) == 0)
{
//key_num = 0;
if (ID_NUM > 0)
ID_NUM--;
if (ID_NUM < 10)
sprintf(str2, "ID=0%d", ID_NUM);
else
sprintf(str2, "ID=%d", ID_NUM);
u8g2.firstPage();
do
{
u8g2.drawStr(44, 62, str2);
}
while (u8g2.nextPage());
}
if (digitalRead(UP) == 0)
{
if (ID_NUM < 99)
ID_NUM++;
if (ID_NUM < 10)
sprintf(str2, "ID=0%d", ID_NUM);
else
sprintf(str2, "ID=%d", ID_NUM);
u8g2.firstPage();
do
{
u8g2.drawStr(44, 62, str2);
}
while (u8g2.nextPage());
}
if (digitalRead(LEFT) == 0) {
return ;
}
if (digitalRead(UP) == 0 && digitalRead(DOWN) == 0)
{
ensure = finger.emptyDatabase(); //清空指纹库
if (ensure == 0)
{
u8g2.firstPage();
do
{
u8g2.drawXBMP(8, 16, 80, 16, State17); //显示字模汉字
u8g2.drawXBMP(88, 16, 32, 16, State19); //显示字模汉字
}
while (u8g2.nextPage());
}
else
{
u8g2.firstPage();
do
{
u8g2.drawXBMP(8, 16, 80, 16, State17); //显示字模汉字
u8g2.drawXBMP(88, 16, 32, 16, State20); //显示字模汉字
}
while (u8g2.nextPage());
}
delay(1500);
return ;
}
}
ensure = finger.deleteModel(ID_NUM); //删除单个指纹
if (ensure == 0)
{
u8g2.firstPage();
do
{
u8g2.drawXBMP(16, 16, 64, 16, State18); //显示字模汉字
u8g2.drawXBMP(80, 16, 32, 16, State19); //显示字模汉字
}
while (u8g2.nextPage());
}
else
{
u8g2.firstPage();
do
{
u8g2.drawXBMP(16, 16, 64, 16, State18); //显示字模汉字
u8g2.drawXBMP(80, 16, 32, 16, State20); //显示字模汉字
}
while (u8g2.nextPage());
}
delay(1500);
}
void setup()
{
key_init();
dht.begin();
myServo.attach(18);
myServo.write(0);
myServo.detach();
Serial.begin(115200);
u8g2.begin();
finger.begin(57600);
Blinker.begin(auth, ssid, pswd);
timeClient.begin();
Menu();
Blinker.attachData(dataRead);
Button1.attach(button1_callback);//APP
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
u8g2.enableUTF8Print();
}
void loop()
{
Blinker.run();
timeClient.update();
Menu();
if (digitalRead(UP) == 0) //按下K1键 调用Add_FR()
{
Add_FR();
}
if (digitalRead(DOWN) == 0) //按下K2键 调用Del_FR();
{
Del_FR();
}
int ensure, i; // 验证指纹并开锁 函数
char str[20];
char cishu[5];
u8g2.firstPage();
ensure = finger.getImage();
if (ensure == 0x00) //获取图像成功
{
ensure = finger.image2Tz();
if (ensure == 0x00) //生成特征成功
{
ensure = finger.fingerFastSearch();
if (ensure == 0x00) //搜索成功
{
Mg966r();
q++;
sprintf(str, "ID: % d Score: % d", finger.fingerID, finger.confidence);
sprintf(cishu, " % d", q);
u8g2.firstPage();
do
{
u8g2.setFont(u8g2_font_gb16st_t_2); // 选择字体
u8g2.drawXBMP(16, 16, 96, 16, State13); //显示指纹搜索成功
u8g2.drawStr(100, 16, cishu);
u8g2.drawStr(1, 46, str);
}
while (u8g2.nextPage());
//Serial.println("OPEN");
delay(1500);
}
else
{
u8g2.firstPage();
do
{
u8g2.drawXBMP(16, 16, 96, 16, State14); //State14_字串 未搜索到指纹 96x16
}
while (u8g2.nextPage());
delay(1500);
}
}
}
else
{
//ShowErrMessage(ensure);
}
delay(200);
}