Initial commit
This commit is contained in:
151
sensor.yaml
Normal file
151
sensor.yaml
Normal file
@ -0,0 +1,151 @@
|
||||
# ===================================================================
|
||||
# ESPHome 配置: 基于 ESP32 + ADS1015 + SSD1306 的压力监测器 (最终完整版)
|
||||
#
|
||||
# 修正了 ota 组件的配置格式。
|
||||
# ===================================================================
|
||||
|
||||
esphome:
|
||||
name: pressure-monitor
|
||||
|
||||
# -- 平台和板型配置 --
|
||||
esp32:
|
||||
board: esp32dev
|
||||
framework:
|
||||
type: arduino
|
||||
|
||||
# -- 标准网络连接配置 --
|
||||
wifi:
|
||||
ssid: "hhhhhhhh"
|
||||
password: "nihaoahhh"
|
||||
|
||||
# -- Home Assistant API, OTA 更新, 日志等 --
|
||||
api:
|
||||
|
||||
ota:
|
||||
platform: esphome # <-- 已修正:为 ota 添加 platform
|
||||
|
||||
logger:
|
||||
|
||||
# ===================================================================
|
||||
# I2C 总线配置
|
||||
# ===================================================================
|
||||
i2c:
|
||||
sda: 21
|
||||
scl: 22
|
||||
scan: true
|
||||
frequency: 400kHz
|
||||
|
||||
# ===================================================================
|
||||
# ADC 硬件组件配置
|
||||
# ===================================================================
|
||||
ads1115:
|
||||
id: ads1015_hub
|
||||
address: 0x48
|
||||
|
||||
# ===================================================================
|
||||
# 传感器实体配置
|
||||
# ===================================================================
|
||||
sensor:
|
||||
# -----------------------------------------------------------------
|
||||
# 步骤 1: 定义一个 ADS1115 传感器,读取电压 (Vs)
|
||||
# -----------------------------------------------------------------
|
||||
- platform: ads1115
|
||||
ads1115_id: ads1015_hub
|
||||
id: sensor_voltage
|
||||
name: "Sensor Voltage"
|
||||
multiplexer: 'A0_GND'
|
||||
gain: 4.096
|
||||
update_interval: 50ms
|
||||
unit_of_measurement: "V"
|
||||
icon: "mdi:flash-circle"
|
||||
accuracy_decimals: 3
|
||||
filters:
|
||||
- sliding_window_moving_average:
|
||||
window_size: 16
|
||||
send_every: 4
|
||||
|
||||
# -----------------------------------------------------------------
|
||||
# 步骤 2: 创建一个模板传感器来计算压力 (MPa)
|
||||
# -----------------------------------------------------------------
|
||||
- platform: template
|
||||
id: pressure_sensor
|
||||
name: "Pressure"
|
||||
unit_of_measurement: "MPa"
|
||||
icon: "mdi:gauge"
|
||||
accuracy_decimals: 3
|
||||
lambda: |-
|
||||
const float sensor_volts = id(sensor_voltage).state;
|
||||
if (isnan(sensor_volts)) {
|
||||
return NAN;
|
||||
}
|
||||
|
||||
const float SENSOR_MIN_V = 0.5f;
|
||||
const float SENSOR_MAX_V = 4.5f;
|
||||
const float PRESSURE_FULL_SCALE_MPA = 0.2f;
|
||||
const float span_v = SENSOR_MAX_V - SENSOR_MIN_V;
|
||||
|
||||
if (span_v <= 0.0f) {
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
float ratio = (sensor_volts - SENSOR_MIN_V) / span_v;
|
||||
|
||||
if (ratio < 0.0f) ratio = 0.0f;
|
||||
if (ratio > 1.0f) ratio = 1.0f;
|
||||
|
||||
return ratio * PRESSURE_FULL_SCALE_MPA;
|
||||
|
||||
# ===================================================================
|
||||
# 显示屏配置 (SSD1306 OLED)
|
||||
# ===================================================================
|
||||
font:
|
||||
- file: "gfonts://Roboto"
|
||||
id: roboto_font
|
||||
size: 12
|
||||
- file: "gfonts://Roboto"
|
||||
id: roboto_font_large
|
||||
size: 24
|
||||
|
||||
display:
|
||||
- platform: ssd1306_i2c
|
||||
id: oled_display
|
||||
model: "SSD1306 128x64"
|
||||
address: 0x3C
|
||||
update_interval: 200ms
|
||||
lambda: |-
|
||||
const float pressure = id(pressure_sensor).state;
|
||||
const float voltage = id(sensor_voltage).state;
|
||||
const bool sensor_ok = !isnan(pressure) && !isnan(voltage) && voltage > 0.1;
|
||||
|
||||
// 绘制标题栏
|
||||
it.filled_rectangle(0, 0, 128, 16, COLOR_ON);
|
||||
it.print(4, 4, id(roboto_font), COLOR_OFF, "PRESSURE");
|
||||
|
||||
if (sensor_ok) {
|
||||
it.print(128 - 22, 4, id(roboto_font), COLOR_OFF, "OK");
|
||||
} else {
|
||||
it.print(128 - 26, 4, id(roboto_font), COLOR_OFF, "ERR");
|
||||
}
|
||||
|
||||
if (!sensor_ok) {
|
||||
it.printf(it.get_width() / 2, 40, id(roboto_font), TextAlign::CENTER, "SENSOR ERR");
|
||||
return;
|
||||
}
|
||||
|
||||
// 绘制主读数
|
||||
it.printf(it.get_width() / 2, 34, id(roboto_font_large), TextAlign::CENTER, "%.3f", pressure);
|
||||
it.print(it.get_width() - 2, 44, id(roboto_font), TextAlign::TOP_RIGHT, "MPa");
|
||||
|
||||
// 绘制电压
|
||||
it.printf(4, 52, id(roboto_font), TextAlign::TOP_LEFT, "Vs: %.3f V", voltage);
|
||||
|
||||
// 绘制进度条
|
||||
const int bar_x = 4, bar_y = 64 - 8, bar_w = 128 - 8, bar_h = 6;
|
||||
it.rectangle(bar_x, bar_y, bar_w, bar_h, COLOR_ON);
|
||||
float ratio = pressure / 0.2f;
|
||||
if (ratio < 0.0f) ratio = 0.0f;
|
||||
if (ratio > 1.0f) ratio = 1.0f;
|
||||
int filled_w = (int)(ratio * (bar_w - 2));
|
||||
if (filled_w > 0) {
|
||||
it.filled_rectangle(bar_x + 1, bar_y + 1, filled_w, bar_h - 2, COLOR_ON);
|
||||
}
|
Reference in New Issue
Block a user