5 namespace hydreon_rgxx {
7 static const char *
const TAG =
"hydreon_rgxx.sensor";
8 static const int MAX_DATA_LENGTH_BYTES = 80;
9 static const uint8_t ASCII_LF = 0x0A;
10 #define HYDREON_RGXX_COMMA , 11 static const char *
const PROTOCOL_NAMES[] = {HYDREON_RGXX_PROTOCOL_LIST(, HYDREON_RGXX_COMMA)};
12 static const char *
const IGNORE_STRINGS[] = {HYDREON_RGXX_IGNORE_LIST(, HYDREON_RGXX_COMMA)};
16 ESP_LOGCONFIG(TAG,
"hydreon_rgxx:");
18 ESP_LOGE(TAG,
"Connection with hydreon_rgxx failed!");
20 LOG_UPDATE_INTERVAL(
this);
23 #define HYDREON_RGXX_LOG_SENSOR(s) \ 24 if (this->sensors_[i++] != nullptr) { \ 25 LOG_SENSOR(" ", #s, this->sensors_[i - 1]); \ 27 HYDREON_RGXX_PROTOCOL_LIST(HYDREON_RGXX_LOG_SENSOR, );
31 ESP_LOGCONFIG(TAG,
"Setting up hydreon_rgxx...");
42 int ret = NUM_SENSORS;
43 for (
int i = 0; i < NUM_SENSORS; i++) {
58 for (
int i = 0; i < NUM_SENSORS; i++) {
63 ESP_LOGW(TAG,
"Missing %s", PROTOCOL_NAMES[i]);
70 ESP_LOGE(TAG,
"asking sensor to reboot");
71 for (
auto &sensor : this->
sensors_) {
72 if (sensor !=
nullptr) {
73 sensor->publish_state(NAN);
83 #ifdef USE_BINARY_SENSOR 106 if (this->
buffer_.back() ==
static_cast<char>(ASCII_LF) || this->
buffer_.length() >= MAX_DATA_LENGTH_BYTES) {
133 ESP_LOGW(TAG,
"hydreon_rgxx failed to boot %d times", -this->
boot_count_);
138 ESP_LOGE(TAG,
"hydreon_rgxx can't boot, giving up");
139 for (
auto &sensor : this->
sensors_) {
140 if (sensor !=
nullptr) {
141 sensor->publish_state(NAN);
156 ESP_LOGV(TAG,
"Read from serial: %s", this->
buffer_.substr(0, this->buffer_.size() - 2).c_str());
159 ESP_LOGI(TAG,
"Comment: %s", this->
buffer_.substr(0, this->buffer_.size() - 2).c_str());
162 std::string::size_type newlineposn = this->
buffer_.find(
'\n');
163 if (newlineposn <= 1) {
165 ESP_LOGD(TAG,
"Received empty line");
168 if (newlineposn <= 2) {
170 ESP_LOGD(TAG,
"Received ack: %s", this->
buffer_.substr(0, this->buffer_.size() - 2).c_str());
173 if (this->
buffer_.find(
"LensBad") != std::string::npos) {
174 ESP_LOGW(TAG,
"Received LensBad!");
177 if (this->
buffer_.find(
"EmSat") != std::string::npos) {
178 ESP_LOGW(TAG,
"Received EmSat!");
189 ESP_LOGI(TAG,
"Boot detected: %s", this->
buffer_.substr(0, this->buffer_.size() - 2).c_str());
194 std::string::size_type majend = this->
buffer_.find(
'.');
195 std::string::size_type endversion = this->
buffer_.find(
' ', 3);
196 if (majend == std::string::npos || endversion == std::string::npos || majend > endversion) {
197 ESP_LOGW(TAG,
"invalid version string: %s", this->
buffer_.substr(0, this->buffer_.size() - 2).c_str());
199 int major = strtol(this->
buffer_.substr(3, majend - 3).c_str(),
nullptr, 10);
200 int minor = strtol(this->
buffer_.substr(majend + 1, endversion - (majend + 1)).c_str(),
nullptr, 10);
202 if (major > 10 || minor >= 1000 || minor < 0 || major < 0) {
203 ESP_LOGW(TAG,
"invalid version: %s", this->
buffer_.substr(0, this->buffer_.size() - 2).c_str());
206 ESP_LOGI(TAG,
"detected sw version %i", this->sw_version_);
209 bool is_data_line =
false;
210 for (
int i = 0; i < NUM_SENSORS; i++) {
217 std::string::size_type tc = this->
buffer_.find(
"TooCold");
218 this->
too_cold_ |= tc != std::string::npos;
220 ESP_LOGD(TAG,
"Received TooCold");
222 for (
int i = 0; i < NUM_SENSORS; i++) {
226 std::string::size_type n = this->
buffer_.find(PROTOCOL_NAMES[i]);
227 if (n == std::string::npos) {
230 float data = strtof(this->
buffer_.substr(n + strlen(PROTOCOL_NAMES[i])).c_str(),
nullptr);
232 ESP_LOGD(TAG,
"Received %s: %f", PROTOCOL_NAMES[i], this->
sensors_[i]->get_raw_state());
239 for (
const auto *ignore : IGNORE_STRINGS) {
241 ESP_LOGI(TAG,
"Ignoring %s", this->
buffer_.substr(0, this->buffer_.size() - 2).c_str());
245 ESP_LOGI(TAG,
"Got unknown line: %s", this->
buffer_.c_str());
void write_str(const char *str)
const float DATA
For components that import data from directly connected sensors like DHT.
void set_interval(const std::string &name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
float get_setup_priority() const override
void dump_config() override
binary_sensor::BinarySensor * too_cold_sensor_
binary_sensor::BinarySensor * lens_bad_sensor_
bool cancel_interval(const std::string &name)
Cancel an interval function.
void setup() override
Setup the sensor and test for a connection.
void schedule_reboot_()
Communication with the sensor is asynchronous.
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...
int num_sensors_missing_()
bool read_byte(uint8_t *data)
void publish_state(float state)
Publish a new state to the front-end.
void publish_state(bool state)
Publish a new state to the front-end.
bool request_temperature_
virtual void mark_failed()
Mark this component as failed.
sensor::Sensor * sensors_[NUM_SENSORS]
binary_sensor::BinarySensor * em_sat_sensor_
void loop() override
Read data once available.
bool buffer_starts_with_(const std::string &prefix)
void update() override
Schedule data readings.
int16_t no_response_count_