-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathair-quality-monitor.yaml
More file actions
720 lines (666 loc) · 20.5 KB
/
Copy pathair-quality-monitor.yaml
File metadata and controls
720 lines (666 loc) · 20.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
# ============================================================================
# ESPHome LVGL Air Quality Monitor - Optimized for NO PSRAM
# ============================================================================
# Device: ESP32-2432S028 (CYD - Cheap Yellow Display, 320×240, NO PSRAM)
# Sensors: SCD4x (CO2/Temp/Humidity) + SGP4x (VOC)
# Use Case: 24/7 bedroom/room air quality monitoring
#
# GitHub: https://github.com/el-bakkali/esphome-air-quality-monitor
# 3D Case: https://www.printables.com/model/1586920-cyd-cheap-yellow-display-modular-mod-rear
# ============================================================================
esphome:
name: air-quality-monitor
friendly_name: Air Quality Monitor
esp32:
board: esp32dev
framework:
type: esp-idf
sdkconfig_options:
# NO PSRAM - disable SPIRAM
CONFIG_SPIRAM_USE: "n"
CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY: "n"
# Size optimization for limited RAM
CONFIG_COMPILER_OPTIMIZATION_SIZE: "y"
advanced:
minimum_chip_revision: "3.1"
freertos_in_iram: false # ~8 KB IRAM saved
ringbuf_in_iram: false # ~1.5 KB IRAM saved
heap_in_iram: false # ~4-6 KB IRAM saved
disable_libc_locks_in_iram: true # ~1.3 KB IRAM saved
# ============================================================================
# DEBUG & MONITORING - Critical for NO PSRAM stability
# ============================================================================
debug:
update_interval: 60s
logger:
level: WARN
# Reduce log buffer for memory savings
logs:
component: ERROR
wifi: WARN
api: WARN
# ============================================================================
# CONNECTIVITY
# ============================================================================
api:
encryption:
key: !secret api_encryption_key
# Reboot if API disconnected for 15min (stability)
reboot_timeout: 15min
ota:
- platform: esphome
password: !secret ota_password
# Safe mode for failed OTA recovery
safe_mode:
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
fast_connect: true
min_auth_mode: WPA2
power_save_mode: none
# REDUCED from 15min - faster recovery from WiFi issues
reboot_timeout: 5min
# AP mode fallback
ap:
ssid: "Air-Quality-Monitor"
password: !secret ap_password
captive_portal:
# ============================================================================
# I2C & SPI CONFIGURATION
# ============================================================================
i2c:
- id: bus_a
sda: 27
scl: 22
scan: false # Disable scanning after initial setup for stability
frequency: 100kHz # Conservative speed for sensor reliability
spi:
- id: tft
clk_pin: 14
mosi_pin: 13
miso_pin:
number: 12
ignore_strapping_warning: true
- id: touch
clk_pin: 25
mosi_pin: 32
miso_pin: 39
# ============================================================================
# OUTPUTS & LIGHTS
# ============================================================================
output:
- id: backlight_pwm
platform: ledc
pin: 21
- id: output_red
platform: ledc
pin: 4
inverted: true
- id: output_green
platform: ledc
pin: 16
inverted: true
- id: output_blue
platform: ledc
pin: 17
inverted: true
light:
- id: backlight
platform: monochromatic
output: backlight_pwm
name: Display Backlight
restore_mode: ALWAYS_ON
default_transition_length: 500ms
- id: led
platform: rgb
red: output_red
green: output_green
blue: output_blue
restore_mode: ALWAYS_OFF
# ============================================================================
# DISPLAY - Optimized for NO PSRAM
# ============================================================================
display:
- id: main_display
platform: mipi_spi
model: esp32-2432S028
spi_id: tft
cs_pin:
number: 15
ignore_strapping_warning: true
dc_pin:
number: 2
ignore_strapping_warning: true
data_rate: 40MHz
invert_colors: false
color_order: bgr
dimensions:
width: 320
height: 240
rotation: 180°
auto_clear_enabled: false
update_interval: never
# ============================================================================
# TOUCHSCREEN
# ============================================================================
touchscreen:
- id: main_touchscreen
display: main_display
platform: xpt2046
spi_id: touch
cs_pin: 33
interrupt_pin: 36
threshold: 400
calibration:
x_min: 280
x_max: 3860
y_min: 340
y_max: 3860
transform:
mirror_x: true
on_touch:
- light.turn_on:
id: backlight
brightness: 100%
# Stop anti-burn-in if user touches screen
- if:
condition:
switch.is_on: switch_antiburn
then:
- switch.turn_off: switch_antiburn
# ============================================================================
# TIME
# ============================================================================
time:
- platform: sntp
id: sntp_time
timezone: Europe/London # Change to your timezone
# Update time every 6 hours for accuracy
update_interval: 6h
# Weekly maintenance reboot - cleaner than interval-based approach
on_time:
- cron: '0 0 4 * * SUN' # Every Sunday at 4:00 AM
then:
- logger.log: "Scheduled weekly maintenance reboot"
- delay: 5s
- button.press: restart_button
# ============================================================================
# SENSORS - Optimized with filtering for display stability
# ============================================================================
sensor:
# --- Debug/Monitoring Sensors ---
- platform: debug
free:
id: free_heap
name: "Free Heap"
entity_category: diagnostic
# Alert if heap drops below 30KB (critical threshold)
on_value_range:
- below: 30000
then:
- logger.log:
level: ERROR
format: "CRITICAL: Free heap below 30KB: %.0f bytes"
args: ['x']
# Visual alert on display
- lvgl.label.update:
id: co2_status
text: "MEM!"
- lvgl.widget.update:
id: co2_status
text_color: 0xF44336
block:
id: largest_free_block
name: "Largest Free Block"
entity_category: diagnostic
# Alert if fragmentation is severe (largest block < 20KB)
on_value_range:
- below: 20000
then:
- logger.log:
level: WARN
format: "WARNING: Heap fragmentation - largest block: %.0f bytes"
args: ['x']
loop_time:
id: loop_time
name: "Loop Time"
entity_category: diagnostic
# Alert if loop time exceeds 500ms (LVGL performance issue)
on_value_range:
- above: 500
then:
- logger.log:
level: WARN
format: "WARNING: Loop time exceeded 500ms: %.0f ms"
args: ['x']
# Critical alert for severe lag
- above: 1000
then:
- logger.log:
level: ERROR
format: "CRITICAL: Loop time exceeded 1000ms: %.0f ms - consider restart"
args: ['x']
- platform: uptime
id: uptime_sensor
name: "Uptime"
entity_category: diagnostic
update_interval: 60s
- platform: wifi_signal
id: wifi_signal_db
name: "WiFi Signal"
entity_category: diagnostic
update_interval: 60s
# --- SCD4x CO2/Temp/Humidity ---
# 30s update with low_power_periodic is optimal for continuous monitoring
- platform: scd4x
i2c_id: bus_a
co2:
id: co2_sensor
name: "CO2"
# Exponential Moving Average filter to reduce jitter on display
filters:
- exponential_moving_average:
alpha: 0.5 # Increased from 0.3 - faster response to actual changes
send_every: 1
# Clamp to reasonable range to prevent display overflow
- clamp:
min_value: 400
max_value: 5000
on_value:
# Consolidated update logic - fewer LVGL calls, more efficient
- lambda: |-
// Update CO2 value text
char buf[8];
snprintf(buf, sizeof(buf), "%.0f", x);
lv_label_set_text(id(co2_value), buf);
// Determine colors and status based on CO2 level
uint32_t text_color;
uint32_t bar_color;
const char* status;
if (x < 800) {
text_color = 0x4CAF50; // Green
bar_color = 0x1B5E20;
status = "GOOD";
} else if (x < 1200) {
text_color = 0xFFC107; // Amber
bar_color = 0xFF8F00;
status = "FAIR";
} else {
text_color = 0xF44336; // Red
bar_color = 0xB71C1C;
status = "POOR";
}
// Calculate bar value (0-100 scale, clamped at 2000 ppm)
int bar_val = (int)(std::min(x, 2000.0f) / 2000.0f * 100.0f);
// Apply all updates efficiently
lv_obj_set_style_text_color(id(co2_value), lv_color_hex(text_color), 0);
lv_bar_set_value(id(co2_bar), bar_val, LV_ANIM_ON);
lv_obj_set_style_bg_color(id(co2_bar), lv_color_hex(bar_color), LV_PART_INDICATOR);
lv_label_set_text(id(co2_status), status);
temperature:
id: temp_sensor
name: "Temperature"
filters:
- exponential_moving_average:
alpha: 0.5
send_every: 1
on_value:
- lvgl.label.update:
id: temp_value
text:
format: "%.1f°"
args: ['x']
humidity:
id: humidity_sensor
name: "Humidity"
filters:
- exponential_moving_average:
alpha: 0.5
send_every: 1
on_value:
- lvgl.label.update:
id: humidity_value
text:
format: "%.0f%%"
args: ['x']
# Sensor configuration
update_interval: 30s
automatic_self_calibration: true
measurement_mode: low_power_periodic
altitude_compensation: 20m
# Temperature offset: Adjust based on actual measurements
# 6°C is typical for enclosed sensors
temperature_offset: 6.0
# --- SGP4x VOC ---
- platform: sgp4x
i2c_id: bus_a
voc:
id: voc_sensor
name: "VOC Index"
filters:
- exponential_moving_average:
alpha: 0.2 # Decreased from 0.3 - VOC index is inherently noisy
send_every: 1
- clamp:
min_value: 0
max_value: 500
on_value:
- lvgl.label.update:
id: voc_value
text:
format: "%.0f"
args: ['x']
# VOC color coding (100 = normal baseline)
- if:
condition:
lambda: 'return x <= 150;'
then:
- lvgl.widget.update:
id: voc_value
text_color: 0x4CAF50
else:
- if:
condition:
lambda: 'return x <= 250;'
then:
- lvgl.widget.update:
id: voc_value
text_color: 0xFFC107
else:
- lvgl.widget.update:
id: voc_value
text_color: 0xF44336
update_interval: 30s
compensation:
temperature_source: temp_sensor
humidity_source: humidity_sensor
store_baseline: true
# ============================================================================
# BINARY SENSORS
# ============================================================================
binary_sensor:
- platform: status
id: system_status
name: "System Status"
# ============================================================================
# TEXT SENSORS
# ============================================================================
text_sensor:
- platform: wifi_info
ip_address:
name: "IP Address"
entity_category: diagnostic
ssid:
name: "Connected SSID"
entity_category: diagnostic
mac_address:
name: "MAC Address"
entity_category: diagnostic
# Device information for troubleshooting
- platform: debug
device:
name: "Device Info"
entity_category: diagnostic
reset_reason:
name: "Reset Reason"
entity_category: diagnostic
# ============================================================================
# BUTTONS
# ============================================================================
button:
- platform: restart
id: restart_button
name: "Restart"
- platform: safe_mode
name: "Restart (Safe Mode)"
# ============================================================================
# SWITCHES - Anti Burn-in Control
# ============================================================================
switch:
- platform: template
name: "Anti Burn-in"
id: switch_antiburn
icon: mdi:television-shimmer
optimistic: true
entity_category: config
# Turn ON: Start the snow/static effect to exercise pixels
turn_on_action:
- logger.log: "Starting anti-burn-in routine (white noise)"
# Resume first if already paused (edge case)
- if:
condition: lvgl.is_paused
then:
- lvgl.resume:
- lvgl.widget.redraw:
# Pause LVGL with snow effect - random colored pixels
- lvgl.pause:
show_snow: true
# Turn OFF: Stop the effect and restore normal display
turn_off_action:
- logger.log: "Stopping anti-burn-in routine"
- if:
condition: lvgl.is_paused
then:
- lvgl.resume:
- lvgl.widget.redraw:
- light.turn_on:
id: backlight
brightness: 100%
# ============================================================================
# NUMBER - Adjustable settings from Home Assistant
# ============================================================================
number:
- platform: template
name: "Display Brightness"
id: display_brightness
icon: mdi:brightness-percent
min_value: 10
max_value: 100
step: 10
initial_value: 100
restore_value: true
unit_of_measurement: "%"
optimistic: true
on_value:
then:
- light.turn_on:
id: backlight
brightness: !lambda 'return x / 100.0;'
# ============================================================================
# LVGL CONFIGURATION - FLAT HIERARCHY FOR NO PSRAM
# ============================================================================
# Memory Budget:
# Buffer: 320 × 240 × 2 bytes × 25% = 38,400 bytes
# Per widget: ~100-200 bytes (labels), ~200-400 bytes (complex)
# Estimated widget total: ~3KB for this layout
# Total LVGL footprint: ~45-50KB
# ============================================================================
lvgl:
log_level: WARN
# 25% buffer - optimal for NO PSRAM per ESPHome docs
buffer_size: 25%
# Solid background color (no gradients)
disp_bg_color: 0x303030
# Draw completion tracking for stability monitoring
on_draw_end:
- lambda: |-
// Track draw performance in debug builds
// Uncomment for debugging display issues:
// static uint32_t last_draw = 0;
// ESP_LOGD("lvgl", "Draw completed in %lu ms", millis() - last_draw);
// last_draw = millis();
# Touch configuration
touchscreens:
- touchscreen_id: main_touchscreen
long_press_time: 400ms
# Screen timeout - dim after 60s idle
on_idle:
- timeout: 60s
then:
- light.turn_on:
id: backlight
brightness: 50%
- timeout: 300s
then:
- light.turn_on:
id: backlight
brightness: 20%
resume_on_input: true
# =========================================================================
# FLAT WIDGET LAYOUT - Zero nested obj containers
# All widgets placed directly on page with absolute positioning
# =========================================================================
pages:
- id: main_page
bg_color: 0x0D1117
widgets:
# ----- HEADER BAR (simple rectangle via styling) -----
- label:
id: header_title
x: 8
y: 6
text: "Air Quality"
text_font: montserrat_14
text_color: 0xA0A0A0
- label:
id: clock_label
x: 248
y: 6
width: 64
text_align: RIGHT
text: "00:00"
text_font: montserrat_14
text_color: 0x8B949E
# ----- CO2 MAIN DISPLAY -----
# Large 48pt value for glanceability
- label:
id: co2_label
x: 8
y: 32
text: "CO2"
text_font: montserrat_12
text_color: 0x8B949E
- label:
id: co2_value
x: 8
y: 48
width: 180
text: "---"
text_font: montserrat_48
text_color: 0x4CAF50
- label:
id: co2_unit
x: 8
y: 105
text: "ppm"
text_font: montserrat_12
text_color: 0x6E7681
- label:
id: co2_status
x: 260
y: 70
width: 52
text_align: CENTER
text: "---"
text_font: montserrat_14
text_color: 0xFFFFFF
# CO2 Bar indicator - MORE MEMORY EFFICIENT than arc
# Bar uses ~200 bytes vs Arc ~400 bytes
# Also simpler visual, easier to read at a glance
- bar:
id: co2_bar
x: 8
y: 125
width: 304
height: 12
min_value: 0
max_value: 100
value: 0
radius: 6
bg_color: 0x21262D
border_width: 0
indicator:
bg_color: 0x1B5E20
radius: 6
# ----- SECONDARY READINGS ROW -----
# Temperature
- label:
x: 8
y: 148
text: "Temp"
text_font: montserrat_10
text_color: 0x808080
- label:
id: temp_value
x: 8
y: 162
width: 90
text: "--.-°"
text_font: montserrat_24
text_color: 0xFF7B54
# Humidity
- label:
x: 115
y: 148
text: "Humidity"
text_font: montserrat_10
text_color: 0x808080
- label:
id: humidity_value
x: 115
y: 162
width: 90
text: "--%"
text_font: montserrat_24
text_color: 0x54B4FF
# VOC
- label:
x: 222
y: 148
text: "VOC"
text_font: montserrat_10
text_color: 0x8B949E
- label:
id: voc_value
x: 222
y: 162
width: 90
text: "---"
text_font: montserrat_24
text_color: 0xB388FF
# ============================================================================
# INTERVALS - Minimal for stability
# ============================================================================
interval:
# Clock update - once per minute
- interval: 1min
then:
- lvgl.label.update:
id: clock_label
text:
format: "%02d:%02d"
args: ['id(sntp_time).now().hour', 'id(sntp_time).now().minute']
# Memory monitoring - every 5 minutes
- interval: 5min
then:
- lambda: |-
float heap = id(free_heap).state;
float block = id(largest_free_block).state;
float uptime_hrs = id(uptime_sensor).state / 3600.0;
ESP_LOGI("monitor", "Heap: %.0f, Block: %.0f, Uptime: %.1fh",
heap, block, uptime_hrs);
# Nightly LCD burn-in prevention (2 AM for 15 minutes)
# Exercises pixels to prevent image persistence on 24/7 display
- interval: 1h
then:
- if:
condition:
lambda: |-
auto time = id(sntp_time).now();
return time.is_valid() &&
time.hour == 2 &&
time.minute == 0;
then:
- switch.turn_on: switch_antiburn
- delay: 15min
- switch.turn_off: switch_antiburn