· 2 years ago · Oct 07, 2023, 09:50 PM
1substitutions:
2 esp_devicename: Pad OLED Allarme
3 deviceidname: pad-oled-allarme
4
5esphome:
6 name: pad-oled-allarme
7 friendly_name: Pad OLED Allarme
8
9esp32:
10 board: esp32dev
11 framework:
12 type: arduino
13
14captive_portal:
15
16logger:
17 baud_rate: 0
18
19ota:
20 password: "24245800"
21
22web_server:
23 port: 80
24
25wifi:
26 ssid: "LAN SOLO"
27 password: !secret wifi_password
28 manual_ip:
29 static_ip: 192.168.178.144
30 gateway: 192.168.178.1
31 subnet: 255.255.255.0
32
33 ap:
34 ssid: "${esp_devicename} Fallback AP"
35 password: !secret wifi_password
36
37api:
38 encryption:
39 key: "2vY7511Y3laolOH/mo9Qx9I="
40 services:
41 - service: enroll
42 variables:
43 finger_id: int
44 num_scans: int
45 then:
46 - fingerprint_grow.enroll:
47 finger_id: !lambda 'return finger_id;'
48 num_scans: !lambda 'return num_scans;'
49 - service: cancel_enroll
50 then:
51 - fingerprint_grow.cancel_enroll:
52 - service: delete
53 variables:
54 finger_id: int
55 then:
56 - fingerprint_grow.delete:
57 finger_id: !lambda 'return finger_id;'
58 - service: delete_all
59 then:
60 - fingerprint_grow.delete_all:
61
62################################################################################
63## FONT
64################################################
65font:
66 - file: 'arial.ttf'
67 id: font1
68 size: 18
69
70 - file: 'arial_bold.ttf'
71 id: font2
72 size: 24
73
74 - file: 'arial.ttf'
75 id: font3
76 size: 36
77
78 - file: 'arial.ttf'
79 id: font4
80 size: 28
81
82image:
83 - file: mdi:shield-lock-outline
84 id: alert
85 resize: 25x25
86
87 - file: mdi:shield-home
88 id: alert_off
89 resize: 75x75
90
91 - file: mdi:thumb-up
92 id: ok
93 resize: 30x30
94
95################################################################################
96## I2C
97################################################
98i2c:
99 sda: GPIO21
100 scl: GPIO22
101
102################################################################################
103## UART
104################################################
105uart:
106 rx_pin: GPIO17
107 tx_pin: GPIO16
108 baud_rate: 57600
109
110################################################################################
111## FINGERPRINT
112################################################
113fingerprint_grow:
114 sensing_pin: GPIO12
115 on_finger_scan_matched:
116 - if:
117 condition:
118 lambda: 'return confidence > 89;'
119 then:
120 - homeassistant.tag_scanned: !lambda |-
121 switch (finger_id) {
122 case 1:
123 return "Francesco";
124 case 2:
125 return "Simona";
126 case 3:
127 return "Gaia";
128 default:
129 return "Sconosciuto";
130 }
131
132 - display.page.show: page3
133
134 - text_sensor.template.publish:
135 id: fingerprint_state
136 state: !lambda |-
137 switch (finger_id) {
138 case 1:
139 return "Francesco";
140 case 2:
141 return "Simona";
142 default:
143 return "Autorizzato";
144 }
145
146 - fingerprint_grow.aura_led_control:
147 state: BREATHING
148 speed: 40
149 color: GREEN
150 count: 2
151
152 - rtttl.play: 'two_short:d=32,o=8,b=100:32e6,40e6'
153 - delay: 1000ms
154
155 - text_sensor.template.publish:
156 id: fingerprint_state
157 state: "Attesa"
158
159 - display.page.show: page1
160
161 - if:
162 condition:
163 lambda: 'return confidence < 90;'
164 then:
165 - display.page.show: page5
166
167 - fingerprint_grow.aura_led_control:
168 state: FLASHING
169 speed: 30
170 color: PURPLE
171 count: 4
172
173 - rtttl.play: 'two_short:d=32,o=8,b=100:32e6'
174 - delay: 1000ms
175
176 - text_sensor.template.publish:
177 id: fingerprint_state
178 state: "Attesa"
179
180 - display.page.show: page1
181
182
183 on_finger_scan_unmatched:
184 - text_sensor.template.publish:
185 id: fingerprint_state
186 state: "Impronta non autorizzata"
187
188 - fingerprint_grow.aura_led_control:
189 state: FLASHING
190 speed: 25
191 color: RED
192 count: 3
193
194 - switch.turn_on: buzzer_error
195 - delay: 500ms
196 - switch.turn_off: buzzer_error
197
198 on_enrollment_scan:
199 - text_sensor.template.publish:
200 id: fingerprint_state
201 state: "Impronta acquisita"
202
203 - fingerprint_grow.aura_led_control:
204 state: FLASHING
205 speed: 25
206 color: WHITE
207 count: 2
208 - fingerprint_grow.aura_led_control:
209 state: ALWAYS_ON
210 speed: 0
211 color: YELLOW
212 count: 0
213
214 on_enrollment_failed:
215 - text_sensor.template.publish:
216 id: fingerprint_state
217 state: "Acquisizione impronta fallita"
218 - fingerprint_grow.aura_led_control:
219 state: FLASHING
220 speed: 25
221 color: RED
222 count: 4
223
224################################################################################
225## PAD
226################################################
227matrix_keypad:
228 id: mykeypad
229 rows:
230 - pin: GPIO19
231 - pin: GPIO33
232 - pin: GPIO25
233 - pin: GPIO26
234 columns:
235 - pin: GPIO27
236 - pin: GPIO13
237 - pin: GPIO5
238 keys: "123456789*0#"
239 has_diodes: false
240
241################################################################################
242## Key Collector
243################################################
244key_collector:
245 - id: pincode_reader
246 source_id: mykeypad
247 min_length: 1
248 max_length: 4
249 back_keys: "*"
250 end_key_required: false
251 clear_keys: "#"
252 allowed_keys: "0124578"
253 timeout: 4s
254 on_progress:
255 - logger.log:
256 format: "input progress: '%s', started by '%c'"
257 args: [ 'x.c_str()', "(start == 0 ? '~' : start)" ]
258
259 - text_sensor.template.publish:
260 id: keypadp
261 state: !lambda "return x.c_str();"
262
263 on_result:
264 then:
265
266 - logger.log:
267 format: "input result: '%s', started by '%c', ended by '%c''x.c_str()'"
268 args: [ 'x.c_str()', "(start == 0 ? '~' : start)", "(end == 0 ? '~' : end)" ]
269
270 - text_sensor.template.publish:
271 id: keypad
272 state: !lambda "return x.c_str();"
273
274 - delay: 2s
275
276 - text_sensor.template.publish:
277 id: keypad
278 state: ""
279
280 - id: pincode_reader2
281 source_id: mykeypad
282 min_length: 1
283 max_length: 4
284 back_keys: "*"
285 end_key_required: false
286 clear_keys: "#"
287 allowed_keys: "0124578"
288 timeout: 4s
289
290 on_progress:
291 - script.stop: timer
292 - script.execute: timer
293
294 - if:
295 condition:
296 lambda: 'return id(page).state != "1";'
297 then:
298 - text_sensor.template.publish:
299 id: page
300 state: "2"
301 - display.page.show: page2
302
303 - text_sensor.template.publish:
304 id: page
305 state: "2"
306
307 - switch.turn_on: buzzer
308 - delay: 100ms
309 - switch.turn_off: buzzer
310
311 - logger.log:
312 format: "input progress: '%s', started by '%c'"
313 args: [ 'x.c_str()', "(start == 0 ? '~' : start)" ]
314
315 - text_sensor.template.publish:
316 id: kpad
317 state: !lambda "return std::string(x.length(), 'x').c_str();"
318
319 on_result:
320
321 - script.stop: timer
322 - text_sensor.template.publish:
323 id: kpad
324 state: ""
325
326 - if:
327 condition:
328 lambda: 'return id(allarme_scattato).state == "on";'
329 then:
330 - display.page.show: page4
331 else:
332 - display.page.show: page1
333
334 - text_sensor.template.publish:
335 id: page
336 state: "1"
337
338 on_timeout:
339 - script.stop: timer
340 - logger.log:
341 format: "input timeout: '%s', started by '%c'"
342 args: [ 'x.c_str()', "(start == 0 ? '~' : start)" ]
343
344 - text_sensor.template.publish:
345 id: kpad
346 state: !lambda "return x.c_str();"
347
348 - if:
349 condition:
350 lambda: 'return id(allarme_scattato).state == "on";'
351 then:
352 - display.page.show: page4
353 else:
354 - display.page.show: page1
355
356 - text_sensor.template.publish:
357 id: page
358 state: "1"
359
360################################################################################
361## Script
362################################################
363script:
364 id: timer
365 then:
366 - delay: 4s
367 - if:
368 condition:
369 lambda: 'return id(allarme_scattato).state == "on";'
370 then:
371 - display.page.show: page4
372 else:
373 - display.page.show: page1
374
375################################################################################
376## Display
377################################################
378display:
379 - platform: ssd1306_i2c
380 model: "SSD1306 128x64"
381 reset_pin: GPIO23
382 address: 0x3C
383 id: my_display
384 pages:
385 - id: page1
386 lambda: |-
387 it.clear();
388 it.printf(64, 3, id(font1), TextAlign::TOP_CENTER, "Allarme");
389 it.image(2, 2, id(alert));
390 it.line(1, 0, 124, 0);
391 it.line(1, 63, 124, 63);
392 it.line(0, 1, 0, 62);
393 it.line(125, 1, 125, 62);
394
395 if (id(alarm_state).state.length() == 0) {
396 it.printf(64, 24, id(font2), TextAlign::TOP_CENTER, "Starting");
397 } else if (id(alarm_state).state == "Attivo") {
398 it.printf(64, 24, id(font2), TextAlign::TOP_CENTER, id(alarm_state).state.c_str());
399 } else if (id(alarm_state).state == "...Avvio") {
400 it.printf(64, 24, id(font2), TextAlign::TOP_CENTER, id(alarm_state).state.c_str());
401 } else if (id(alarm_state).state == "Disattivato") {
402 it.clear();
403 } else {
404 it.clear();
405 }
406
407 - id: page2
408 lambda: |-
409 it.printf(64, 10, id(font3), TextAlign::TOP_CENTER, id(kpad).state.c_str());
410
411 - id: page3
412 lambda: |-
413 it.image(50, 2, id(ok));
414 it.printf(64, 30, id(font4), TextAlign::TOP_CENTER, id(fingerprint_state).state.c_str());
415
416 - id: page4
417 lambda: |-
418 it.clear();
419 it.printf(64, 3, id(font1), TextAlign::TOP_CENTER, "Allarme");
420
421 static bool flashText = false;
422 flashText = !flashText;
423 if (flashText) {
424 it.printf(64, 24, id(font2), TextAlign::TOP_CENTER, "Scattato");
425 }
426
427 it.image(2, 2, id(alert));
428 it.line(1, 0, 124, 0);
429 it.line(1, 63, 124, 63);
430 it.line(0, 1, 0, 62);
431 it.line(125, 1, 125, 62);
432
433 - id: page5
434 lambda: |-
435 it.printf(64, 3, id(font1), TextAlign::TOP_CENTER, "Poggia");
436 it.printf(64, 30, id(font4), TextAlign::TOP_CENTER, "Meglio");
437
438################################################################################
439## Text Sensor
440################################################
441text_sensor:
442 - platform: homeassistant
443 entity_id: sensor.stato_allarme_pad_oled
444 name: "Stato Allarme"
445 id: alarm_state
446
447 - platform: homeassistant
448 entity_id: sensor.stato_allarme
449 name: "Stato Allarme"
450 id: allarme
451
452 - platform: homeassistant
453 name: "Allarme Scattato"
454 entity_id: input_boolean.allarme_scattato
455 id: allarme_scattato
456 internal: true
457
458 - platform: homeassistant
459 name: "input"
460 id: input
461 entity_id: sensor.pad_oled_allarme_keypad_code
462 internal: true
463
464 - platform: homeassistant
465 name: "error"
466 id: error
467 entity_id: sensor.counter_code_error
468 internal: true
469
470 - platform: wifi_info
471 ip_address:
472 name: "${esp_devicename} IP"
473 icon: "mdi:lan"
474
475 - platform: template
476 name: "Keypad code"
477 id: keypad
478
479 - platform: template # Visualizza i numeri sul display
480 name: "Keypad OLED"
481 id: kpad
482 internal: true
483
484 - platform: template
485 name: "page"
486 id: page
487 internal: true
488
489 - platform: template
490 id: fingerprint_state
491 name: "Stato impronta"
492
493 - platform: template
494 name: "Keypad code test"
495 id: keypadp
496
497################################################################################
498## SENSOR
499################################################
500sensor:
501 - platform: wifi_signal
502 name: "${esp_devicename} WiFi Signal"
503 update_interval: 10s
504
505 - platform: fingerprint_grow
506 fingerprint_count:
507 name: "Conta impronte"
508 last_finger_id:
509 name: "Ultimo ID impronta"
510 last_confidence:
511 name: "Affidabilità ultima impronta"
512 status:
513 name: "Stato impronta"
514 capacity:
515 name: "Capacità impronta"
516 security_level:
517 name: "Livello di sicurezza"
518
519################################################################################
520## Binary Sensor
521################################################
522binary_sensor:
523 - platform: fingerprint_grow
524 id: fingerprint_enrolling
525 name: "Acquisendo impronta"
526
527 - platform: template # Allarme Disattivato
528 name: "Allarme Disattivato"
529 internal: true
530 lambda: |-
531 if (id(alarm_state).state == "Disattivato") {
532 return true;
533 } else {
534 return false;
535 }
536 on_press:
537 - while:
538 condition:
539 lambda: |-
540 if (id(alarm_state).state == "Disattivato") {
541 return true;
542 } else {
543 return false;
544 }
545 then:
546 - output.turn_on: led_blu
547 - delay: 50ms
548 - output.turn_off: led_blu
549 - delay: 3s
550
551 - platform: template # Allarme Scattato LED
552 name: "Allarme Scattato"
553 internal: true
554 lambda: |-
555 if (id(allarme_scattato).state == "on") {
556 return true;
557 } else {
558 return false;
559 }
560 on_press:
561 - while:
562 condition:
563 lambda: |-
564 if (id(allarme_scattato).state == "on") {
565 return true;
566 } else {
567 return false;
568 }
569 then:
570 - output.turn_on: led_blu
571 - delay: 300ms
572 - output.turn_off: led_blu
573 - output.turn_on: led_rosso
574 - delay: 300ms
575 - output.turn_off: led_rosso
576
577 - platform: template # Allarme Attivo
578 name: "Allarme Attivo"
579 internal: true
580 lambda: |-
581 if (id(alarm_state).state == "Attivo") {
582 return true;
583 } else {
584 return false;
585 }
586 on_press:
587 - while:
588 condition:
589 lambda: |-
590 if (id(alarm_state).state == "Attivo") {
591 return true;
592 } else {
593 return false;
594 }
595 then:
596 - output.turn_on: led_rosso
597 - delay: 80ms
598 - output.turn_off: led_rosso
599 - delay: 80ms
600 - output.turn_on: led_rosso
601 - delay: 80ms
602 - output.turn_off: led_rosso
603 - delay: 3s
604
605 - platform: template # Allarme Pedding
606 name: "Allarme Pedding"
607 internal: true
608 lambda: |-
609 if (id(alarm_state).state == "...Avvio") {
610 return true;
611 } else {
612 return false;
613 }
614 on_press:
615 - while:
616 condition:
617 lambda: |-
618 if (id(alarm_state).state == "...Avvio") {
619 return true;
620 } else {
621 return false;
622 }
623 then:
624 - output.turn_on: led_giallo
625 - delay: 500ms
626 - output.turn_off: led_giallo
627 - delay: 500ms
628
629 - platform: template # Allarme Scattato
630 name: "Allarme Scat"
631 internal: true
632 lambda: |-
633 if (id(allarme_scattato).state == "on") {
634 return true;
635 } else {
636 return false;
637 }
638 on_press:
639 then:
640 - display.page.show: page4
641 on_release:
642 then:
643 - display.page.show: page1
644
645################################################################################
646## OUTPUT
647################################################
648output:
649 - platform: ledc
650 pin: GPIO15
651 id: buzzer_output
652
653 - platform: ledc
654 id: led_verde
655 pin: GPIO19
656
657 - platform: ledc
658 id: led_giallo
659 pin: GPIO18
660
661 - platform: ledc
662 id: led_rosso
663 pin: GPIO4
664
665 - platform: ledc
666 id: led_blu
667 pin: GPIO2
668
669################################################################################
670## RTTTL
671################################################
672rtttl:
673 output: buzzer_output
674
675################################################################################
676## Switch
677################################################
678switch:
679 - platform: output
680 internal: true
681 name: 'buzzer'
682 id: buzzer
683 output: buzzer_output
684 on_turn_on:
685 then:
686 - output.turn_on: buzzer_output
687 - output.ledc.set_frequency:
688 id: buzzer_output
689 frequency: "2000Hz"
690 - output.set_level:
691 id: buzzer_output
692 level: "90%"
693
694 on_turn_off:
695 then:
696 - output.turn_off: buzzer_output
697
698 - platform: output
699 internal: true
700 name: 'buzzer error'
701 id: buzzer_error
702 output: buzzer_output
703 on_turn_on:
704 then:
705 - output.turn_on: buzzer_output
706 - output.ledc.set_frequency:
707 id: buzzer_output
708 frequency: "200Hz"
709 - output.set_level:
710 id: buzzer_output
711 level: "90%"
712
713 on_turn_off:
714 then:
715 - output.turn_off: buzzer_output
716
717 - platform: restart
718 name: "Restart Pad"
719 id: restart_switch