ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
radon_eye_rd200.cpp
Go to the documentation of this file.
1#include "radon_eye_rd200.h"
3
4#include <cstring>
5
6#ifdef USE_ESP32
7
9
10static const char *const TAG = "radon_eye_rd200";
11
12static const esp32_ble_tracker::ESPBTUUID SERVICE_UUID_V1 =
13 esp32_ble_tracker::ESPBTUUID::from_raw("00001523-1212-efde-1523-785feabcd123");
14static const esp32_ble_tracker::ESPBTUUID WRITE_CHARACTERISTIC_UUID_V1 =
15 esp32_ble_tracker::ESPBTUUID::from_raw("00001524-1212-efde-1523-785feabcd123");
16static const esp32_ble_tracker::ESPBTUUID READ_CHARACTERISTIC_UUID_V1 =
17 esp32_ble_tracker::ESPBTUUID::from_raw("00001525-1212-efde-1523-785feabcd123");
18static const uint8_t WRITE_COMMAND_V1 = 0x50;
19
20static const esp32_ble_tracker::ESPBTUUID SERVICE_UUID_V2 =
21 esp32_ble_tracker::ESPBTUUID::from_raw("00001523-0000-1000-8000-00805f9b34fb");
22static const esp32_ble_tracker::ESPBTUUID WRITE_CHARACTERISTIC_UUID_V2 =
23 esp32_ble_tracker::ESPBTUUID::from_raw("00001524-0000-1000-8000-00805f9b34fb");
24static const esp32_ble_tracker::ESPBTUUID READ_CHARACTERISTIC_UUID_V2 =
25 esp32_ble_tracker::ESPBTUUID::from_raw("00001525-0000-1000-8000-00805f9b34fb");
26static const uint8_t WRITE_COMMAND_V2 = 0x40;
27
28void RadonEyeRD200::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
29 esp_ble_gattc_cb_param_t *param) {
30 switch (event) {
31 case ESP_GATTC_OPEN_EVT: {
32 if (param->open.status == ESP_GATT_OK) {
33 ESP_LOGI(TAG, "Connected successfully!");
34 }
35 break;
36 }
37
38 case ESP_GATTC_DISCONNECT_EVT: {
39 ESP_LOGW(TAG, "Disconnected!");
40 break;
41 }
42
43 case ESP_GATTC_SEARCH_CMPL_EVT: {
44 if (this->parent()->get_service(SERVICE_UUID_V1) != nullptr) {
45 service_uuid_ = SERVICE_UUID_V1;
46 sensors_write_characteristic_uuid_ = WRITE_CHARACTERISTIC_UUID_V1;
47 sensors_read_characteristic_uuid_ = READ_CHARACTERISTIC_UUID_V1;
48 write_command_ = WRITE_COMMAND_V1;
49 } else if (this->parent()->get_service(SERVICE_UUID_V2) != nullptr) {
50 service_uuid_ = SERVICE_UUID_V2;
51 sensors_write_characteristic_uuid_ = WRITE_CHARACTERISTIC_UUID_V2;
52 sensors_read_characteristic_uuid_ = READ_CHARACTERISTIC_UUID_V2;
53 write_command_ = WRITE_COMMAND_V2;
54 } else {
55 ESP_LOGW(TAG, "No supported device has been found, disconnecting");
56 parent()->set_enabled(false);
57 break;
58 }
59
60 this->read_handle_ = 0;
62 if (chr == nullptr) {
63 char service_buf[esp32_ble::UUID_STR_LEN];
64 char char_buf[esp32_ble::UUID_STR_LEN];
65 ESP_LOGW(TAG, "No sensor read characteristic found at service %s char %s", service_uuid_.to_str(service_buf),
67 break;
68 }
69 this->read_handle_ = chr->handle;
70
72 if (write_chr == nullptr) {
73 char service_buf[esp32_ble::UUID_STR_LEN];
74 char char_buf[esp32_ble::UUID_STR_LEN];
75 ESP_LOGW(TAG, "No sensor write characteristic found at service %s char %s", service_uuid_.to_str(service_buf),
77 break;
78 }
79 this->write_handle_ = write_chr->handle;
80
81 esp_err_t status =
82 esp_ble_gattc_register_for_notify(gattc_if, this->parent()->get_remote_bda(), this->read_handle_);
83 if (status) {
84 ESP_LOGW(TAG, "Error registering for sensor notify, status=%d", status);
85 }
86 break;
87 }
88
89 case ESP_GATTC_WRITE_DESCR_EVT: {
90 if (param->write.status != ESP_GATT_OK) {
91 ESP_LOGE(TAG, "write descr failed, error status = %x", param->write.status);
92 break;
93 }
94 ESP_LOGV(TAG, "Write descr success, writing 0x%02X at write_handle=%d", this->write_command_,
95 this->write_handle_);
96 esp_err_t status =
97 esp_ble_gattc_write_char(gattc_if, this->parent()->get_conn_id(), this->write_handle_, sizeof(write_command_),
98 (uint8_t *) &write_command_, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
99 if (status) {
100 ESP_LOGW(TAG, "Error writing 0x%02x command, status=%d", write_command_, status);
101 }
102 break;
103 }
104
105 case ESP_GATTC_NOTIFY_EVT: {
106 if (param->notify.is_notify) {
107 ESP_LOGV(TAG, "ESP_GATTC_NOTIFY_EVT, receive notify value, %d bytes", param->notify.value_len);
108 } else {
109 ESP_LOGV(TAG, "ESP_GATTC_NOTIFY_EVT, receive indicate value, %d bytes", param->notify.value_len);
110 }
111 read_sensors_(param->notify.value, param->notify.value_len);
112 break;
113 }
114
115 default:
116 break;
117 }
118}
119
120void RadonEyeRD200::read_sensors_(uint8_t *value, uint16_t value_len) {
121 if (value_len < 1) {
122 ESP_LOGW(TAG, "Unexpected empty message");
123 return;
124 }
125
126 uint8_t command = value[0];
127
128 if ((command == WRITE_COMMAND_V1 && value_len < 20) || (command == WRITE_COMMAND_V2 && value_len < 68)) {
129 ESP_LOGW(TAG, "Unexpected command 0x%02X message length %d", command, value_len);
130 return;
131 }
132
133 // Example data V1:
134 // 501085EBB9400000000000000000220025000000
135 // Example data V2:
136 // 4042323230313033525532303338330652443230304e56322e302e3200014a00060a00080000000300010079300000e01108001c00020000003822005c8f423fa4709d3f
137 ESP_LOGV(TAG, "radon sensors raw bytes");
138 ESP_LOG_BUFFER_HEX_LEVEL(TAG, value, value_len, ESP_LOG_VERBOSE);
139
140 // Convert from pCi/L to Bq/m³
141 constexpr float convert_to_bwpm3 = 37.0;
142
143 float radon_now; // in Bq/m³
144 float radon_day; // in Bq/m³
145 float radon_month; // in Bq/m³
146 if (command == WRITE_COMMAND_V1) {
147 // Use memcpy to avoid unaligned memory access
148 float temp;
149 memcpy(&temp, value + 2, sizeof(float));
150 radon_now = temp * convert_to_bwpm3;
151 memcpy(&temp, value + 6, sizeof(float));
152 radon_day = temp * convert_to_bwpm3;
153 memcpy(&temp, value + 10, sizeof(float));
154 radon_month = temp * convert_to_bwpm3;
155 } else if (command == WRITE_COMMAND_V2) {
156 // Use memcpy to avoid unaligned memory access
157 uint16_t temp;
158 memcpy(&temp, value + 33, sizeof(uint16_t));
159 radon_now = temp;
160 memcpy(&temp, value + 35, sizeof(uint16_t));
161 radon_day = temp;
162 memcpy(&temp, value + 37, sizeof(uint16_t));
163 radon_month = temp;
164 } else {
165 ESP_LOGW(TAG, "Unexpected command value: 0x%02X", command);
166 return;
167 }
168
169 if (this->radon_sensor_ != nullptr) {
170 this->radon_sensor_->publish_state(radon_now);
171 }
172
173 if (this->radon_long_term_sensor_ != nullptr) {
174 if (radon_month > 0) {
175 ESP_LOGV(TAG, "Radon Long Term based on month");
176 this->radon_long_term_sensor_->publish_state(radon_month);
177 } else {
178 ESP_LOGV(TAG, "Radon Long Term based on day");
179 this->radon_long_term_sensor_->publish_state(radon_day);
180 }
181 }
182
183 ESP_LOGV(TAG,
184 " Measurements (Bq/m³) now: %0.03f, day: %0.03f, month: %0.03f\n"
185 " Measurements (pCi/L) now: %0.03f, day: %0.03f, month: %0.03f",
186 radon_now, radon_day, radon_month, radon_now / convert_to_bwpm3, radon_day / convert_to_bwpm3,
187 radon_month / convert_to_bwpm3);
188
189 // This instance must not stay connected
190 // so other clients can connect to it (e.g. the
191 // mobile app).
192 parent()->set_enabled(false);
193}
194
197 if (!parent()->enabled) {
198 ESP_LOGW(TAG, "Reconnecting to device");
199 parent()->set_enabled(true);
200 } else {
201 ESP_LOGW(TAG, "Connection in progress");
202 }
203 }
204}
205
207 LOG_SENSOR(" ", "Radon", this->radon_sensor_);
208 LOG_SENSOR(" ", "Radon Long Term", this->radon_long_term_sensor_);
209}
210
212
213} // namespace esphome::radon_eye_rd200
214
215#endif // USE_ESP32
uint8_t status
Definition bl0942.h:8
This class simplifies creating components that periodically check a state.
Definition component.h:585
void set_enabled(bool enabled)
ESPDEPRECATED("Use to_str() instead. Removed in 2026.8.0", "2026.2.0") std const char * to_str(std::span< char, UUID_STR_LEN > output) const
Definition ble_uuid.cpp:146
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
void read_sensors_(uint8_t *value, uint16_t value_len)
esp32_ble_tracker::ESPBTUUID sensors_write_characteristic_uuid_
esp32_ble_tracker::ESPBTUUID service_uuid_
esp32_ble_tracker::ESPBTUUID sensors_read_characteristic_uuid_
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68