Add new Component: Ultrasonic Distance Sensor JSN-SR04T (#6023)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Mafus1 2024-03-27 02:14:23 +01:00 committed by GitHub
parent 58de8a4ee6
commit 94e9476838
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 216 additions and 0 deletions

View File

@ -173,6 +173,7 @@ esphome/components/inkplate6/* @jesserockz
esphome/components/integration/* @OttoWinter
esphome/components/internal_temperature/* @Mat931
esphome/components/interval/* @esphome/core
esphome/components/jsn_sr04t/* @Mafus1
esphome/components/json/* @OttoWinter
esphome/components/kamstrup_kmp/* @cfeenstra1024
esphome/components/key_collector/* @ssieb

View File

@ -0,0 +1 @@
CODEOWNERS = ["@Mafus1"]

View File

@ -0,0 +1,58 @@
#include "jsn_sr04t.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include <cinttypes>
// Very basic support for JSN_SR04T V3.0 distance sensor in mode 2
namespace esphome {
namespace jsn_sr04t {
static const char *const TAG = "jsn_sr04t.sensor";
void Jsnsr04tComponent::update() {
this->write_byte(0x55);
ESP_LOGV(TAG, "Request read out from sensor");
}
void Jsnsr04tComponent::loop() {
while (this->available() > 0) {
uint8_t data;
this->read_byte(&data);
ESP_LOGV(TAG, "Read byte from sensor: %x", data);
if (this->buffer_.empty() && data != 0xFF)
continue;
this->buffer_.push_back(data);
if (this->buffer_.size() == 4)
this->check_buffer_();
}
}
void Jsnsr04tComponent::check_buffer_() {
uint8_t checksum = this->buffer_[0] + this->buffer_[1] + this->buffer_[2];
if (this->buffer_[3] == checksum) {
uint16_t distance = encode_uint16(this->buffer_[1], this->buffer_[2]);
if (distance > 250) {
float meters = distance / 1000.0f;
ESP_LOGV(TAG, "Distance from sensor: %" PRIu32 "mm, %.3fm", distance, meters);
this->publish_state(meters);
} 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 Jsnsr04tComponent::dump_config() {
LOG_SENSOR("", "JST_SR04T Sensor", this);
LOG_UPDATE_INTERVAL(this);
}
} // namespace jsn_sr04t
} // namespace esphome

View File

@ -0,0 +1,28 @@
#pragma once
#include <vector>
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/uart/uart.h"
namespace esphome {
namespace jsn_sr04t {
class Jsnsr04tComponent : public sensor::Sensor, public PollingComponent, public uart::UARTDevice {
public:
// Nothing really public.
// ========== INTERNAL METHODS ==========
void update() override;
void loop() override;
void dump_config() override;
protected:
void check_buffer_();
std::vector<uint8_t> buffer_;
};
} // namespace jsn_sr04t
} // namespace esphome

View File

@ -0,0 +1,44 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, uart
from esphome.const import (
STATE_CLASS_MEASUREMENT,
UNIT_METER,
ICON_ARROW_EXPAND_VERTICAL,
)
CODEOWNERS = ["@Mafus1"]
DEPENDENCIES = ["uart"]
jsn_sr04t_ns = cg.esphome_ns.namespace("jsn_sr04t")
Jsnsr04tComponent = jsn_sr04t_ns.class_(
"Jsnsr04tComponent", sensor.Sensor, cg.PollingComponent, uart.UARTDevice
)
CONFIG_SCHEMA = (
sensor.sensor_schema(
Jsnsr04tComponent,
unit_of_measurement=UNIT_METER,
icon=ICON_ARROW_EXPAND_VERTICAL,
accuracy_decimals=3,
state_class=STATE_CLASS_MEASUREMENT,
)
.extend(cv.polling_component_schema("60s"))
.extend(uart.UART_DEVICE_SCHEMA)
)
FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema(
"jsn_sr04t",
baud_rate=9600,
require_tx=True,
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)

View File

@ -0,0 +1,14 @@
uart:
- id: uart_jsn_sr04t
tx_pin:
number: 4
rx_pin:
number: 5
baud_rate: 9600
sensor:
- platform: jsn_sr04t
id: jsn_sr04t_sensor
name: "jsn_sr04t Distance"
uart_id: uart_jsn_sr04t
update_interval: 1s

View File

@ -0,0 +1,14 @@
uart:
- id: uart_jsn_sr04t
tx_pin:
number: 4
rx_pin:
number: 5
baud_rate: 9600
sensor:
- platform: jsn_sr04t
id: jsn_sr04t_sensor
name: "jsn_sr04t Distance"
uart_id: uart_jsn_sr04t
update_interval: 1s

View File

@ -0,0 +1,14 @@
uart:
- id: uart_jsn_sr04t
tx_pin:
number: 17
rx_pin:
number: 16
baud_rate: 9600
sensor:
- platform: jsn_sr04t
id: jsn_sr04t_sensor
name: "jsn_sr04t Distance"
uart_id: uart_jsn_sr04t
update_interval: 1s

View File

@ -0,0 +1,14 @@
uart:
- id: uart_jsn_sr04t
tx_pin:
number: 17
rx_pin:
number: 16
baud_rate: 9600
sensor:
- platform: jsn_sr04t
id: jsn_sr04t_sensor
name: "jsn_sr04t Distance"
uart_id: uart_jsn_sr04t
update_interval: 1s

View File

@ -0,0 +1,14 @@
uart:
- id: uart_jsn_sr04t
tx_pin:
number: 4
rx_pin:
number: 5
baud_rate: 9600
sensor:
- platform: jsn_sr04t
id: jsn_sr04t_sensor
name: "jsn_sr04t Distance"
uart_id: uart_jsn_sr04t
update_interval: 1s

View File

@ -0,0 +1,14 @@
uart:
- id: uart_jsn_sr04t
tx_pin:
number: 4
rx_pin:
number: 5
baud_rate: 9600
sensor:
- platform: jsn_sr04t
id: jsn_sr04t_sensor
name: "jsn_sr04t Distance"
uart_id: uart_jsn_sr04t
update_interval: 1s