-
-
Notifications
You must be signed in to change notification settings - Fork 173
/
tagreader.yaml
196 lines (174 loc) · 4.89 KB
/
tagreader.yaml
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
# Insert your SSID and Your PWD after inital setup
wifi:
networks:
# - ssid: !secret wifi_ssid # Uncomment this line (remove # at beginning of line) and enter your details in secrets.yaml file!
# password: !secret wifi_password # Uncomment this line (remove # at beginning of line) and enter your details in secrets.yaml file!
ap:
ssid: ${devicename}
# Enable the captive portal for inital WiFi setup
captive_portal:
substitutions:
devicename: tagreader
friendly_name: TagReader
esphome:
name: $devicename
platform: ESP8266
board: d1_mini
# If buzzer is enabled, notify on api connection success
on_boot:
priority: -10
then:
- wait_until:
api.connected:
- logger.log: API is connected!
- rtttl.play: "success:d=24,o=5,b=100:c,g,b"
# Define switches to control LED and buzzer from HA
switch:
- platform: template
name: "${friendly_name} Buzzer Enabled"
id: buzzer_enabled
icon: mdi:volume-high
optimistic: true
- platform: template
name: "${friendly_name} LED enabled"
id: led_enabled
icon: mdi:alarm-light-outline
optimistic: true
# Enable logging
logger:
# Enable Home Assistant API
api:
services:
- service: rfidreader_tag_ok
then:
- rtttl.play: "beep:d=16,o=5,b=100:b"
- service: rfidreader_tag_ko
then:
- rtttl.play: "beep:d=8,o=5,b=100:b"
- service: play_rtttl
variables:
song_str: string
then:
- rtttl.play: !lambda 'return song_str;'
- service: write_tag_random
variables:
continuous: bool
then:
- lambda: 'id(continous_writing_mode) = continuous;'
- while:
condition:
lambda: 'return id(continous_writing_mode);'
then:
- lambda: |-
static const char alphanum[] = "0123456789abcdef";
std::string uri = "https://www.home-assistant.io/tag/";
for (int i = 0; i < 8; i++)
uri += alphanum[random_uint32() % (sizeof(alphanum) - 1)];
uri += "-";
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 4; i++)
uri += alphanum[random_uint32() % (sizeof(alphanum) - 1)];
uri += "-";
}
for (int i = 0; i < 12; i++)
uri += alphanum[random_uint32() % (sizeof(alphanum) - 1)];
auto message = new nfc::NdefMessage();
message->add_uri_record(uri);
ESP_LOGD("tagreader", "Writing payload: %s", uri.c_str());
id(pn532_board).write_mode(message);
- wait_until:
not:
pn532.is_writing:
- service: write_tag_id
variables:
tag_id: string
then:
- globals.set:
id: continous_writing_mode
value: 'false'
- lambda: |-
auto message = new nfc::NdefMessage();
std::string uri = "https://www.home-assistant.io/tag/";
uri += tag_id;
message->add_uri_record(uri);
id(pn532_board).write_mode(message);
- service: clean_tag
then:
- globals.set:
id: continous_writing_mode
value: 'false'
- lambda: 'id(pn532_board).clean_mode();'
- service: cancel_writing
then:
- globals.set:
id: continous_writing_mode
value: 'false'
- lambda: 'id(pn532_board).read_mode();'
# Enable OTA upgrade
ota:
globals:
- id: continous_writing_mode
type: bool
restore_value: no
initial_value: 'false'
i2c:
scan: False
frequency: 400kHz
pn532_i2c:
id: pn532_board
on_tag:
then:
- homeassistant.tag_scanned: !lambda |
if (!tag.has_ndef_message()) {
ESP_LOGD("tagreader", "No NDEF");
return x;
}
auto message = tag.get_ndef_message();
auto records = message->get_records();
for (auto &record : records) {
std::string payload = record->get_payload();
size_t pos = payload.find("https://www.home-assistant.io/tag/");
if (pos != std::string::npos) {
return payload.substr(pos + 34);
}
}
ESP_LOGD("tagreader", "Bad NDEF, fallback to uid");
return x;
- if:
condition:
switch.is_on: buzzer_enabled
then:
- rtttl.play: "success:d=24,o=5,b=100:c,g,b"
- if:
condition:
switch.is_on: led_enabled
then:
- light.turn_on:
id: activity_led
brightness: 100%
red: 0%
green: 100%
blue: 0%
flash_length: 500ms
# Define the buzzer output
output:
- platform: esp8266_pwm
pin: D7
id: buzzer
binary_sensor:
- platform: status
name: "${friendly_name} Status"
# Define buzzer as output for RTTTL
rtttl:
output: buzzer
# Configure LED
light:
- platform: fastled_clockless
chipset: WS2812
pin: D8
default_transition_length: 10ms
num_leds: 1
rgb_order: GRB
id: activity_led
name: "${friendly_name} LED"
restore_mode: ALWAYS_OFF