-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesphome_fishfeeder.yaml
More file actions
408 lines (355 loc) · 11.5 KB
/
esphome_fishfeeder.yaml
File metadata and controls
408 lines (355 loc) · 11.5 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
substitutions:
name: fishfeeder
friendly_name: Fish feeder
esphome:
name: ${name}
friendly_name: ${friendly_name}
min_version: 2024.6.0
name_add_mac_suffix: true
project:
name: esphome.web
version: '1.0'
esp32:
board: esp32cam
wifi:
networks:
- ssid: "xxx"
password: "yyy"
ap:
ssid: "${friendly_name} Setup"
ap_timeout: 5min # Optional: AP deaktiviert sich nach 5 Minuten
captive_portal:
improv_serial: # Erlaubt Konfiguration über USB
# Automatisches OTA + Bluetooth-Konfiguration
ota:
- platform: esphome
# Enable logging
logger:
# level: DEBUG
# Enable Home Assistant API
api:
services: # change camera parameters on-the-fly
- service: camera_set_param
variables:
name: string
value: int
then:
- lambda: |-
bool state_return = false;
if (("contrast" == name) && (value >= -2) && (value <= 2)) { id(espcam).set_contrast(value); state_return = true; }
if (("brightness" == name) && (value >= -2) && (value <= 2)) { id(espcam).set_brightness(value); state_return = true; }
if (("saturation" == name) && (value >= -2) && (value <= 2)) { id(espcam).set_saturation(value); state_return = true; }
if (("special_effect" == name) && (value >= 0U) && (value <= 6U)) { id(espcam).set_special_effect((esphome::esp32_camera::ESP32SpecialEffect)value); state_return = true; }
if (("aec_mode" == name) && (value >= 0U) && (value <= 1U)) { id(espcam).set_aec_mode((esphome::esp32_camera::ESP32GainControlMode)value); state_return = true; }
if (("aec2" == name) && (value >= 0U) && (value <= 1U)) { id(espcam).set_aec2(value); state_return = true; }
if (("ae_level" == name) && (value >= -2) && (value <= 2)) { id(espcam).set_ae_level(value); state_return = true; }
if (("aec_value" == name) && (value >= 0U) && (value <= 1200U)) { id(espcam).set_aec_value(value); state_return = true; }
if (("agc_mode" == name) && (value >= 0U) && (value <= 1U)) { id(espcam).set_agc_mode((esphome::esp32_camera::ESP32GainControlMode)value); state_return = true; }
if (("agc_value" == name) && (value >= 0U) && (value <= 30U)) { id(espcam).set_agc_value(value); state_return = true; }
if (("agc_gain_ceiling" == name) && (value >= 0U) && (value <= 6U)) { id(espcam).set_agc_gain_ceiling((esphome::esp32_camera::ESP32AgcGainCeiling)value); state_return = true; }
if (("wb_mode" == name) && (value >= 0U) && (value <= 4U)) { id(espcam).set_wb_mode((esphome::esp32_camera::ESP32WhiteBalanceMode)value); state_return = true; }
if (("test_pattern" == name) && (value >= 0U) && (value <= 1U)) { id(espcam).set_test_pattern(value); state_return = true; }
if (("vertical_flip" == name) && (value >= 0U) && (value <= 1U)) { id(espcam).set_vertical_flip(value); state_return = true; }
if (("horizontal_mirror" == name) && (value >= 0U) && (value <= 1U)) { id(espcam).set_horizontal_mirror(value); state_return = true; }
if (true == state_return) {
id(espcam).update_camera_parameters();
}
else {
ESP_LOGW("esp32_camera_set_param", "Error in name or data range");
}
- service: feed_grams # Feed x grams
variables:
weight: float
then:
- lambda: |-
const uint32_t MAX_FEED_DURATION_MS = 100*1000; // Sicherheitsgrenze
float grams_per_pulse = id(weight_per_pulse).state;
float pulse_duration_ms = id(feed_pulse_duration).state;
if (grams_per_pulse <= 0.0f || pulse_duration_ms <= 0.0f) {
ESP_LOGW("feed", "weight_per_pulse or feed_pulse_duration is not set!");
return;
}
float pulses_needed = weight / grams_per_pulse;
if (pulses_needed <= 0.0f) {
ESP_LOGW("feed", "Calculated pulses is zero, aborting feed.");
return;
}
uint32_t total_duration = (uint32_t)(pulses_needed * pulse_duration_ms);
if (total_duration > MAX_FEED_DURATION_MS) {
ESP_LOGW("feed", "Feed duration too long (%d ms). Aborting for safety.", total_duration);
return;
}
ESP_LOGI("feed", "Füttere %.1f g (%.2f Pulsäquivalente, Dauer %d ms)", weight, pulses_needed, total_duration);
// Servo anwerfen
id(feed_servo).write(id(servo_speed).state / 100.0);
// Zeit merken wann er stoppen soll
id(feed_end_time) = millis() + total_duration;
id(feed_active) = true;
// Zähler direkt aktualisieren
id(total_pulses) += (int) roundf(pulses_needed);
id(total_weight) += weight;
# Confugure the hardware pins
output:
- platform: gpio
pin: GPIO4
id: light_pin
- platform: ledc
id: feed_servo_output
pin: GPIO13
frequency: 50 Hz
light:
- platform: binary
output: light_pin
name: camera light
# ================== Servo für Förderschnecke ==================
servo:
- id: feed_servo
output: feed_servo_output
min_level: 12%
max_level: 3%
esp32_camera:
id: espcam
name: camera
external_clock:
pin: GPIO0
frequency: 10MHz
i2c_pins:
sda: GPIO26
scl: GPIO27
data_pins: [GPIO5, GPIO18, GPIO19, GPIO21, GPIO36, GPIO39, GPIO34, GPIO35]
vsync_pin: GPIO25
href_pin: GPIO23
pixel_clock_pin: GPIO22
power_down_pin: GPIO32
resolution: 800x600
jpeg_quality: 10 # max. 63
max_framerate: 5.0fps
idle_framerate: 0.2fps
vertical_flip: true
horizontal_mirror: false
brightness: 2 # -2 to 2
contrast: 1 # -2 to 2
special_effect: none
# exposure settings
aec_mode: auto
aec2: false
ae_level: 0
aec_value: 300
# gain settings
agc_mode: auto
agc_gain_ceiling: 2x
agc_value: 0
# white balance setting
wb_mode: auto
## Globale Variablen
globals:
- id: total_pulses
type: int
restore_value: true
initial_value: '0'
- id: total_weight
type: float
restore_value: true
initial_value: '0.0'
- id: feed_active
type: bool
restore_value: false
initial_value: 'false'
- id: feed_end_time
type: uint32_t
restore_value: false
initial_value: '0'
# ================== Home Assistant Steuerung ==================
number:
# Servo control between -100% (linkslauf) <-> 0 <-> 100% (rechtslauf)
- platform: template
name: "Servo Control"
id: servo_control
entity_category: config
min_value: -100
initial_value: 0
max_value: 100
step: 1
optimistic: true
set_action:
then:
- servo.write:
id: feed_servo
level: !lambda 'return x / 100.0;'
# Dauer eines Futterimpulses
- platform: template
name: "Impulse Duration"
id: feed_pulse_duration
entity_category: config
min_value: 100
max_value: 30000
unit_of_measurement: "ms"
initial_value: 500
step: 100
optimistic: true
restore_value: true
# Impulsgeschwindigkeit
- platform: template
name: "Impulse Speed"
id: servo_speed
entity_category: config
min_value: 10
max_value: 100
initial_value: 25
step: 1
optimistic: true
restore_value: true
## Configuration variables
- platform: template
name: "Total Pulses"
id: total_pulses_display
lambda: |-
return id(total_pulses);
set_action:
then:
- lambda: |-
id(total_pulses) = x;
update_interval: 60s
min_value: 0
max_value: 999999
step: 1
mode: box
entity_category: config
icon: mdi:gauge
- platform: template
name: "Total Weight"
id: total_weight_display
lambda: |-
return id(total_weight);
set_action:
then:
- lambda: |-
id(total_weight) = x;
update_interval: 60s
unit_of_measurement: "g"
min_value: 0
max_value: 999999
step: 1
mode: box
entity_category: config
icon: mdi:scale
- platform: template
name: "Weight per Pulse"
id: weight_per_pulse
unit_of_measurement: "g"
min_value: 0
max_value: 100
step: 0.1
entity_category: config
icon: mdi:gauge
restore_value: True
optimistic: True
# Knöpfe zum bedienen
button:
# Einmal Füttern
- platform: template
name: "Feed now"
on_press:
then:
- script.execute:
id: feed
pulses: 1
# Festgefressenes losrütteln
- platform: template
name: "Unclog device"
on_press:
then:
- script.execute: unclog
# Servo abschalten
- platform: template
name: "Stop servo"
on_press:
then:
- servo.detach: feed_servo
- lambda: 'id(servo_control) = 0;'
- lambda: 'id(servo_control).publish_state(0);'
# Neu starten
- platform: restart
name: "Restart device"
# Zähler zurücksetzen
- platform: template
name: "Reset Counters"
id: reset_counters
entity_category: config
on_press:
then:
- lambda: |-
id(total_pulses) = 0;
id(total_weight) = 0.0;
script:
# Festgefressenes losruetteln
- id: unclog
then:
- lambda: |-
ESP_LOGI("feed", "Try to unclog the auger");
- repeat:
count: 10
then:
- servo.write:
id: feed_servo
level: -30% # etwas nach Links
- delay: 150ms
- servo.write:
id: feed_servo
level: 30% # etwas nach Rechts
- delay: 150ms
- servo.detach: feed_servo
# Fische einmal füttern
- id: feed
parameters:
pulses: int
then:
- lambda: |-
ESP_LOGI("feed", "Feeding with %d pulses", pulses);
- repeat:
count: !lambda 'return pulses;'
then:
- lambda: |-
id(feed_servo).write(id(servo_speed).state / 100.0);
- delay: !lambda 'return (uint32_t) id(feed_pulse_duration).state;'
- lambda: |-
id(feed_servo).write(0.0);
- delay: 200ms
- lambda: |-
id(total_pulses) += 1;
id(total_weight) += id(weight_per_pulse).state;
- servo.detach: feed_servo
time:
- platform: homeassistant
id: homeassistant_time
binary_sensor:
- platform: status
name: Status
sensor:
- platform: wifi_signal
name: WiFi Signal
text_sensor:
- platform: wifi_info
ip_address:
name: "IP Address"
- platform: wifi_info
mac_address:
name: "MAC Address"
- platform: template
entity_category: diagnostic
name: "Hostname"
lambda: |-
return { App.get_name() };
# Stop the servo afrt the feeding has ended
# (This is needed because ESPHome is not multithreaded)
interval:
- interval: 100ms
then:
- lambda: |-
if (id(feed_active)) {
if ((int32_t)(millis() - id(feed_end_time)) >= 0) {
ESP_LOGI("feed", "Finished feeding, Stopping servo.");
id(feed_servo).write(0.0);
id(feed_active) = false;
}
}