-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodemcu.yaml
173 lines (159 loc) · 4.6 KB
/
nodemcu.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
substitutions:
# FIXME: adjust
unit_name: nodemcu
wifi:
# FIXME: adjust
ssid: !secret wifi_ssid
password: !secret wifi_password
fast_connect: true
reboot_timeout: 0s
# I'm using mqtt for communication with home assistant
mqtt:
broker: !secret mqtt_broker
username: !secret mqtt_username
password: !secret mqtt_password
esphome:
name: ${unit_name}
platform: ESP8266
# FIXME: adjust
board: nodemcuv2
includes:
# this sensor will receive responses from the arduino
# https://esphome.io/cookbook/uart_text_sensor.html
- uart_read_line_sensor.h
on_boot:
then:
# optionally reboot arduino when this device is rebooted
- switch.turn_on: arduino_reset
logger:
# move esphome's logger to UART1 since we use UART0 for communcation with the arduino
hardware_uart: UART1
# disable hardware logging
baud_rate: 0
level: INFO
globals:
# global variable for the hostmachine power state
- id: last_host_state
type: std::string
restore_value: no
initial_value: ""
# serial port for arduino
uart:
id: ducky
tx_pin: TX
rx_pin: RX
baud_rate: 115200
# optional
# status_led:
# pin:
# number: GPIO2
# inverted: true
# sensor:
# - platform: wifi_signal
# name: ${unit_name}_signal
# update_interval: 60s
# optional arduino reset pin
switch:
- platform: gpio
pin:
number: D4
inverted: True
id: arduino_reset
on_turn_on:
- delay: 50ms
- switch.turn_off: arduino_reset
text_sensor:
# feedback from arduino
- platform: template
name: ${unit_name}_response
id: ducky_response
on_value:
- logger.log:
level: INFO
format: "ducky_response: %s"
args: ['x.c_str()']
# host machine's usb port power state
- platform: template
id: ducky_host_state
name: ${unit_name}_host_state
# host machine's usb serial input forwarding
- platform: template
id: ducky_usb_input
name: ${unit_name}_usb_input
# topic for message reception
- platform: mqtt_subscribe
id: ducky_request
topic: esphome/${unit_name}/run
retain: false
on_value:
then:
- if:
condition:
# ignore empty messages
lambda: |-
return ((x.length() > 0) && (x != "\n"));
then:
- logger.log:
level: INFO
format: "ducky_request: %s"
args: ['x.c_str()']
- uart.write:
id: ducky
# encapsulate message with STX and ETX chars and write to serial port
data: !lambda |-
char stx = 0x02;
char etx = 0x03;
std::string s = stx + x + etx;
return std::vector<unsigned char>(s.begin(),s.end());
# serial port sensor
- platform: custom
lambda: |-
auto my_custom_sensor = new UartReadLineSensor(id(ducky));
App.register_component(my_custom_sensor);
return {my_custom_sensor};
text_sensors:
id: read_line_sensor
on_value:
then:
- if:
condition:
# usb power state
lambda: |-
return str_startswith(x, "STATE=");
then:
- globals.set:
id: last_host_state
value: !lambda |-
x.erase(0,6);
return x.c_str();
- if:
condition:
lambda: |-
return (id(last_host_state) != id(ducky_host_state).state);
then:
- text_sensor.template.publish:
id: ducky_host_state
state: !lambda |-
return id(last_host_state);
- if:
condition:
# usb serial input forwarding
lambda: |-
return str_startswith(x, "USB_INPUT=");
then:
- text_sensor.template.publish:
id: ducky_usb_input
state: !lambda |-
x.erase(0,10);
return x.c_str();
else:
# responses (ready/busy etc.)
- if:
condition:
lambda: |-
return (x != id(ducky_response).state);
then:
- text_sensor.template.publish:
id: ducky_response
state: !lambda |-
return x;