-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcat-feeder.yaml
282 lines (246 loc) · 7.04 KB
/
cat-feeder.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# -------------------------------------
# ---- Implemented on owon smart pet feeder ----
# ---- https://www.instructables.com/ESPHome-Home-Assistant-Cat-Feeder/ ----
#
# Motor driver chip is a TC118S
# INA INB MOTOR
# L L Hi-Z
# L H Left
# H L right
# H H Brake
#
#
# |--------|
# | TYWE3S |
# | |
# RST | | GPIO1 TXD0 (Ultrasonic Trigger)
# ADC | | GPIO3 RXD0 (Ultrasonic Echo + 1k resister)
# EN | | GPIO5 (MotorDriveB)
# (Red LED) GPIO16 | | GPIO4 (MotorDriveA)
# (Blue LED) GPIO14 | | GPIO0 (TopButton)
# GPIO12 | | GPIO2
# (MotorPosition) GPIO13 | | GPIO15
# VCC |--------| GND
#
# -------------------------------------
substitutions:
devicename: cat-feeder
distance_empty: "0.3"
esphome:
name: ${devicename}
platform: ESP8266
board: esp_wroom_02
wifi:
power_save_mode: light # power reduced from 1.2w to 0.8w
#fast_connect: true
networks:
- ssid: !secret wifi_ssid
password: !secret wifi_password
hidden: True
manual_ip:
static_ip: !secret ip_cat_feeder
gateway: !secret ip_gateway
subnet: 255.255.255.0
domain: .homelan
ap: # Enable fallback hotspot (captive portal) in case wifi connection fails
ssid: "${devicename} Hotspot"
password: !secret password
captive_portal:
# Enable logging
logger:
level: WARN
# Enable Home Assistant API
api:
reboot_timeout: 0s # Dont reboot..
encryption:
key: !secret encryption_key
ota:
password: !secret password
web_server:
port: 80
auth:
username: admin
password: !secret password
#-------------------------------------------------------------------------------
text_sensor:
- platform: template
name: "${devicename} State"
lambda: |-
if( id(run_motor).is_running() ) { return {"Running"}; }
else
if( id(flash_pos_switch_error).is_running() ) { return {"Jammed"}; }
else
return {"Idle"};
update_interval: 1s
switch:
- platform: gpio # Motor driver
pin: GPIO4
id: motor_drive_a
restore_mode: ALWAYS_OFF
- platform: gpio # Motor driver
pin: GPIO5
id: motor_drive_b
restore_mode: ALWAYS_OFF
- platform: gpio # BLUE led (D1)
pin: GPIO14
id: led_blue
inverted: true
restore_mode: ALWAYS_OFF
- platform: template # Home assistant control
id: ha_run
name: "${devicename} Run"
icon: "mdi:cat"
restore_state: off
lambda: "return id(run_motor).is_running();"
turn_on_action:
- logger.log:
format: "ha_run on_action. Calling script run motor."
level: DEBUG
# Do not run if the product is jammed.
- if:
condition:
- script.is_running: flash_pos_switch_error
then:
- logger.log:
format: "ha_run on_action. Cannot run: Jammed."
level: ERROR
else:
- script.execute: run_motor # Run the motor.
status_led:
pin:
number: GPIO16 # RED led (D3)
inverted: yes
binary_sensor:
- platform: gpio
pin:
number: GPIO0
id: sw_user_btn
filters:
- invert:
- delayed_on_off: 25ms
on_press:
then:
- logger.log:
format: "sw_user_btn pressed. Calling script run motor."
level: DEBUG
- script.execute: run_motor # Run the motor.
- platform: template
id: is_motor_running
on_press:
then:
- switch.turn_on: motor_drive_a # Turn on motor.
- script.execute: flash_motor_running_led
- logger.log:
format: "is_motor_running - go."
level: DEBUG
on_release:
then:
- switch.turn_off: motor_drive_a # Turn off motor.
- script.stop: flash_motor_running_led
- switch.turn_off: led_blue
- logger.log:
format: "is_motor_running - stop."
level: DEBUG
- platform: gpio
pin:
number: GPIO13
id: sw_motor_position
filters:
- delayed_on_off: 25ms
on_release:
then:
- logger.log:
format: "Position switch released - stopping motor."
level: DEBUG
# Stop the motor.
- binary_sensor.template.publish:
id: is_motor_running
state: OFF
# The position switch is ok, clear any error.
- script.stop: flash_pos_switch_error
- platform: gpio
pin:
number: GPIO2
id: unknown_usage
sensor:
- platform: ultrasonic
id: ultras
name: "${devicename} Food Level"
trigger_pin: GPIO1
echo_pin: GPIO3
update_interval: 30s
icon: "mdi:gauge"
accuracy_decimals: 1
unit_of_measurement: "%"
timeout: 1m
filters:
- filter_out: nan
- calibrate_linear: # https://esphome.io/components/sensor/index.html#sensor-filters
# - 0.16 -> 0.0 # Empty Value
- 0.128 -> 0.0 # Time to shake or refill
- 0.06 -> 100.0 # Full Value
- median:
window_size: 10
send_every: 5
script:
- id: motor_overrun_protection # A script to stop the motor if it runs too long
mode: restart
then:
- delay: 4000ms
# If the motor is still running, an error occurred (position switch faulty.)
- if:
condition:
- binary_sensor.is_on: is_motor_running
then:
# Stop the motor.
- binary_sensor.template.publish:
id: is_motor_running
state: OFF
# Flash an error and log an error message.
- script.execute: flash_pos_switch_error
- logger.log:
format: "Motor overrun!! Check motor position switch!"
level: ERROR
- id: run_motor # A script that runs the motor.
# This script will be stopped by the motor position switch.
# If this script runs for > 4 secs, the motors will be stopped and a error logged.
mode: queued
then:
- logger.log:
format: "run_motor script begin."
level: DEBUG
- binary_sensor.template.publish:
id: is_motor_running
state: ON
- script.execute: motor_overrun_protection # Script will restart if it's already running.
# Do nothing until the position switch turns off the motor.
- wait_until:
condition:
- binary_sensor.is_off: is_motor_running
- logger.log:
format: "run_motor script complete."
level: DEBUG
- id: flash_motor_running_led
mode: restart
then:
while:
condition:
lambda: |-
return true;
then:
- switch.turn_on: led_blue
- delay: 250ms
- switch.turn_off: led_blue
- delay: 250ms
- id: flash_pos_switch_error
mode: restart
then:
while:
condition:
lambda: |-
return true;
then:
- switch.turn_on: led_blue
- delay: 150ms
- switch.turn_off: led_blue
- delay: 150ms