ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
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
13
14static const char *const TAG = "hrxl.maxsonar.wr.sensor";
15static const uint8_t ASCII_CR = 0x0D;
16static const uint8_t ASCII_NBSP = 0xFF;
17static const int MAX_DATA_LENGTH_BYTES = 6;
18
26 uint8_t data;
27 while (this->available() > 0) {
28 if (this->read_byte(&data)) {
29 buffer_ += (char) data;
30 this->check_buffer_();
31 }
32 }
33}
34
36 // The sensor seems to inject a rogue ASCII 255 byte from time to time. Get rid of that.
37 if (this->buffer_.back() == static_cast<char>(ASCII_NBSP)) {
38 this->buffer_.pop_back();
39 return;
40 }
41
42 // Stop reading at ASCII_CR. Also prevent the buffer from growing
43 // indefinitely if no ASCII_CR is received after MAX_DATA_LENGTH_BYTES.
44 if (this->buffer_.back() == static_cast<char>(ASCII_CR) || this->buffer_.length() >= MAX_DATA_LENGTH_BYTES) {
45 ESP_LOGV(TAG, "Read from serial: %s", this->buffer_.c_str());
46
47 size_t rpos = this->buffer_.find(static_cast<char>(ASCII_CR));
48
49 if (this->buffer_.length() <= MAX_DATA_LENGTH_BYTES && this->buffer_[0] == 'R' && rpos != std::string::npos) {
50 std::string distance = this->buffer_.substr(1, rpos - 1);
51 int millimeters = parse_number<int>(distance).value_or(0);
52
53 // XL reports in cm instead of mm and reports 3 digits instead of 4
54 if (distance.length() == 3) {
55 millimeters = millimeters * 10;
56 }
57
58 float meters = float(millimeters) / 1000.0;
59 ESP_LOGV(TAG, "Distance from sensor: %d mm, %f m", millimeters, meters);
60 this->publish_state(meters);
61 } else {
62 ESP_LOGW(TAG, "Invalid data read from sensor: %s", this->buffer_.c_str());
63 }
64 this->buffer_.clear();
65 }
66}
67
69 ESP_LOGCONFIG(TAG, "HRXL MaxSonar WR Sensor:");
70 LOG_SENSOR(" ", "Distance", this);
71 // As specified in the sensor's data sheet
73}
74
75} // namespace esphome::hrxl_maxsonar_wr
void loop() override
HRXL sensors output the format "R1234\r" at 6Hz The 1234 means 1234mm XL sensors output the format "R...
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
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:16
bool read_byte(uint8_t *data)
Definition uart.h:34
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:1143