mirror of
https://github.com/esphome/esphome.git
synced 2024-11-21 14:38:10 +01:00
A02yyuw (#5729)
This commit is contained in:
parent
c6dc336c4a
commit
259a6d52e1
6 changed files with 126 additions and 0 deletions
|
@ -12,6 +12,7 @@ esphome/core/* @esphome/core
|
|||
|
||||
# Integrations
|
||||
esphome/components/a01nyub/* @MrSuicideParrot
|
||||
esphome/components/a02yyuw/* @TH-Braemer
|
||||
esphome/components/absolute_humidity/* @DAVe3283
|
||||
esphome/components/ac_dimmer/* @glmnet
|
||||
esphome/components/adc/* @esphome/core
|
||||
|
|
1
esphome/components/a02yyuw/__init__.py
Normal file
1
esphome/components/a02yyuw/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
CODEOWNERS = ["@TH-Braemer"]
|
43
esphome/components/a02yyuw/a02yyuw.cpp
Normal file
43
esphome/components/a02yyuw/a02yyuw.cpp
Normal file
|
@ -0,0 +1,43 @@
|
|||
// Datasheet https://wiki.dfrobot.com/_A02YYUW_Waterproof_Ultrasonic_Sensor_SKU_SEN0311
|
||||
|
||||
#include "a02yyuw.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace a02yyuw {
|
||||
|
||||
static const char *const TAG = "a02yyuw.sensor";
|
||||
|
||||
void A02yyuwComponent::loop() {
|
||||
uint8_t data;
|
||||
while (this->available() > 0) {
|
||||
this->read_byte(&data);
|
||||
if (this->buffer_.empty() && (data != 0xff))
|
||||
continue;
|
||||
buffer_.push_back(data);
|
||||
if (this->buffer_.size() == 4)
|
||||
this->check_buffer_();
|
||||
}
|
||||
}
|
||||
|
||||
void A02yyuwComponent::check_buffer_() {
|
||||
uint8_t checksum = this->buffer_[0] + this->buffer_[1] + this->buffer_[2];
|
||||
if (this->buffer_[3] == checksum) {
|
||||
float distance = (this->buffer_[1] << 8) + this->buffer_[2];
|
||||
if (distance > 30) {
|
||||
ESP_LOGV(TAG, "Distance from sensor: %f mm", distance);
|
||||
this->publish_state(distance);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Invalid data read from sensor: %s", format_hex_pretty(this->buffer_).c_str());
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(TAG, "checksum failed: %02x != %02x", checksum, this->buffer_[3]);
|
||||
}
|
||||
this->buffer_.clear();
|
||||
}
|
||||
|
||||
void A02yyuwComponent::dump_config() { LOG_SENSOR("", "A02yyuw Sensor", this); }
|
||||
|
||||
} // namespace a02yyuw
|
||||
} // namespace esphome
|
27
esphome/components/a02yyuw/a02yyuw.h
Normal file
27
esphome/components/a02yyuw/a02yyuw.h
Normal file
|
@ -0,0 +1,27 @@
|
|||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/uart/uart.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace a02yyuw {
|
||||
|
||||
class A02yyuwComponent : public sensor::Sensor, public Component, public uart::UARTDevice {
|
||||
public:
|
||||
// Nothing really public.
|
||||
|
||||
// ========== INTERNAL METHODS ==========
|
||||
void loop() override;
|
||||
void dump_config() override;
|
||||
|
||||
protected:
|
||||
void check_buffer_();
|
||||
|
||||
std::vector<uint8_t> buffer_;
|
||||
};
|
||||
|
||||
} // namespace a02yyuw
|
||||
} // namespace esphome
|
41
esphome/components/a02yyuw/sensor.py
Normal file
41
esphome/components/a02yyuw/sensor.py
Normal file
|
@ -0,0 +1,41 @@
|
|||
import esphome.codegen as cg
|
||||
from esphome.components import sensor, uart
|
||||
from esphome.const import (
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
ICON_ARROW_EXPAND_VERTICAL,
|
||||
DEVICE_CLASS_DISTANCE,
|
||||
)
|
||||
|
||||
CODEOWNERS = ["@TH-Braemer"]
|
||||
DEPENDENCIES = ["uart"]
|
||||
UNIT_MILLIMETERS = "mm"
|
||||
|
||||
a02yyuw_ns = cg.esphome_ns.namespace("a02yyuw")
|
||||
A02yyuwComponent = a02yyuw_ns.class_(
|
||||
"A02yyuwComponent", sensor.Sensor, cg.Component, uart.UARTDevice
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = sensor.sensor_schema(
|
||||
A02yyuwComponent,
|
||||
unit_of_measurement=UNIT_MILLIMETERS,
|
||||
icon=ICON_ARROW_EXPAND_VERTICAL,
|
||||
accuracy_decimals=0,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
device_class=DEVICE_CLASS_DISTANCE,
|
||||
).extend(uart.UART_DEVICE_SCHEMA)
|
||||
|
||||
FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema(
|
||||
"a02yyuw",
|
||||
baud_rate=9600,
|
||||
require_tx=False,
|
||||
require_rx=True,
|
||||
data_bits=8,
|
||||
parity=None,
|
||||
stop_bits=1,
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = await sensor.new_sensor(config)
|
||||
await cg.register_component(var, config)
|
||||
await uart.register_uart_device(var, config)
|
|
@ -84,6 +84,14 @@ uart:
|
|||
allow_other_uses: true
|
||||
number: GPIO26
|
||||
baud_rate: 9600
|
||||
- id: uart_a02yyuw
|
||||
tx_pin:
|
||||
allow_other_uses: true
|
||||
number: GPIO22
|
||||
rx_pin:
|
||||
allow_other_uses: true
|
||||
number: GPIO23
|
||||
baud_rate: 9600
|
||||
- id: uart_he60r
|
||||
tx_pin:
|
||||
number: GPIO18
|
||||
|
@ -357,6 +365,11 @@ sensor:
|
|||
name: "a01nyub Distance"
|
||||
uart_id: uart9600
|
||||
state_topic: "esphome/sensor/a01nyub_sensor/state"
|
||||
- platform: a02yyuw
|
||||
id: a02yyuw_sensor
|
||||
name: "a02yyuw Distance"
|
||||
uart_id: uart_a02yyuw
|
||||
state_topic: "esphome/sensor/a02yyuw_sensor/state"
|
||||
|
||||
#
|
||||
# platform sensor.apds9960 requires component apds9960
|
||||
|
|
Loading…
Reference in a new issue