· 2 years ago · Sep 20, 2023, 08:25 AM
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: "XXXXXXXX"
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: "2vY7511Y3ldi67iVIKRg7+1lSN4G3Qx9I="
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:shield-lock-outline
92 id: ok
93 resize: 75x75
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 > 100;'
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 - delay: 3000ms
153
154 - text_sensor.template.publish:
155 id: fingerprint_state
156 state: "Attesa"
157
158 - display.page.show: page1
159
160 on_finger_scan_unmatched:
161 - text_sensor.template.publish:
162 id: fingerprint_state
163 state: "Impronta non autorizzata"
164
165 - fingerprint_grow.aura_led_control:
166 state: FLASHING
167 speed: 25
168 color: RED
169 count: 3
170
171 on_enrollment_scan:
172 - text_sensor.template.publish:
173 id: fingerprint_state
174 state: "Impronta acquisita"
175
176 - fingerprint_grow.aura_led_control:
177 state: FLASHING
178 speed: 25
179 color: WHITE
180 count: 2
181 - fingerprint_grow.aura_led_control:
182 state: ALWAYS_ON
183 speed: 0
184 color: YELLOW
185 count: 0
186
187 on_enrollment_failed:
188 - text_sensor.template.publish:
189 id: fingerprint_state
190 state: "Acquisizione impronta fallita"
191 - fingerprint_grow.aura_led_control:
192 state: FLASHING
193 speed: 25
194 color: RED
195 count: 4
196
197################################################################################
198## PAD
199################################################
200matrix_keypad:
201 id: mykeypad
202 rows:
203 - pin: GPIO19
204 - pin: GPIO33
205 - pin: GPIO25
206 - pin: GPIO26
207 columns:
208 - pin: GPIO27
209 - pin: GPIO13
210 - pin: GPIO5
211 keys: "123456789*0#"
212 has_diodes: false
213
214################################################################################
215## Key Collector
216################################################
217key_collector:
218 - id: pincode_reader
219 source_id: mykeypad
220 min_length: 1
221 max_length: 4
222 back_keys: "*"
223 end_key_required: false
224 clear_keys: "#"
225 allowed_keys: "0123456789"
226 timeout: 4s
227 on_progress:
228 - logger.log:
229 format: "input progress: '%s', started by '%c'"
230 args: [ 'x.c_str()', "(start == 0 ? '~' : start)" ]
231 on_result:
232 then:
233
234 - logger.log:
235 format: "input result: '%s', started by '%c', ended by '%c''x.c_str()'"
236 args: [ 'x.c_str()', "(start == 0 ? '~' : start)", "(end == 0 ? '~' : end)" ]
237
238 - text_sensor.template.publish:
239 id: keypad
240 state: !lambda "return x.c_str();"
241
242 - delay: 2s
243
244 - text_sensor.template.publish:
245 id: keypad
246 state: ""
247
248 - id: pincode_reader2
249 source_id: mykeypad
250 min_length: 1
251 max_length: 4
252 back_keys: "*"
253 end_key_required: false
254 clear_keys: "#"
255 allowed_keys: "0123456789"
256 timeout: 4s
257
258 on_progress:
259 - script.stop: timer
260 - script.execute: timer
261
262 - if:
263 condition:
264 lambda: 'return id(page).state != "1";'
265 then:
266 - text_sensor.template.publish:
267 id: page
268 state: "2"
269 - display.page.show: page2
270
271 - text_sensor.template.publish:
272 id: page
273 state: "2"
274
275 - switch.turn_on: buzzer
276 - delay: 100ms
277 - switch.turn_off: buzzer
278
279 - logger.log:
280 format: "input progress: '%s', started by '%c'"
281 args: [ 'x.c_str()', "(start == 0 ? '~' : start)" ]
282
283 - text_sensor.template.publish:
284 id: kpad
285 state: !lambda "return std::string(x.length(), 'x').c_str();"
286
287 on_result:
288
289 - script.stop: timer
290 - text_sensor.template.publish:
291 id: kpad
292 state: ""
293
294 - if:
295 condition:
296 lambda: 'return id(allarme_scattato).state == "on";'
297 then:
298 - display.page.show: page4
299 else:
300 - display.page.show: page1
301
302 - text_sensor.template.publish:
303 id: page
304 state: "1"
305
306 on_timeout:
307 - script.stop: timer
308 - logger.log:
309 format: "input timeout: '%s', started by '%c'"
310 args: [ 'x.c_str()', "(start == 0 ? '~' : start)" ]
311
312 - text_sensor.template.publish:
313 id: kpad
314 state: !lambda "return x.c_str();"
315
316 - if:
317 condition:
318 lambda: 'return id(allarme_scattato).state == "on";'
319 then:
320 - display.page.show: page4
321 else:
322 - display.page.show: page1
323
324 - text_sensor.template.publish:
325 id: page
326 state: "1"
327
328################################################################################
329## Script
330################################################
331script:
332 id: timer
333 then:
334 - delay: 4s
335 - if:
336 condition:
337 lambda: 'return id(allarme_scattato).state == "on";'
338 then:
339 - display.page.show: page4
340 else:
341 - display.page.show: page1
342
343################################################################################
344## Display
345################################################
346display:
347 - platform: ssd1306_i2c
348 model: "SSD1306 128x64"
349 reset_pin: GPIO23
350 address: 0x3C
351 id: my_display
352 pages:
353 - id: page1
354 lambda: |-
355 it.clear();
356 it.printf(64, 3, id(font1), TextAlign::TOP_CENTER, "Allarme");
357 it.image(2, 2, id(alert));
358 it.line(1, 0, 124, 0);
359 it.line(1, 63, 124, 63);
360 it.line(0, 1, 0, 62);
361 it.line(125, 1, 125, 62);
362
363 if (id(alarm_state).state.length() == 0) {
364 it.printf(64, 24, id(font2), TextAlign::TOP_CENTER, "Starting");
365 } else if (id(alarm_state).state == "Attivo") {
366 it.printf(64, 24, id(font2), TextAlign::TOP_CENTER, id(alarm_state).state.c_str());
367 } else if (id(alarm_state).state == "Avvio") {
368 it.printf(64, 24, id(font2), TextAlign::TOP_CENTER, id(alarm_state).state.c_str());
369 } else if (id(alarm_state).state == "Disattivato") {
370 it.clear();
371 } else {
372 it.clear();
373 }
374
375 - id: page2
376 lambda: |-
377 it.printf(64, 10, id(font3), TextAlign::TOP_CENTER, id(kpad).state.c_str());
378
379 - id: page3
380 lambda: |-
381 it.printf(64, 10, id(font4), TextAlign::TOP_CENTER, id(fingerprint_state).state.c_str());
382
383 - id: page4
384 lambda: |-
385 it.clear();
386 it.printf(64, 3, id(font1), TextAlign::TOP_CENTER, "Allarme");
387
388 static bool flashText = false;
389 flashText = !flashText;
390 if (flashText) {
391 it.printf(64, 24, id(font2), TextAlign::TOP_CENTER, "Scattato");
392 }
393
394 it.image(2, 2, id(alert));
395 it.line(1, 0, 124, 0);
396 it.line(1, 63, 124, 63);
397 it.line(0, 1, 0, 62);
398 it.line(125, 1, 125, 62);
399
400 - id: page5
401 lambda: |-
402 it.image(2, 2, id(ok));
403
404
405
406################################################################################
407## Text Sensor
408################################################
409text_sensor:
410 - platform: homeassistant
411 entity_id: sensor.stato_allarme_pad_oled
412 name: "Stato Allarme"
413 id: alarm_state
414
415 - platform: homeassistant
416 entity_id: sensor.stato_allarme
417 name: "Stato Allarme"
418 id: allarme
419
420 - platform: homeassistant
421 name: "Allarme Scattato"
422 entity_id: input_boolean.allarme_scattato
423 id: allarme_scattato
424 internal: true
425
426 - platform: homeassistant
427 name: "input"
428 id: input
429 entity_id: sensor.pad_oled_allarme_keypad_code
430 internal: true
431
432 - platform: homeassistant
433 name: "error"
434 id: error
435 entity_id: sensor.counter_code_error
436 internal: true
437
438 - platform: wifi_info
439 ip_address:
440 name: "${esp_devicename} IP"
441 icon: "mdi:lan"
442
443 - platform: template
444 name: "Keypad OLED code"
445 id: keypad
446
447 - platform: template # Visualizza i numeri sul display
448 name: "Keypad OLED"
449 id: kpad
450 internal: true
451
452 - platform: template
453 name: "page"
454 id: page
455 internal: true
456
457 - platform: template
458 id: fingerprint_state
459 name: "Stato impronta"
460
461################################################################################
462## SENSOR
463################################################
464sensor:
465 - platform: wifi_signal
466 name: "${esp_devicename} WiFi Signal"
467 update_interval: 10s
468
469 - platform: fingerprint_grow
470 fingerprint_count:
471 name: "Conta impronte"
472 last_finger_id:
473 name: "Ultimo ID impronta"
474 last_confidence:
475 name: "Affidabilità ultima impronta"
476 status:
477 name: "Stato impronta"
478 capacity:
479 name: "Capacità impronta"
480 security_level:
481 name: "Livello di sicurezza"
482
483################################################################################
484## Binary Sensor
485################################################
486binary_sensor:
487 - platform: fingerprint_grow
488 id: fingerprint_enrolling
489 name: "Acquisendo impronta"
490
491 - platform: template # Allarme Disattivato
492 name: "Allarme Disattivato"
493 internal: true
494 lambda: |-
495 if (id(alarm_state).state == "Disattivato") {
496 return true;
497 } else {
498 return false;
499 }
500 on_press:
501 - while:
502 condition:
503 lambda: |-
504 if (id(alarm_state).state == "Disattivato") {
505 return true;
506 } else {
507 return false;
508 }
509 then:
510 - output.turn_on: led_blu
511 - delay: 50ms
512 - output.turn_off: led_blu
513 - delay: 3s
514
515 - platform: template # Allarme Scattato LED
516 name: "Allarme Scattato"
517 internal: true
518 lambda: |-
519 if (id(allarme_scattato).state == "on") {
520 return true;
521 } else {
522 return false;
523 }
524 on_press:
525 - while:
526 condition:
527 lambda: |-
528 if (id(allarme_scattato).state == "on") {
529 return true;
530 } else {
531 return false;
532 }
533 then:
534 - output.turn_on: led_blu
535 - delay: 300ms
536 - output.turn_off: led_blu
537 - output.turn_on: led_rosso
538 - delay: 300ms
539 - output.turn_off: led_rosso
540
541 - platform: template # Allarme Attivo
542 name: "Allarme Attivo"
543 internal: true
544 lambda: |-
545 if (id(alarm_state).state == "Attivo") {
546 return true;
547 } else {
548 return false;
549 }
550 on_press:
551 - while:
552 condition:
553 lambda: |-
554 if (id(alarm_state).state == "Attivo") {
555 return true;
556 } else {
557 return false;
558 }
559 then:
560 - output.turn_on: led_rosso
561 - delay: 80ms
562 - output.turn_off: led_rosso
563 - delay: 80ms
564 - output.turn_on: led_rosso
565 - delay: 80ms
566 - output.turn_off: led_rosso
567 - delay: 3s
568
569 - platform: template # Allarme Pedding
570 name: "Allarme Pedding"
571 internal: true
572 lambda: |-
573 if (id(alarm_state).state == "Avvio") {
574 return true;
575 } else {
576 return false;
577 }
578 on_press:
579 - while:
580 condition:
581 lambda: |-
582 if (id(alarm_state).state == "Avvio") {
583 return true;
584 } else {
585 return false;
586 }
587 then:
588 - output.turn_on: led_giallo
589 - delay: 500ms
590 - output.turn_off: led_giallo
591 - delay: 500ms
592
593 - platform: template # Allarme Scattato
594 name: "Allarme Scat"
595 internal: true
596 lambda: |-
597 if (id(allarme_scattato).state == "on") {
598 return true;
599 } else {
600 return false;
601 }
602 on_press:
603 then:
604 - display.page.show: page4
605 on_release:
606 then:
607 - display.page.show: page1
608
609################################################################################
610## Sensor
611################################################
612output:
613 - platform: ledc
614 pin: GPIO15
615 id: buzzer_output
616
617 - platform: ledc
618 id: led_verde
619 pin: GPIO19
620
621 - platform: ledc
622 id: led_giallo
623 pin: GPIO18
624
625 - platform: ledc
626 id: led_rosso
627 pin: GPIO4
628
629 - platform: ledc
630 id: led_blu
631 pin: GPIO2
632
633################################################################################
634## OUTPUT
635################################################
636switch:
637 - platform: output
638 internal: true
639 name: 'buzzer'
640 id: buzzer
641 output: buzzer_output
642 on_turn_on:
643 then:
644 - output.turn_on: buzzer_output
645 - output.ledc.set_frequency:
646 id: buzzer_output
647 frequency: "2000Hz"
648 - output.set_level:
649 id: buzzer_output
650 level: "90%"
651
652 on_turn_off:
653 then:
654 - output.turn_off: buzzer_output
655
656 - platform: restart
657 name: "Restart Pad"
658 id: restart_switch