mirror of
https://github.com/esphome/esphome.git
synced 2024-12-28 17:37:44 +01:00
jsn_sr04t component: Add RCWL-1655 module compatibility
This commit is contained in:
parent
b32078a5fe
commit
584334fa15
@ -10,23 +10,47 @@ namespace jsn_sr04t {
|
|||||||
static const char *const TAG = "jsn_sr04t.sensor";
|
static const char *const TAG = "jsn_sr04t.sensor";
|
||||||
|
|
||||||
void Jsnsr04tComponent::update() {
|
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");
|
ESP_LOGV(TAG, "Request read out from sensor");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Jsnsr04tComponent::loop() {
|
void Jsnsr04tComponent::loop() {
|
||||||
while (this->available() > 0) {
|
uint8_t data;
|
||||||
uint8_t data;
|
if (this->model_ == RCWL_1655) {
|
||||||
this->read_byte(&data);
|
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)
|
if (this->buffer_.size() == 3) {
|
||||||
continue;
|
this->parse_buffer_rclw_1655_();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
while (this->available() > 0) {
|
||||||
|
uint8_t data;
|
||||||
|
this->read_byte(&data);
|
||||||
|
|
||||||
this->buffer_.push_back(data);
|
ESP_LOGV(TAG, "Read byte from sensor: %02X", data);
|
||||||
if (this->buffer_.size() == 4)
|
|
||||||
this->check_buffer_();
|
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();
|
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() {
|
void Jsnsr04tComponent::dump_config() {
|
||||||
LOG_SENSOR("", "JST_SR04T Sensor", this);
|
LOG_SENSOR("", "JST_SR04T Sensor", this);
|
||||||
switch (this->model_) {
|
switch (this->model_) {
|
||||||
@ -65,6 +98,9 @@ void Jsnsr04tComponent::dump_config() {
|
|||||||
case AJ_SR04M:
|
case AJ_SR04M:
|
||||||
ESP_LOGCONFIG(TAG, " sensor model: aj_sr04m");
|
ESP_LOGCONFIG(TAG, " sensor model: aj_sr04m");
|
||||||
break;
|
break;
|
||||||
|
case RCWL_1655:
|
||||||
|
ESP_LOGCONFIG(TAG, " sensor model: RCWL-1655");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
LOG_UPDATE_INTERVAL(this);
|
LOG_UPDATE_INTERVAL(this);
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ namespace jsn_sr04t {
|
|||||||
enum Model {
|
enum Model {
|
||||||
JSN_SR04T,
|
JSN_SR04T,
|
||||||
AJ_SR04M,
|
AJ_SR04M,
|
||||||
|
RCWL_1655,
|
||||||
};
|
};
|
||||||
|
|
||||||
class Jsnsr04tComponent : public sensor::Sensor, public PollingComponent, public uart::UARTDevice {
|
class Jsnsr04tComponent : public sensor::Sensor, public PollingComponent, public uart::UARTDevice {
|
||||||
@ -25,6 +26,7 @@ class Jsnsr04tComponent : public sensor::Sensor, public PollingComponent, public
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
void check_buffer_();
|
void check_buffer_();
|
||||||
|
void parse_buffer_rclw_1655_();
|
||||||
Model model_;
|
Model model_;
|
||||||
|
|
||||||
std::vector<uint8_t> buffer_;
|
std::vector<uint8_t> buffer_;
|
||||||
|
@ -19,6 +19,7 @@ Model = jsn_sr04t_ns.enum("Model")
|
|||||||
MODEL = {
|
MODEL = {
|
||||||
"jsn_sr04t": Model.JSN_SR04T,
|
"jsn_sr04t": Model.JSN_SR04T,
|
||||||
"aj_sr04m": Model.AJ_SR04M,
|
"aj_sr04m": Model.AJ_SR04M,
|
||||||
|
"rcwl_1655": Model.RCWL_1655,
|
||||||
}
|
}
|
||||||
|
|
||||||
CONFIG_SCHEMA = (
|
CONFIG_SCHEMA = (
|
||||||
|
Loading…
Reference in New Issue
Block a user