ESPHome  2024.12.2
hrxl_maxsonar_wr.cpp
Go to the documentation of this file.
1 // Official Datasheet:
2 // HRXL: https://www.maxbotix.com/documents/HRXL-MaxSonar-WR_Datasheet.pdf
3 // XL: https://www.maxbotix.com/documents/XL-MaxSonar-WR_Datasheet.pdf
4 //
5 // This implementation is designed to work with the TTL Versions of the
6 // MaxBotix HRXL and XL MaxSonar WR sensor series. The sensor's TTL Pin (5)
7 // should be wired to one of the ESP's input pins and configured as uart rx_pin.
8 
9 #include "hrxl_maxsonar_wr.h"
10 #include "esphome/core/log.h"
11 
12 namespace esphome {
13 namespace hrxl_maxsonar_wr {
14 
15 static const char *const TAG = "hrxl.maxsonar.wr.sensor";
16 static const uint8_t ASCII_CR = 0x0D;
17 static const uint8_t ASCII_NBSP = 0xFF;
18 static const int MAX_DATA_LENGTH_BYTES = 6;
19 
27  uint8_t data;
28  while (this->available() > 0) {
29  if (this->read_byte(&data)) {
30  buffer_ += (char) data;
31  this->check_buffer_();
32  }
33  }
34 }
35 
37  // The sensor seems to inject a rogue ASCII 255 byte from time to time. Get rid of that.
38  if (this->buffer_.back() == static_cast<char>(ASCII_NBSP)) {
39  this->buffer_.pop_back();
40  return;
41  }
42 
43  // Stop reading at ASCII_CR. Also prevent the buffer from growing
44  // indefinitely if no ASCII_CR is received after MAX_DATA_LENGTH_BYTES.
45  if (this->buffer_.back() == static_cast<char>(ASCII_CR) || this->buffer_.length() >= MAX_DATA_LENGTH_BYTES) {
46  ESP_LOGV(TAG, "Read from serial: %s", this->buffer_.c_str());
47 
48  size_t rpos = this->buffer_.find(static_cast<char>(ASCII_CR));
49 
50  if (this->buffer_.length() <= MAX_DATA_LENGTH_BYTES && this->buffer_[0] == 'R' && rpos != std::string::npos) {
51  std::string distance = this->buffer_.substr(1, rpos - 1);
52  int millimeters = parse_number<int>(distance).value_or(0);
53 
54  // XL reports in cm instead of mm and reports 3 digits instead of 4
55  if (distance.length() == 3) {
56  millimeters = millimeters * 10;
57  }
58 
59  float meters = float(millimeters) / 1000.0;
60  ESP_LOGV(TAG, "Distance from sensor: %d mm, %f m", millimeters, meters);
61  this->publish_state(meters);
62  } else {
63  ESP_LOGW(TAG, "Invalid data read from sensor: %s", this->buffer_.c_str());
64  }
65  this->buffer_.clear();
66  }
67 }
68 
70  ESP_LOGCONFIG(TAG, "HRXL MaxSonar WR Sensor:");
71  LOG_SENSOR(" ", "Distance", this);
72  // As specified in the sensor's data sheet
74 }
75 
76 } // namespace hrxl_maxsonar_wr
77 } // namespace esphome
void loop() override
HRXL sensors output the format "R1234\r" at 6Hz The 1234 means 1234mm XL sensors output the format "R...
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits=1, UARTParityOptions parity=UART_CONFIG_PARITY_NONE, uint8_t data_bits=8)
Check that the configuration of the UART bus matches the provided values and otherwise print a warnin...
Definition: uart.cpp:13
bool read_byte(uint8_t *data)
Definition: uart.h:29
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7