implement status LEDs

This commit is contained in:
Cyberes 2024-09-15 21:57:37 -06:00
parent 881c31d067
commit e54363e9cf
5 changed files with 101 additions and 6 deletions

View File

@ -12,6 +12,14 @@ My [geiger counter from RH Electronics](https://www.rhelectronics.store/arduino-
You'll need to use a logic level shifter to convert the 5v signals to 3.3v.
## Configuration
The status LEDs are configured using a ranging approach. The green LED turns on when
the uSv reading is below the configured `safe_level`. When the uSv reading is between
`safe_level` and `warning_level`, the yellow LED turns on. All readings above `warning_level`
trigger the red LED.
## Example HA Config for Statistics
```yaml

View File

@ -9,29 +9,74 @@ static const char *TAG = "geiger_counter_sensor.sensor";
void GeigerCounterSensor::setup() {
ESP_LOGCONFIG(TAG, "Setting up Geiger Counter Sensor...");
ESP_LOGCONFIG(TAG, "Setting up Geiger Counter Sensor...");
if (green_led_pin_ != nullptr) {
green_led_pin_->setup();
green_led_pin_->digital_write(false);
}
if (yellow_led_pin_ != nullptr) {
yellow_led_pin_->setup();
yellow_led_pin_->digital_write(false);
}
if (red_led_pin_ != nullptr) {
red_led_pin_->setup();
red_led_pin_->digital_write(false);
}
}
void GeigerCounterSensor::loop() {
const int max_line_length = 80;
static char buffer[max_line_length];
while (available()) {
if(readline(read(), buffer, max_line_length) > 0) {
if (readline(read(), buffer, max_line_length) > 0) {
int cpm = atof(buffer);
float usv = cpm * 0.0057;
ESP_LOGCONFIG(TAG, "Received radiation level: %d cpm, %.2f μSv", cpm, usv);
this->publish_state(usv);
if (usv < safe_level_) {
if (green_led_pin_ != nullptr) {
green_led_pin_->digital_write(true);
}
if (yellow_led_pin_ != nullptr) {
yellow_led_pin_->digital_write(false);
}
if (red_led_pin_ != nullptr) {
red_led_pin_->digital_write(false);
}
} else if (usv >= safe_level_ && usv < warning_level_) {
if (green_led_pin_ != nullptr) {
green_led_pin_->digital_write(false);
}
if (yellow_led_pin_ != nullptr) {
yellow_led_pin_->digital_write(true);
}
if (red_led_pin_ != nullptr) {
red_led_pin_->digital_write(false);
}
} else {
if (green_led_pin_ != nullptr) {
green_led_pin_->digital_write(false);
}
if (yellow_led_pin_ != nullptr) {
yellow_led_pin_->digital_write(false);
}
if (red_led_pin_ != nullptr) {
red_led_pin_->digital_write(true);
}
}
}
}
}
void GeigerCounterSensor::dump_config() {
LOG_SENSOR("", "Geiger Counter Sensor", this);
}
void GeigerCounterSensor::update() {
}
} // namespace geiger_counter_sensor

View File

@ -2,7 +2,7 @@
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/uart/uart.h"
#include "esphome/core/gpio.h"
namespace esphome {
namespace geiger_counter_sensor {
@ -14,6 +14,18 @@ class GeigerCounterSensor : public sensor::Sensor, public PollingComponent, publ
void update() override;
void loop() override;
void dump_config() override;
void set_green_led_pin(GPIOPin *pin) { green_led_pin_ = pin; }
void set_yellow_led_pin(GPIOPin *pin) { yellow_led_pin_ = pin; }
void set_red_led_pin(GPIOPin *pin) { red_led_pin_ = pin; }
void set_safe_level(float safe_level) { safe_level_ = safe_level; }
void set_warning_level(float warning_level) { warning_level_ = warning_level; }
private:
GPIOPin *green_led_pin_{nullptr};
GPIOPin *yellow_led_pin_{nullptr};
GPIOPin *red_led_pin_{nullptr};
float safe_level_{0.35};
float warning_level_{0.7};
};
} // namespace geiger_counter_sensor

View File

@ -2,6 +2,7 @@ import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import uart, sensor
from esphome.const import ICON_RADIATOR, UNIT_EMPTY
from esphome import pins
DEPENDENCIES = ["uart"]
@ -10,6 +11,12 @@ GeigerCounterSensor = geiger_counter_sensor_ns.class_(
"GeigerCounterSensor", cg.PollingComponent, uart.UARTDevice
)
CONF_GREEN_LED_PIN = "green_led_pin"
CONF_YELLOW_LED_PIN = "yellow_led_pin"
CONF_RED_LED_PIN = "red_led_pin"
CONF_SAFE_LEVEL = "safe_level"
CONF_WARNING_LEVEL = "warning_level"
CONFIG_SCHEMA = (
sensor.sensor_schema(
GeigerCounterSensor,
@ -19,6 +26,13 @@ CONFIG_SCHEMA = (
)
.extend(cv.polling_component_schema("60s"))
.extend(uart.UART_DEVICE_SCHEMA)
.extend({
cv.Required(CONF_GREEN_LED_PIN): pins.gpio_output_pin_schema,
cv.Required(CONF_YELLOW_LED_PIN): pins.gpio_output_pin_schema,
cv.Required(CONF_RED_LED_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_SAFE_LEVEL, default=0.35): cv.float_,
cv.Optional(CONF_WARNING_LEVEL, default=0.7): cv.float_,
})
)
@ -26,3 +40,15 @@ async def to_code(config):
var = await sensor.new_sensor(config)
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
green_led_pin = await cg.gpio_pin_expression(config[CONF_GREEN_LED_PIN])
cg.add(var.set_green_led_pin(green_led_pin))
yellow_led_pin = await cg.gpio_pin_expression(config[CONF_YELLOW_LED_PIN])
cg.add(var.set_yellow_led_pin(yellow_led_pin))
red_led_pin = await cg.gpio_pin_expression(config[CONF_RED_LED_PIN])
cg.add(var.set_red_led_pin(red_led_pin))
cg.add(var.set_safe_level(config[CONF_SAFE_LEVEL]))
cg.add(var.set_warning_level(config[CONF_WARNING_LEVEL]))

View File

@ -13,4 +13,8 @@ uart:
sensor:
- platform: geiger_counter_sensor
name: geiger-counter
# safe_rads: 55
green_led_pin: GPIO0
yellow_led_pin: GPIO4
red_led_pin: GPIO5
safe_level: 0.35
warning_level: 0.7