mirror of
https://github.com/esphome/esphome.git
synced 2024-11-23 15:38:11 +01:00
Change component name to aj_sr04m
This commit is contained in:
parent
b2fd02256f
commit
b304d25336
5 changed files with 43 additions and 37 deletions
32
esphome/components/aj_sr04m/aj_sr04m.cpp
Normal file
32
esphome/components/aj_sr04m/aj_sr04m.cpp
Normal 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
|
|
@ -5,9 +5,9 @@
|
|||
#include "esphome/components/uart/uart.h"
|
||||
|
||||
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:
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
|
@ -17,5 +17,5 @@ class UltrasonicSensorUart : public sensor::Sensor, public PollingComponent, pub
|
|||
float get_setup_priority() const override;
|
||||
};
|
||||
|
||||
} // namespace ultrasonic_uart
|
||||
} // namespace aj_sr04m
|
||||
} // namespace esphome
|
|
@ -3,16 +3,16 @@ import esphome.config_validation as cv
|
|||
from esphome.components import sensor, uart
|
||||
from esphome.const import (
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
UNIT_CENTIMETER,
|
||||
UNIT_METER,
|
||||
ICON_ARROW_EXPAND_VERTICAL,
|
||||
DEVICE_CLASS_DISTANCE,
|
||||
)
|
||||
|
||||
DEPENDENCIES = ["uart"]
|
||||
|
||||
ultrasonic_uart_ns = cg.esphome_ns.namespace("ultrasonic_uart")
|
||||
UltrasonicSensorUart = ultrasonic_uart_ns.class_(
|
||||
"UltrasonicSensorUart",
|
||||
aj_sr04m_ns = cg.esphome_ns.namespace("aj_sr04m")
|
||||
Ajsr04mComponent = aj_sr04m_ns.class_(
|
||||
"Ajsr04mComponent",
|
||||
sensor.Sensor,
|
||||
cg.PollingComponent,
|
||||
uart.UARTDevice,
|
||||
|
@ -20,10 +20,10 @@ UltrasonicSensorUart = ultrasonic_uart_ns.class_(
|
|||
|
||||
CONFIG_SCHEMA = (
|
||||
sensor.sensor_schema(
|
||||
UltrasonicSensorUart,
|
||||
unit_of_measurement=UNIT_CENTIMETER,
|
||||
Ajsr04mComponent,
|
||||
unit_of_measurement=UNIT_METER,
|
||||
icon=ICON_ARROW_EXPAND_VERTICAL,
|
||||
accuracy_decimals=2,
|
||||
accuracy_decimals=3,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
device_class=DEVICE_CLASS_DISTANCE,
|
||||
)
|
||||
|
@ -32,7 +32,7 @@ CONFIG_SCHEMA = (
|
|||
)
|
||||
|
||||
FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema(
|
||||
"ultrasonic_uart",
|
||||
"aj_sr04m",
|
||||
baud_rate=9600,
|
||||
require_tx=True,
|
||||
require_rx=True,
|
|
@ -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
|
Loading…
Reference in a new issue