Change component name to aj_sr04m

This commit is contained in:
Masterz69 2024-04-19 13:25:25 +03:00
parent b2fd02256f
commit b304d25336
5 changed files with 43 additions and 37 deletions

View file

@ -0,0 +1,32 @@
#include "aj_sr04m.h"
#include "esphome/core/log.h"
namespace esphome {
namespace aj_sr04m {
static const char *const TAG = "aj_sr04m.sensor";
void Ajsr04mComponent::setup() { ESP_LOGCONFIG(TAG, "Setting up AJ_SR04M Sensor..."); }
void Ajsr04mComponent::update() {
this->write(0x55);
ESP_LOGV(TAG, "Request read out from sensor");
while (this->available() == 4) {
auto frame = *this->read_array<4>();
auto checksum = (frame[0] + frame[1] + frame[2]) & 0x00FF;
if ((frame[0] == 0xFF) && (checksum == frame[3])) {
float value = ((frame[1] << 8) + frame[2]) / 1000.0;
this->publish_state(value);
} else {
ESP_LOGW(TAG, "checksum failed: %02x != %02x", checksum, frame[3]);
}
}
}
void Ajsr04mComponent::dump_config() {
LOG_SENSOR("", "AJ_SR04M Sensor", this);
LOG_UPDATE_INTERVAL(this);
}
float Ajsr04mComponent::get_setup_priority() const { return setup_priority::DATA; }
} // namespace aj_sr04m
} // namespace esphome

View file

@ -5,9 +5,9 @@
#include "esphome/components/uart/uart.h" #include "esphome/components/uart/uart.h"
namespace esphome { namespace esphome {
namespace ultrasonic_uart { namespace aj_sr04m {
class UltrasonicSensorUart : public sensor::Sensor, public PollingComponent, public uart::UARTDevice { class Ajsr04mComponent : public sensor::Sensor, public PollingComponent, public uart::UARTDevice {
public: public:
void setup() override; void setup() override;
void dump_config() override; void dump_config() override;
@ -17,5 +17,5 @@ class UltrasonicSensorUart : public sensor::Sensor, public PollingComponent, pub
float get_setup_priority() const override; float get_setup_priority() const override;
}; };
} // namespace ultrasonic_uart } // namespace aj_sr04m
} // namespace esphome } // namespace esphome

View file

@ -3,16 +3,16 @@ import esphome.config_validation as cv
from esphome.components import sensor, uart from esphome.components import sensor, uart
from esphome.const import ( from esphome.const import (
STATE_CLASS_MEASUREMENT, STATE_CLASS_MEASUREMENT,
UNIT_CENTIMETER, UNIT_METER,
ICON_ARROW_EXPAND_VERTICAL, ICON_ARROW_EXPAND_VERTICAL,
DEVICE_CLASS_DISTANCE, DEVICE_CLASS_DISTANCE,
) )
DEPENDENCIES = ["uart"] DEPENDENCIES = ["uart"]
ultrasonic_uart_ns = cg.esphome_ns.namespace("ultrasonic_uart") aj_sr04m_ns = cg.esphome_ns.namespace("aj_sr04m")
UltrasonicSensorUart = ultrasonic_uart_ns.class_( Ajsr04mComponent = aj_sr04m_ns.class_(
"UltrasonicSensorUart", "Ajsr04mComponent",
sensor.Sensor, sensor.Sensor,
cg.PollingComponent, cg.PollingComponent,
uart.UARTDevice, uart.UARTDevice,
@ -20,10 +20,10 @@ UltrasonicSensorUart = ultrasonic_uart_ns.class_(
CONFIG_SCHEMA = ( CONFIG_SCHEMA = (
sensor.sensor_schema( sensor.sensor_schema(
UltrasonicSensorUart, Ajsr04mComponent,
unit_of_measurement=UNIT_CENTIMETER, unit_of_measurement=UNIT_METER,
icon=ICON_ARROW_EXPAND_VERTICAL, icon=ICON_ARROW_EXPAND_VERTICAL,
accuracy_decimals=2, accuracy_decimals=3,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
device_class=DEVICE_CLASS_DISTANCE, device_class=DEVICE_CLASS_DISTANCE,
) )
@ -32,7 +32,7 @@ CONFIG_SCHEMA = (
) )
FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema( FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema(
"ultrasonic_uart", "aj_sr04m",
baud_rate=9600, baud_rate=9600,
require_tx=True, require_tx=True,
require_rx=True, require_rx=True,

View file

@ -1,26 +0,0 @@
#include "ultrasonic_sensor.h"
#include "esphome/core/log.h"
namespace esphome {
namespace ultrasonic_uart {
static const char *const TAG = "ultrasonic_uart";
void UltrasonicSensorUart::setup() { ESP_LOGCONFIG(TAG, "Setting up Ultrasonic Sensor..."); }
void UltrasonicSensorUart::update() {
this->write(0x55);
while (this->available() == 4) {
auto frame = *this->read_array<4>();
if ((frame[0] == 0xFF) && (((frame[0] + frame[1] + frame[2]) & 0x00FF) == frame[3])) {
float value = ((frame[1] << 8) + frame[2]) / 10.0;
this->publish_state(value);
}
}
}
void UltrasonicSensorUart::dump_config() {
LOG_SENSOR("", "Ultrasonic Sensor", this);
LOG_UPDATE_INTERVAL(this);
}
float UltrasonicSensorUart::get_setup_priority() const { return setup_priority::DATA; }
} // namespace ultrasonic_uart
} // namespace esphome