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!");
21 ESP_LOGCONFIG(TAG,
" Model: RG9");
22 ESP_LOGCONFIG(TAG,
" Disable Led: %s", TRUEFALSE(this->
disable_led_));
24 ESP_LOGCONFIG(TAG,
" Model: RG15");
26 ESP_LOGCONFIG(TAG,
" Resolution: high");
28 ESP_LOGCONFIG(TAG,
" Resolution: low");
31 LOG_UPDATE_INTERVAL(
this);
34 #define HYDREON_RGXX_LOG_SENSOR(s) \ 35 if (this->sensors_[i++] != nullptr) { \ 36 LOG_SENSOR(" ", #s, this->sensors_[i - 1]); \ 38 HYDREON_RGXX_PROTOCOL_LIST(HYDREON_RGXX_LOG_SENSOR, );
42 ESP_LOGCONFIG(TAG,
"Setting up hydreon_rgxx...");
53 int ret = NUM_SENSORS;
54 for (
int i = 0; i < NUM_SENSORS; i++) {
69 for (
int i = 0; i < NUM_SENSORS; i++) {
74 ESP_LOGW(TAG,
"Missing %s", PROTOCOL_NAMES[i]);
81 ESP_LOGE(TAG,
"asking sensor to reboot");
94 #ifdef USE_BINARY_SENSOR 117 if (this->
buffer_.back() ==
static_cast<char>(ASCII_LF) || this->
buffer_.length() >= MAX_DATA_LENGTH_BYTES) {
144 ESP_LOGW(TAG,
"hydreon_rgxx failed to boot %d times", -this->
boot_count_);
149 ESP_LOGE(TAG,
"hydreon_rgxx can't boot, giving up");
167 ESP_LOGV(TAG,
"Read from serial: %s", this->
buffer_.substr(0, this->buffer_.size() - 2).c_str());
170 ESP_LOGI(TAG,
"Comment: %s", this->
buffer_.substr(0, this->buffer_.size() - 2).c_str());
173 std::string::size_type newlineposn = this->
buffer_.find(
'\n');
174 if (newlineposn <= 1) {
176 ESP_LOGD(TAG,
"Received empty line");
179 if (newlineposn <= 2) {
181 ESP_LOGD(TAG,
"Received ack: %s", this->
buffer_.substr(0, this->buffer_.size() - 2).c_str());
184 if (this->
buffer_.find(
"LensBad") != std::string::npos) {
185 ESP_LOGW(TAG,
"Received LensBad!");
188 if (this->
buffer_.find(
"EmSat") != std::string::npos) {
189 ESP_LOGW(TAG,
"Received EmSat!");
200 ESP_LOGI(TAG,
"Boot detected: %s", this->
buffer_.substr(0, this->buffer_.size() - 2).c_str());
222 std::string::size_type majend = this->
buffer_.find(
'.');
223 std::string::size_type endversion = this->
buffer_.find(
' ', 3);
224 if (majend == std::string::npos || endversion == std::string::npos || majend > endversion) {
225 ESP_LOGW(TAG,
"invalid version string: %s", this->
buffer_.substr(0, this->buffer_.size() - 2).c_str());
227 int major = strtol(this->
buffer_.substr(3, majend - 3).c_str(),
nullptr, 10);
228 int minor = strtol(this->
buffer_.substr(majend + 1, endversion - (majend + 1)).c_str(),
nullptr, 10);
230 if (major > 10 || minor >= 1000 || minor < 0 || major < 0) {
231 ESP_LOGW(TAG,
"invalid version: %s", this->
buffer_.substr(0, this->buffer_.size() - 2).c_str());
234 ESP_LOGI(TAG,
"detected sw version %i", this->sw_version_);
237 bool is_data_line =
false;
238 for (
int i = 0; i < NUM_SENSORS; i++) {
239 if (this->
sensors_[i] !=
nullptr && this->
buffer_.find(PROTOCOL_NAMES[i]) != std::string::npos) {
245 std::string::size_type tc = this->
buffer_.find(
"TooCold");
246 this->
too_cold_ |= tc != std::string::npos;
248 ESP_LOGD(TAG,
"Received TooCold");
250 for (
int i = 0; i < NUM_SENSORS; i++) {
254 std::string::size_type n = this->
buffer_.find(PROTOCOL_NAMES[i]);
255 if (n == std::string::npos) {
259 if (n == this->
buffer_.find(
't', n)) {
263 n = this->
buffer_.find(
'F', n);
264 if (n == std::string::npos) {
269 n += strlen(PROTOCOL_NAMES[i]);
273 float data = strtof(this->
buffer_.substr(n).c_str(),
nullptr);
275 ESP_LOGD(TAG,
"Received %s: %f", PROTOCOL_NAMES[i], this->
sensors_[i]->get_raw_state());
282 for (
const auto *ignore : IGNORE_STRINGS) {
284 ESP_LOGI(TAG,
"Ignoring %s", this->
buffer_.substr(0, this->buffer_.size() - 2).c_str());
288 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.
RG15Resolution resolution_
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]
Implementation of SPI Controller mode.
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.
esphome::sensor::Sensor * sensor
int16_t no_response_count_