ESPHome 2025.5.0
Loading...
Searching...
No Matches
mopeka_pro_check.cpp
Go to the documentation of this file.
1#include "mopeka_pro_check.h"
2#include "esphome/core/log.h"
3
4#ifdef USE_ESP32
5
6namespace esphome {
7namespace mopeka_pro_check {
8
9static const char *const TAG = "mopeka_pro_check";
10static const uint8_t MANUFACTURER_DATA_LENGTH = 10;
11static const uint16_t MANUFACTURER_ID = 0x0059;
12static const double MOPEKA_LPG_COEF[] = {0.573045, -0.002822, -0.00000535}; // Magic numbers provided by Mopeka
13
15 ESP_LOGCONFIG(TAG, "Mopeka Pro Check");
16 LOG_SENSOR(" ", "Level", this->level_);
17 LOG_SENSOR(" ", "Temperature", this->temperature_);
18 LOG_SENSOR(" ", "Battery Level", this->battery_level_);
19 LOG_SENSOR(" ", "Reading Distance", this->distance_);
20 LOG_SENSOR(" ", "Read Quality", this->read_quality_);
21 LOG_SENSOR(" ", "Ignored Reads", this->ignored_reads_);
22}
23
30 if (device.address_uint64() != this->address_) {
31 return false;
32 }
33
34 ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str());
35
36 const auto &manu_datas = device.get_manufacturer_datas();
37
38 if (manu_datas.size() != 1) {
39 ESP_LOGE(TAG, "Unexpected manu_datas size (%d)", manu_datas.size());
40 return false;
41 }
42
43 const auto &manu_data = manu_datas[0];
44
45 ESP_LOGVV(TAG, "Manufacturer data:");
46 for (const uint8_t byte : manu_data.data) {
47 ESP_LOGVV(TAG, "0x%02x", byte);
48 }
49
50 if (manu_data.data.size() != MANUFACTURER_DATA_LENGTH) {
51 ESP_LOGE(TAG, "Unexpected manu_data size (%d)", manu_data.data.size());
52 return false;
53 }
54
55 // Now parse the data - See Datasheet for definition
56
57 if (static_cast<SensorType>(manu_data.data[0]) != STANDARD_BOTTOM_UP &&
58 static_cast<SensorType>(manu_data.data[0]) != LIPPERT_BOTTOM_UP &&
59 static_cast<SensorType>(manu_data.data[0]) != PLUS_BOTTOM_UP &&
60 static_cast<SensorType>(manu_data.data[0]) != PRO_UNIVERSAL) {
61 ESP_LOGE(TAG, "Unsupported Sensor Type (0x%X)", manu_data.data[0]);
62 return false;
63 }
64
65 // Get battery level first
66 if (this->battery_level_ != nullptr) {
67 uint8_t level = this->parse_battery_level_(manu_data.data);
68 this->battery_level_->publish_state(level);
69 }
70
71 // Get the quality value
72 SensorReadQuality quality_value = this->parse_read_quality_(manu_data.data);
73 if (this->read_quality_ != nullptr) {
74 this->read_quality_->publish_state(static_cast<int>(quality_value));
75 }
76
77 // Determine if we have a good enough quality of read to report level and distance
78 // sensors. This sensor is reported regardless of distance or level sensors being enabled
79 if (quality_value < this->min_signal_quality_) {
80 ESP_LOGW(TAG, "Read Quality too low to report distance or level");
81 this->ignored_read_count_++;
82 } else {
83 // reset to zero since read quality was sufficient
84 this->ignored_read_count_ = 0;
85 }
86 // Report number of contiguous ignored reads if sensor defined
87 if (this->ignored_reads_ != nullptr) {
89 }
90
91 // Get distance and level if either are sensors
92 if ((this->distance_ != nullptr) || (this->level_ != nullptr)) {
93 uint32_t distance_value = this->parse_distance_(manu_data.data);
94 ESP_LOGD(TAG, "Distance Sensor: Quality (0x%X) Distance (%" PRId32 "mm)", quality_value, distance_value);
95
96 // only update distance and level sensors if read quality was sufficient. This can be determined by
97 // if the ignored_read_count is zero.
98 if (this->ignored_read_count_ == 0) {
99 // update distance sensor
100 if (this->distance_ != nullptr) {
101 this->distance_->publish_state(distance_value);
102 }
103
104 // update level sensor
105 if (this->level_ != nullptr) {
106 uint8_t tank_level = 0;
107 if (distance_value >= this->full_mm_) {
108 tank_level = 100; // cap at 100%
109 } else if (distance_value > this->empty_mm_) {
110 tank_level = ((100.0f / (this->full_mm_ - this->empty_mm_)) * (distance_value - this->empty_mm_));
111 }
112 this->level_->publish_state(tank_level);
113 }
114 }
115 }
116
117 // Get temperature of sensor
118 if (this->temperature_ != nullptr) {
119 uint8_t temp_in_c = this->parse_temperature_(manu_data.data);
120 this->temperature_->publish_state(temp_in_c);
121 }
122
123 return true;
124}
125
126uint8_t MopekaProCheck::parse_battery_level_(const std::vector<uint8_t> &message) {
127 float v = (float) ((message[1] & 0x7F) / 32.0f);
128 // convert voltage and scale for CR2032
129 float percent = (v - 2.2f) / 0.65f * 100.0f;
130 if (percent < 0.0f) {
131 return 0;
132 }
133 if (percent > 100.0f) {
134 return 100;
135 }
136 return (uint8_t) percent;
137}
138
139uint32_t MopekaProCheck::parse_distance_(const std::vector<uint8_t> &message) {
140 uint16_t raw = (message[4] * 256) + message[3];
141 double raw_level = raw & 0x3FFF;
142 double raw_t = (message[2] & 0x7F);
143
144 return (uint32_t) (raw_level *
145 (MOPEKA_LPG_COEF[0] + MOPEKA_LPG_COEF[1] * raw_t + MOPEKA_LPG_COEF[2] * raw_t * raw_t));
146}
147
148uint8_t MopekaProCheck::parse_temperature_(const std::vector<uint8_t> &message) { return (message[2] & 0x7F) - 40; }
149
150SensorReadQuality MopekaProCheck::parse_read_quality_(const std::vector<uint8_t> &message) {
151 // Since a 8 bit value is being shifted and truncated to 2 bits all possible values are defined as enumeration
152 // value and the static cast is safe.
153 return static_cast<SensorReadQuality>(message[4] >> 6);
154}
155
156} // namespace mopeka_pro_check
157} // namespace esphome
158
159#endif
uint8_t raw[35]
Definition bl0939.h:0
const std::vector< ServiceData > & get_manufacturer_datas() const
SensorReadQuality parse_read_quality_(const std::vector< uint8_t > &message)
uint8_t parse_battery_level_(const std::vector< uint8_t > &message)
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
Main parse function that gets called for all ble advertisements.
uint32_t parse_distance_(const std::vector< uint8_t > &message)
uint8_t parse_temperature_(const std::vector< uint8_t > &message)
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7