ESPHome 2026.11.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
14ESPHOME_LOG_TAG(TAG, "hrxl.maxsonar.wr.sensor");
15
23 this->read_available([this](uint8_t data) {
24 size_t len;
25 const char *line = this->line_reader_.feed(data, len);
26 if (line != nullptr) {
27 ESP_LOGV(TAG, "Read from serial: %s", line);
28 this->handle_line_(line, len);
29 } else if (this->line_reader_.overflowed()) {
30 // No CR in time: report it and start over with the next byte
31 ESP_LOGW(TAG, "Invalid data read from sensor: %s", this->line_reader_.partial());
32 this->line_reader_.reset();
33 }
34 });
35}
36
37void HrxlMaxsonarWrComponent::handle_line_(const char *line, size_t len) {
38 if (line[0] != 'R') {
39 ESP_LOGW(TAG, "Invalid data read from sensor: %s", line);
40 return;
41 }
42 int millimeters = parse_number<int>(line + 1).value_or(0);
43
44 // XL reports in cm instead of mm and reports 3 digits instead of 4
45 if (len == 4) {
46 millimeters *= 10;
47 }
48
49 float meters = float(millimeters) / 1000.0f;
50 ESP_LOGV(TAG, "Distance from sensor: %d mm, %f m", millimeters, meters);
51 this->publish_state(meters);
52}
53
55 ESP_LOGCONFIG(TAG, "HRXL MaxSonar WR Sensor:");
56 LOG_SENSOR(" ", "Distance", this);
57}
58
59} // namespace esphome::hrxl_maxsonar_wr
const char * partial()
The partial line, NUL terminated, for prompts with no line end; the first N - 1 characters on the byt...
Definition line_reader.h:60
bool overflowed() const
True only right after the feed() that found the line too long; the rest of that line is then dropped.
Definition line_reader.h:66
char * feed(uint8_t c)
Feed one byte; returns the completed line (empty lines included), valid until the next call,...
Definition line_reader.h:23
void reset()
Forget the partial line and start a new one with the next byte.
Definition line_reader.h:76
void loop() override
HRXL sensors output the format "R1234\r" at 6Hz The 1234 means 1234mm XL sensors output the format "R...
void handle_line_(const char *line, size_t len)
LineReader< 6, '\r', static_cast< char >(0xFF)> line_reader_
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
Definition uart.h:77
ESPHOME_LOG_TAG(TAG, "hrxl.maxsonar.wr.sensor")
const void size_t len
Definition hal.h:68
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:1352