mirror of
https://github.com/esphome/esphome.git
synced 2024-12-21 21:14:52 +01:00
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:
parent
58de8a4ee6
commit
94e9476838
11 changed files with 216 additions and 0 deletions
|
@ -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
|
||||
|
|
1
esphome/components/jsn_sr04t/__init__.py
Normal file
1
esphome/components/jsn_sr04t/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
CODEOWNERS = ["@Mafus1"]
|
58
esphome/components/jsn_sr04t/jsn_sr04t.cpp
Normal file
58
esphome/components/jsn_sr04t/jsn_sr04t.cpp
Normal 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
|
28
esphome/components/jsn_sr04t/jsn_sr04t.h
Normal file
28
esphome/components/jsn_sr04t/jsn_sr04t.h
Normal 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
|
44
esphome/components/jsn_sr04t/sensor.py
Normal file
44
esphome/components/jsn_sr04t/sensor.py
Normal 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)
|
14
tests/components/jsn_sr04t/test.esp32-c3-idf.yaml
Normal file
14
tests/components/jsn_sr04t/test.esp32-c3-idf.yaml
Normal 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
|
14
tests/components/jsn_sr04t/test.esp32-c3.yaml
Normal file
14
tests/components/jsn_sr04t/test.esp32-c3.yaml
Normal 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
|
14
tests/components/jsn_sr04t/test.esp32-idf.yaml
Normal file
14
tests/components/jsn_sr04t/test.esp32-idf.yaml
Normal 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
|
14
tests/components/jsn_sr04t/test.esp32.yaml
Normal file
14
tests/components/jsn_sr04t/test.esp32.yaml
Normal 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
|
14
tests/components/jsn_sr04t/test.esp8266.yaml
Normal file
14
tests/components/jsn_sr04t/test.esp8266.yaml
Normal 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
|
14
tests/components/jsn_sr04t/test.rp2040.yaml
Normal file
14
tests/components/jsn_sr04t/test.rp2040.yaml
Normal 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
|
Loading…
Reference in a new issue