· 7 months ago · Mar 02, 2025, 04:30 PM
1esphome:
2 name: keypad
3
4esp32:
5 board: nodemcu-32s
6
7logger:
8
9api:
10 encryption:
11 key: "sZti93GjNdfsD6Rqk0mf/GhrtKD91VjJkerdLlIfKFA="
12
13ota:
14 platform: esphome
15 password: "1f0d515204c23555c747838c121486be"
16
17wifi:
18 ssid: "Home_C"
19 password: "7S0ecFPCB62NAw505gnE"
20 fast_connect: true
21
22 ap:
23 ssid: "Keypad Fallback Hotspot"
24 password: "QDOqZE3yzoEP"
25
26captive_portal:
27
28globals:
29 - id: entered_code
30 type: std::string
31 initial_value: '" "'
32 - id: last_key_press_time
33 type: unsigned long
34 initial_value: "0"
35 - id: last_code_sent_time
36 type: unsigned long
37 initial_value: "0"
38 - id: send_empty_flag
39 type: bool
40 initial_value: "false"
41
42matrix_keypad:
43 id: mykeypad
44 rows:
45 - pin: GPIO18
46 - pin: GPIO5
47 - pin: GPIO17
48 - pin: GPIO16
49 columns:
50 - pin: GPIO4
51 - pin: GPIO0
52 - pin: GPIO2
53 keys: "123456789*0#"
54 has_diodes: false
55 on_key:
56 then:
57 - lambda: |-
58 // Bliknutí LED při každém stisku klávesy
59 id(led_keypad).turn_on();
60 delay(100);
61 id(led_keypad).turn_off();
62
63 if (x == '#') {
64 // Po stisku '#' odešle zadané číslo
65 id(keypad_pressed_key).publish_state(id(entered_code));
66 id(last_code_sent_time) = millis(); // Nastaví čas posledního odeslání
67 id(send_empty_flag) = true; // Nastaví příznak pro odeslání prázdného znaku
68
69 // Blikání LED podle počtu zadaných číslic
70 int blink_count = id(entered_code).length();
71 for (int i = 0; i < blink_count; i++) {
72 id(led_keypad).turn_on();
73 delay(100);
74 id(led_keypad).turn_off();
75 delay(100);
76 }
77 // Vymazání zadaného čísla po odeslání
78 id(entered_code) = "";
79 } else {
80 // Aktualizace času posledního stisku klávesy
81 id(last_key_press_time) = millis();
82 // Reset času posledního odeslání a příznaku při stisku klávesy
83 id(last_code_sent_time) = 0;
84 id(send_empty_flag) = false;
85
86 // Přidání stisknuté klávesy do zadaného čísla
87 id(entered_code) += x;
88 }
89
90interval:
91 - interval: 100ms
92 then:
93 - lambda: |-
94 // Kontrola, zda uplynulo 2 sekundy od posledního stisku klávesy
95 if (millis() - id(last_key_press_time) >= 2000 && id(entered_code).length() > 0) {
96 // Odeslání zadaného čísla
97 id(keypad_pressed_key).publish_state(id(entered_code));
98 id(last_code_sent_time) = millis(); // Nastaví čas posledního odeslání
99 id(send_empty_flag) = true; // Nastaví příznak pro odeslání prázdného znaku
100
101 // Blikání LED podle počtu zadaných číslic
102 int blink_count = id(entered_code).length();
103 for (int i = 0; i < blink_count; i++) {
104 id(led_keypad).turn_on();
105 delay(100);
106 id(led_keypad).turn_off();
107 delay(100);
108 }
109
110 // Vymazání zadaného čísla po odeslání
111 id(entered_code) = "";
112 }
113 // Kontrola, zda uplynulo 2 sekundy od posledního odeslání čísla a je-li nastaven příznak
114 if (millis() - id(last_code_sent_time) >= 2000 && id(send_empty_flag)) {
115 // Odeslání prázdného znaku
116 id(keypad_pressed_key).publish_state("");
117 id(send_empty_flag) = false; // Reset příznaku
118 }
119
120text_sensor:
121 - platform: wifi_info
122 ip_address:
123 name: ESP IP Address
124 - platform: template
125 name: "Keypad Pressed Key"
126 id: keypad_pressed_key
127 lambda: |-
128 return {};
129 update_interval: 100ms
130
131sensor:
132 - platform: wifi_signal
133 name: "WiFi Signal Strength"
134 update_interval: 60s
135
136binary_sensor:
137 - platform: status
138 id: wifi_status
139 on_state:
140 then:
141 - if:
142 condition:
143 lambda: 'return id(wifi_status).state;'
144 then:
145 - switch.turn_on: wifi_led
146 else:
147 - switch.turn_off: wifi_led
148
149switch:
150 - platform: restart
151 name: "Keypad - RESTART"
152 - platform: gpio
153 pin: GPIO15
154 id: wifi_led
155 name: "WiFi LED"
156 - platform: gpio
157 pin: GPIO25
158 id: led_keypad
159 name: "Keypad LED"
160
161web_server:
162 port: 80
163 version: 2
164