add files

This commit is contained in:
Cyberes 2024-09-13 18:22:15 -06:00
parent 7770d8e00a
commit 185c469dfd
6 changed files with 148 additions and 0 deletions

View File

@ -1,2 +1,16 @@
# esphome-geiger-counter # esphome-geiger-counter
- Wired/UART logs don't work. Use wireless logs.
- Only uSv is sent.
## Example HA Config for Statistics
```yaml
- platform: statistics
name: "Radiation (μSv)"
unique_id: geiger_counter
entity_id: sensor.geiger_counter_raw
state_characteristic: mean
max_age:
minutes: 1
```

View File

@ -0,0 +1,68 @@
#include "geiger_counter_sensor.h"
#include "esphome/core/log.h"
#include "esphome.h"
namespace esphome {
namespace geiger_counter_sensor {
static const char *TAG = "geiger_counter_sensor.sensor";
void GeigerCounterSensor::setup() {
ESP_LOGCONFIG(TAG, "Setting up Geiger Counter Sensor...");
}
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) {
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);
}
}
}
void GeigerCounterSensor::dump_config() {
LOG_SENSOR("", "Geiger Counter Sensor", this);
}
void GeigerCounterSensor::update() {
}
} // namespace geiger_counter_sensor
} // namespace esphome
int readline(int readch, char *buffer, int len) {
static int pos = 0;
int rpos;
if (readch > 0) {
switch (readch) {
case '\n':
case '\r': // Return on CR or newline
buffer[pos] = 0; // Just to be sure, set last character 0
rpos = pos;
pos = 0; // Reset position index ready for next time
return rpos;
default:
if ((pos < len-1) && ( readch < 127 )) { // Filter on <127 to make sure it is a character
buffer[pos++] = readch;
buffer[pos] = 0;
}
else
{
buffer[pos] = 0; // Just to be sure, set last character 0
rpos = pos;
pos = 0; // Reset position index ready for next time
return rpos;
}
}
}
// No end of line has been found, so return -1.
return -1;
}

View File

@ -0,0 +1,22 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/uart/uart.h"
namespace esphome {
namespace geiger_counter_sensor {
class GeigerCounterSensor : public sensor::Sensor, public PollingComponent, public uart::UARTDevice {
public:
void setup() override;
void update() override;
void loop() override;
void dump_config() override;
};
} // namespace geiger_counter_sensor
} // namespace esphome
int readline(int readch, char *buffer, int len);

View File

@ -0,0 +1,28 @@
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
DEPENDENCIES = ["uart"]
geiger_counter_sensor_ns = cg.esphome_ns.namespace("geiger_counter_sensor")
GeigerCounterSensor = geiger_counter_sensor_ns.class_(
"GeigerCounterSensor", cg.PollingComponent, uart.UARTDevice
)
CONFIG_SCHEMA = (
sensor.sensor_schema(
GeigerCounterSensor,
unit_of_measurement="μSv",
icon='mdi:radioactive-circle',
accuracy_decimals=2,
)
.extend(cv.polling_component_schema("60s"))
.extend(uart.UART_DEVICE_SCHEMA)
)
async def to_code(config):
var = await sensor.new_sensor(config)
await cg.register_component(var, config)
await uart.register_uart_device(var, config)

16
geiger_counter.yaml Normal file
View File

@ -0,0 +1,16 @@
external_components:
- source:
type: git
url: https://git.evulid.cc/cyberes/esphome-geiger-counter.git
ref: master
refresh: 0s
uart:
tx_pin: GPIO1
rx_pin: GPIO3
baud_rate: 9600
sensor:
- platform: geiger_counter_sensor
name: geiger-counter
# safe_rads: 55