ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
mopeka_std_check.cpp
Go to the documentation of this file.
1#include "mopeka_std_check.h"
2
4#include "esphome/core/log.h"
5
6#ifdef USE_ESP32
7
8namespace esphome {
9namespace mopeka_std_check {
10
11static const char *const TAG = "mopeka_std_check";
12static const uint16_t SERVICE_UUID = 0xADA0;
13static const uint8_t MANUFACTURER_DATA_LENGTH = 23;
14static const uint16_t MANUFACTURER_ID = 0x000D;
15
16// Maximum bytes to log in very verbose hex output
17static constexpr size_t MOPEKA_MAX_LOG_BYTES = 32;
18
20 ESP_LOGCONFIG(TAG, "Mopeka Std Check");
21 ESP_LOGCONFIG(TAG, " Propane Butane mix: %.0f%%", this->propane_butane_mix_ * 100);
22 ESP_LOGCONFIG(TAG, " Tank distance empty: %" PRIi32 "mm", this->empty_mm_);
23 ESP_LOGCONFIG(TAG, " Tank distance full: %" PRIi32 "mm", this->full_mm_);
24 LOG_SENSOR(" ", "Level", this->level_);
25 LOG_SENSOR(" ", "Temperature", this->temperature_);
26 LOG_SENSOR(" ", "Battery Level", this->battery_level_);
27 LOG_SENSOR(" ", "Reading Distance", this->distance_);
28}
29
36 {
37 // Validate address.
38 if (device.address_uint64() != this->address_) {
39 return false;
40 }
41
42 ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str());
43 }
44
45 {
46 // Validate service uuid
47 const auto &service_uuids = device.get_service_uuids();
48 if (service_uuids.size() != 1) {
49 return false;
50 }
51 const auto &service_uuid = service_uuids[0];
52 if (service_uuid != esp32_ble_tracker::ESPBTUUID::from_uint16(SERVICE_UUID)) {
53 return false;
54 }
55 }
56
57 const auto &manu_datas = device.get_manufacturer_datas();
58
59 if (manu_datas.size() != 1) {
60 ESP_LOGE(TAG, "[%s] Unexpected manu_datas size (%d)", device.address_str().c_str(), manu_datas.size());
61 return false;
62 }
63
64 const auto &manu_data = manu_datas[0];
65
66#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
67 char hex_buf[format_hex_pretty_size(MOPEKA_MAX_LOG_BYTES)];
68#endif
69 ESP_LOGVV(TAG, "[%s] Manufacturer data: %s", device.address_str().c_str(),
70 format_hex_pretty_to(hex_buf, manu_data.data.data(), manu_data.data.size()));
71
72 if (manu_data.data.size() != MANUFACTURER_DATA_LENGTH) {
73 ESP_LOGE(TAG, "[%s] Unexpected manu_data size (%d)", device.address_str().c_str(), manu_data.data.size());
74 return false;
75 }
76
77 // Now parse the data
78 const auto *mopeka_data = (const mopeka_std_package *) manu_data.data.data();
79
80 const u_int8_t hardware_id = mopeka_data->data_1 & 0xCF;
81 if (static_cast<SensorType>(hardware_id) != STANDARD && static_cast<SensorType>(hardware_id) != XL &&
82 static_cast<SensorType>(hardware_id) != ETRAILER && static_cast<SensorType>(hardware_id) != STANDARD_ALT) {
83 ESP_LOGE(TAG, "[%s] Unsupported Sensor Type (0x%X)", device.address_str().c_str(), hardware_id);
84 return false;
85 }
86
87 ESP_LOGVV(TAG, "[%s] Sensor slow update rate: %d", device.address_str().c_str(), mopeka_data->slow_update_rate);
88 ESP_LOGVV(TAG, "[%s] Sensor sync pressed: %d", device.address_str().c_str(), mopeka_data->sync_pressed);
89 for (u_int8_t i = 0; i < 3; i++) {
90 ESP_LOGVV(TAG, "[%s] %u. Sensor data %u time %u.", device.address_str().c_str(), (i * 4) + 1,
91 mopeka_data->val[i].value_0, mopeka_data->val[i].time_0);
92 ESP_LOGVV(TAG, "[%s] %u. Sensor data %u time %u.", device.address_str().c_str(), (i * 4) + 2,
93 mopeka_data->val[i].value_1, mopeka_data->val[i].time_1);
94 ESP_LOGVV(TAG, "[%s] %u. Sensor data %u time %u.", device.address_str().c_str(), (i * 4) + 3,
95 mopeka_data->val[i].value_2, mopeka_data->val[i].time_2);
96 ESP_LOGVV(TAG, "[%s] %u. Sensor data %u time %u.", device.address_str().c_str(), (i * 4) + 4,
97 mopeka_data->val[i].value_3, mopeka_data->val[i].time_3);
98 }
99
100 // Get battery level first
101 if (this->battery_level_ != nullptr) {
102 uint8_t level = this->parse_battery_level_(mopeka_data);
103 this->battery_level_->publish_state(level);
104 }
105
106 // Get temperature of sensor
107 uint8_t temp_in_c = this->parse_temperature_(mopeka_data);
108 if (this->temperature_ != nullptr) {
109 this->temperature_->publish_state(temp_in_c);
110 }
111
112 // Get distance and level if either are sensors
113 if ((this->distance_ != nullptr) || (this->level_ != nullptr)) {
114 // Message contains 12 sensor dataset each 10 bytes long.
115 // each sensor dataset contains 5 byte time and 5 byte value.
116
117 // time in 10us ticks.
118 // value is amplitude.
119
120 std::array<u_int8_t, 12> measurements_time = {};
121 std::array<u_int8_t, 12> measurements_value = {};
122 // Copy measurements over into my array.
123 {
124 u_int8_t measurements_index = 0;
125 for (u_int8_t i = 0; i < 3; i++) {
126 measurements_time[measurements_index] = mopeka_data->val[i].time_0 + 1;
127 measurements_value[measurements_index] = mopeka_data->val[i].value_0;
128 measurements_index++;
129 measurements_time[measurements_index] = mopeka_data->val[i].time_1 + 1;
130 measurements_value[measurements_index] = mopeka_data->val[i].value_1;
131 measurements_index++;
132 measurements_time[measurements_index] = mopeka_data->val[i].time_2 + 1;
133 measurements_value[measurements_index] = mopeka_data->val[i].value_2;
134 measurements_index++;
135 measurements_time[measurements_index] = mopeka_data->val[i].time_3 + 1;
136 measurements_value[measurements_index] = mopeka_data->val[i].value_3;
137 measurements_index++;
138 }
139 }
140
141 // Find best(strongest) value(amplitude) and it's belonging time in sensor dataset.
142 u_int8_t number_of_usable_values = 0;
143 u_int16_t best_value = 0;
144 u_int16_t best_time = 0;
145 {
146 u_int16_t measurement_time = 0;
147 for (u_int8_t i = 0; i < 12; i++) {
148 // Time is summed up until a value is reported. This allows time values larger than the 5 bits in transport.
149 measurement_time += measurements_time[i];
150 if (measurements_value[i] != 0) {
151 // I got a value
152 number_of_usable_values++;
153 if (measurements_value[i] > best_value) {
154 // This value is better than a previous one.
155 best_value = measurements_value[i];
156 best_time = measurement_time;
157 }
158 // Reset measurement_time or next values.
159 measurement_time = 0;
160 }
161 }
162 }
163
164 ESP_LOGV(TAG, "[%s] Found %u values with best data %u time %u.", device.address_str().c_str(),
165 number_of_usable_values, best_value, best_time);
166
167 if (number_of_usable_values < 1 || best_value < 2 || best_time < 2) {
168 // At least two measurement values must be present.
169 ESP_LOGW(TAG, "[%s] Poor read quality. Setting distance to 0.", device.address_str().c_str());
170 if (this->distance_ != nullptr) {
171 this->distance_->publish_state(0);
172 }
173 if (this->level_ != nullptr) {
174 this->level_->publish_state(0);
175 }
176 } else {
177 float lpg_speed_of_sound = this->get_lpg_speed_of_sound_(temp_in_c);
178 ESP_LOGV(TAG, "[%s] Speed of sound in current fluid %f m/s", device.address_str().c_str(), lpg_speed_of_sound);
179
180 uint32_t distance_value = lpg_speed_of_sound * best_time / 100.0f;
181
182 // update distance sensor
183 if (this->distance_ != nullptr) {
184 this->distance_->publish_state(distance_value);
185 }
186
187 // update level sensor
188 if (this->level_ != nullptr) {
189 uint8_t tank_level = 0;
190 if (distance_value >= this->full_mm_) {
191 tank_level = 100; // cap at 100%
192 } else if (distance_value > this->empty_mm_) {
193 tank_level = ((100.0f / (this->full_mm_ - this->empty_mm_)) * (distance_value - this->empty_mm_));
194 }
195 this->level_->publish_state(tank_level);
196 }
197 }
198 }
199
200 return true;
201}
202
204 return 1040.71f - 4.87f * temperature - 137.5f * this->propane_butane_mix_ - 0.0107f * temperature * temperature -
205 1.63f * temperature * this->propane_butane_mix_;
206}
207
209 const float voltage = (float) ((message->raw_voltage / 256.0f) * 2.0f + 1.5f);
210 ESP_LOGVV(TAG, "Sensor battery voltage: %f V", voltage);
211 // convert voltage and scale for CR2032
212 const float percent = (voltage - 2.2f) / 0.65f * 100.0f;
213 if (percent < 0.0f) {
214 return 0;
215 }
216 if (percent > 100.0f) {
217 return 100;
218 }
219 return (uint8_t) percent;
220}
221
223 uint8_t tmp = message->raw_temp;
224 if (tmp == 0x0) {
225 return -40;
226 } else {
227 return (uint8_t) ((tmp - 25.0f) * 1.776964f);
228 }
229}
230
231} // namespace mopeka_std_check
232} // namespace esphome
233
234#endif
const std::vector< ServiceData > & get_manufacturer_datas() const
const std::vector< ESPBTUUID > & get_service_uuids() const
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
Main parse function that gets called for all ble advertisements.
uint8_t parse_temperature_(const mopeka_std_package *message)
uint8_t parse_battery_level_(const mopeka_std_package *message)
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:77
const char * message
Definition component.cpp:38
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:331
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Definition helpers.h:735
uint16_t temperature
Definition sun_gtil2.cpp:12