ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
hydreon_rgxx.cpp
Go to the documentation of this file.
1#include "hydreon_rgxx.h"
2#include "esphome/core/log.h"
3#include <cstring>
4
6
7ESPHOME_LOG_TAG(TAG, "hydreon_rgxx.sensor");
8
9static constexpr uint32_t REBOOT_INTERVAL_ID = 0;
10
11#define HYDREON_RGXX_COMMA ,
12static const char *const PROTOCOL_NAMES[] = {HYDREON_RGXX_PROTOCOL_LIST(, HYDREON_RGXX_COMMA)};
13static const char *const IGNORE_STRINGS[] = {HYDREON_RGXX_IGNORE_LIST(, HYDREON_RGXX_COMMA)};
14
15void HydreonRGxxComponent::dump_config() {
16 ESP_LOGCONFIG(TAG, "hydreon_rgxx:");
17 if (model_ == RG9) {
18 ESP_LOGCONFIG(TAG,
19 " Model: RG9\n"
20 " Disable Led: %s",
21 TRUEFALSE(this->disable_led_));
22 } else {
23 ESP_LOGCONFIG(TAG, " Model: RG15");
24 if (this->resolution_ == FORCE_HIGH) {
25 ESP_LOGCONFIG(TAG, " Resolution: high");
26 } else {
27 ESP_LOGCONFIG(TAG, " Resolution: low");
28 }
29 }
30 LOG_UPDATE_INTERVAL(this);
31
32 int i = 0;
33#define HYDREON_RGXX_LOG_SENSOR(s) \
34 if (this->sensors_[i++] != nullptr) { \
35 LOG_SENSOR(" ", #s, this->sensors_[i - 1]); \
36 }
37 HYDREON_RGXX_PROTOCOL_LIST(HYDREON_RGXX_LOG_SENSOR, );
38}
39
40void HydreonRGxxComponent::setup() {
41 this->discard_available();
42 this->schedule_reboot_();
43}
44
46 if (this->sensors_received_ == -1) {
47 return -1;
48 }
49 int ret = NUM_SENSORS;
50 for (int i = 0; i < NUM_SENSORS; i++) {
51 if (this->sensors_[i] == nullptr) {
52 ret -= 1;
53 continue;
54 }
55 if ((this->sensors_received_ >> i & 1) != 0) {
56 ret -= 1;
57 }
58 }
59 return ret;
60}
61
62void HydreonRGxxComponent::update() {
63 if (this->boot_count_ > 0) {
64 if (this->num_sensors_missing_() > 0) {
65 for (int i = 0; i < NUM_SENSORS; i++) {
66 if (this->sensors_[i] == nullptr) {
67 continue;
68 }
69 if ((this->sensors_received_ >> i & 1) == 0) {
70 ESP_LOGW(TAG, "Missing %s", PROTOCOL_NAMES[i]);
71 }
72 }
73
74 this->no_response_count_++;
75 ESP_LOGE(TAG, "missing %d sensors; %d times in a row", this->num_sensors_missing_(), this->no_response_count_);
76 if (this->no_response_count_ > 15) {
77 ESP_LOGE(TAG, "asking sensor to reboot");
78 for (auto &sensor : this->sensors_) {
79 PUBLISH_STATE(sensor, NAN);
80 }
81 this->schedule_reboot_();
82 return;
83 }
84 } else {
85 this->no_response_count_ = 0;
86 }
87 this->write_str("R\n");
88#ifdef USE_BINARY_SENSOR
89 PUBLISH_STATE(this->too_cold_binary_sensor_, this->too_cold_);
90 PUBLISH_STATE(this->lens_bad_binary_sensor_, this->lens_bad_);
91 PUBLISH_STATE(this->em_sat_binary_sensor_, this->em_sat_);
92#endif
93 this->too_cold_ = false;
94 this->lens_bad_ = false;
95 this->em_sat_ = false;
96 this->sensors_received_ = 0;
97 }
98}
99
100void HydreonRGxxComponent::loop() {
101 this->read_available([this](uint8_t data) {
102 size_t len;
103 const char *line = this->line_reader_.feed(data, len);
105 if (line != nullptr) {
106 this->process_line_(StringRef(line, len));
107 }
108 });
109}
110
126 this->boot_count_ = 0;
127 this->set_interval(REBOOT_INTERVAL_ID, 5000, [this]() {
128 if (this->boot_count_ < 0) {
129 ESP_LOGW(TAG, "hydreon_rgxx failed to boot %d times", -this->boot_count_);
130 }
131 this->boot_count_--;
132 this->write_str("K\n");
133 if (this->boot_count_ < -5) {
134 ESP_LOGE(TAG, "hydreon_rgxx can't boot, giving up");
135 for (auto &sensor : this->sensors_) {
136 PUBLISH_STATE(sensor, NAN);
137 }
138 this->mark_failed(LOG_STR("Sensor did not boot"));
139 }
140 });
141}
142
144 ESP_LOGV(TAG, "Read from serial: %s", line.c_str());
145
146 if (line[0] == ';') {
147 ESP_LOGI(TAG, "Comment: %s", line.c_str());
148 return;
149 }
150 if (line.empty()) {
151 ESP_LOGD(TAG, "Received empty line");
152 return;
153 }
154 if (line.size() == 1) {
155 // single character lines, such as acknowledgements
156 ESP_LOGD(TAG, "Received ack: %s", line.c_str());
157 return;
158 }
159 if (line.find("LensBad") != std::string::npos) {
160 ESP_LOGW(TAG, "Received LensBad!");
161 this->lens_bad_ = true;
162 }
163 if (line.find("EmSat") != std::string::npos) {
164 ESP_LOGW(TAG, "Received EmSat!");
165 this->em_sat_ = true;
166 }
167 if (line.starts_with("PwrDays")) {
168 if (this->boot_count_ <= 0) {
169 this->boot_count_ = 1;
170 } else {
171 this->boot_count_++;
172 }
173 this->cancel_interval(REBOOT_INTERVAL_ID);
174 this->no_response_count_ = 0;
175 ESP_LOGI(TAG, "Boot detected: %s", line.c_str());
176
177 if (this->model_ == RG15) {
178 if (this->resolution_ == FORCE_HIGH) {
179 this->write_str("P\nH\nM\n"); // set sensor to (P)polling mode, (H)high res mode, (M)metric mode
180 } else {
181 this->write_str("P\nL\nM\n"); // set sensor to (P)polling mode, (L)low res mode, (M)metric mode
182 }
183 }
184
185 if (this->model_ == RG9) {
186 this->write_str("P\n"); // set sensor to (P)polling mode
187
188 if (this->disable_led_) {
189 this->write_str("D 1\n"); // set sensor (D 1)rain detection LED disabled
190 } else {
191 this->write_str("D 0\n"); // set sensor (D 0)rain detection LED enabled
192 }
193 }
194 return;
195 }
196 if (line.starts_with("SW")) {
197 StringRef::size_type majend = line.find('.');
198 StringRef::size_type endversion = line.find(' ', 3);
199 if (majend == std::string::npos || endversion == std::string::npos || majend > endversion) {
200 ESP_LOGW(TAG, "invalid version string: %s", line.c_str());
201 }
202 // strtol stops at the '.' and the ' '
203 int major = line.size() > 3 ? strtol(line.c_str() + 3, nullptr, 10) : 0;
204 int minor = strtol(line.c_str() + (majend == std::string::npos ? 0 : majend + 1), nullptr, 10);
205
206 if (major > 10 || minor >= 1000 || minor < 0 || major < 0) {
207 ESP_LOGW(TAG, "invalid version: %s", line.c_str());
208 }
209 this->sw_version_ = major * 1000 + minor;
210 ESP_LOGI(TAG, "detected sw version %i", this->sw_version_);
211 return;
212 }
213 bool is_data_line = false;
214 for (int i = 0; i < NUM_SENSORS; i++) {
215 if (this->sensors_[i] == nullptr) {
216 continue;
217 }
218 StringRef::size_type n = line.find(PROTOCOL_NAMES[i]);
219 if (n == std::string::npos) {
220 continue;
221 }
222 is_data_line = true;
223
224 if (n == line.find('t', n)) {
225 // The device temperature ('t') response contains both °C and °F values:
226 // "t 72F 22C".
227 // ESPHome uses only °C, only parse °C value (move past 'F').
228 n = line.find('F', n);
229 if (n == std::string::npos) {
230 continue;
231 }
232 n += 1; // move past 'F'
233 } else {
234 n += strlen(PROTOCOL_NAMES[i]); // move past protocol name
235 }
236
237 // parse value, starting at str position n
238 float data = strtof(line.c_str() + n, nullptr);
239 this->sensors_[i]->publish_state(data);
240 ESP_LOGD(TAG, "Received %s: %f", PROTOCOL_NAMES[i], this->sensors_[i]->get_raw_state());
241 this->sensors_received_ |= (1 << i);
242 }
243 if (is_data_line) {
244 StringRef::size_type tc = line.find("TooCold");
245 this->too_cold_ |= tc != std::string::npos;
246 if (this->too_cold_) {
247 ESP_LOGD(TAG, "Received TooCold");
248 }
249 if (this->request_temperature_ && this->num_sensors_missing_() == 1) {
250 this->write_str("T\n");
251 }
252 } else {
253 for (const auto *ignore : IGNORE_STRINGS) {
254 if (line.starts_with(ignore)) {
255 ESP_LOGI(TAG, "Ignoring %s", line.c_str());
256 return;
257 }
258 }
259 ESP_LOGI(TAG, "Got unknown line: %s", line.c_str());
260 }
261}
262
263} // namespace esphome::hydreon_rgxx
void mark_failed()
Mark this component as failed.
bool cancel_interval(const char *name)
Cancel an interval function.
Definition component.cpp:92
void set_interval(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a const char* name.
Definition component.cpp:88
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 log_if_overflowed(const char *tag) const
Log the shared "line dropped" warning when the last feed() overflowed.
Definition line_reader.h:69
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
constexpr const char * c_str() const
Definition string_ref.h:78
size_type find(const StringRef &s, size_type pos=0) const
Find first occurrence of a substring, returns std::string::npos if not found.
Definition string_ref.h:122
bool starts_with(const StringRef &prefix) const
True if the view begins with the given prefix (std::string::starts_with-like)
Definition string_ref.h:85
constexpr bool empty() const
Definition string_ref.h:81
constexpr size_type size() const
Definition string_ref.h:79
std::allocator_traits< allocator_type >::size_type size_type
Definition string_ref.h:36
void process_line_(StringRef line)
line is NUL terminated
void schedule_reboot_()
Communication with the sensor is asynchronous.
sensor::Sensor * sensors_[NUM_SENSORS]
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
void discard_available()
Read and drop every byte waiting in the receive buffer.
Definition uart.cpp:36
void write_str(const char *str)
Definition uart.h:37
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
Definition uart.h:77
int ret
ESPHOME_LOG_TAG(TAG, "hydreon_rgxx.sensor")
const void size_t len
Definition hal.h:68
static void uint32_t