From 584334fa15b1ad6e993fd128c879ecc22688a54b Mon Sep 17 00:00:00 2001 From: Andreas Frisch Date: Sun, 14 Jul 2024 19:56:22 +0200 Subject: [PATCH] jsn_sr04t component: Add RCWL-1655 module compatibility --- esphome/components/jsn_sr04t/jsn_sr04t.cpp | 56 ++++++++++++++++++---- esphome/components/jsn_sr04t/jsn_sr04t.h | 2 + esphome/components/jsn_sr04t/sensor.py | 1 + 3 files changed, 49 insertions(+), 10 deletions(-) diff --git a/esphome/components/jsn_sr04t/jsn_sr04t.cpp b/esphome/components/jsn_sr04t/jsn_sr04t.cpp index 077d4e58ea..13b5a330ef 100644 --- a/esphome/components/jsn_sr04t/jsn_sr04t.cpp +++ b/esphome/components/jsn_sr04t/jsn_sr04t.cpp @@ -10,23 +10,47 @@ namespace jsn_sr04t { static const char *const TAG = "jsn_sr04t.sensor"; void Jsnsr04tComponent::update() { - this->write_byte(0x55); + switch (this->model_) { + case JSN_SR04T: + case AJ_SR04M: + this->write_byte(0x55); + break; + case RCWL_1655: + this->buffer_.clear(); + this->write_byte(0xA0); + break; + } ESP_LOGV(TAG, "Request read out from sensor"); } void Jsnsr04tComponent::loop() { - while (this->available() > 0) { - uint8_t data; - this->read_byte(&data); + uint8_t data; + if (this->model_ == RCWL_1655) { + while (this->available() > 0) { + this->read_byte(&data); - ESP_LOGV(TAG, "Read byte from sensor: %x", data); + ESP_LOGV(TAG, "Read byte [%d] from sensor: %02X", this->buffer_.size(), data); + this->buffer_.push_back(data); - if (this->buffer_.empty() && data != 0xFF) - continue; + if (this->buffer_.size() == 3) { + this->parse_buffer_rclw_1655_(); + } + } + } else { + while (this->available() > 0) { + uint8_t data; + this->read_byte(&data); - this->buffer_.push_back(data); - if (this->buffer_.size() == 4) - this->check_buffer_(); + ESP_LOGV(TAG, "Read byte from sensor: %02X", data); + + if (this->buffer_.empty() && data != 0xFF) + continue; + + this->buffer_.push_back(data); + if (this->buffer_.size() == 4) { + this->check_buffer_(); + } + } } } @@ -56,6 +80,15 @@ void Jsnsr04tComponent::check_buffer_() { this->buffer_.clear(); } +void Jsnsr04tComponent::parse_buffer_rclw_1655_() { + uint32_t distance = encode_uint24(this->buffer_[0], this->buffer_[1], this->buffer_[2]); + float millimeters = distance / 1000.0f; + float meters = millimeters / 1000.0f; + ESP_LOGV(TAG, "Distance from sensor: %.0fmm, %.3fm", millimeters, meters); + this->publish_state(meters); + this->buffer_.clear(); +} + void Jsnsr04tComponent::dump_config() { LOG_SENSOR("", "JST_SR04T Sensor", this); switch (this->model_) { @@ -65,6 +98,9 @@ void Jsnsr04tComponent::dump_config() { case AJ_SR04M: ESP_LOGCONFIG(TAG, " sensor model: aj_sr04m"); break; + case RCWL_1655: + ESP_LOGCONFIG(TAG, " sensor model: RCWL-1655"); + break; } LOG_UPDATE_INTERVAL(this); } diff --git a/esphome/components/jsn_sr04t/jsn_sr04t.h b/esphome/components/jsn_sr04t/jsn_sr04t.h index 2a22ff92ec..ec628704ff 100644 --- a/esphome/components/jsn_sr04t/jsn_sr04t.h +++ b/esphome/components/jsn_sr04t/jsn_sr04t.h @@ -12,6 +12,7 @@ namespace jsn_sr04t { enum Model { JSN_SR04T, AJ_SR04M, + RCWL_1655, }; class Jsnsr04tComponent : public sensor::Sensor, public PollingComponent, public uart::UARTDevice { @@ -25,6 +26,7 @@ class Jsnsr04tComponent : public sensor::Sensor, public PollingComponent, public protected: void check_buffer_(); + void parse_buffer_rclw_1655_(); Model model_; std::vector buffer_; diff --git a/esphome/components/jsn_sr04t/sensor.py b/esphome/components/jsn_sr04t/sensor.py index 682cf06570..d9d1ec1d92 100644 --- a/esphome/components/jsn_sr04t/sensor.py +++ b/esphome/components/jsn_sr04t/sensor.py @@ -19,6 +19,7 @@ Model = jsn_sr04t_ns.enum("Model") MODEL = { "jsn_sr04t": Model.JSN_SR04T, "aj_sr04m": Model.AJ_SR04M, + "rcwl_1655": Model.RCWL_1655, } CONFIG_SCHEMA = (