ESPHome  2022.8.0
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 
6 namespace esphome {
7 namespace mopeka_pro_check {
8 
9 static const char *const TAG = "mopeka_pro_check";
10 static const uint8_t MANUFACTURER_DATA_LENGTH = 10;
11 static const uint16_t MANUFACTURER_ID = 0x0059;
12 static 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 }
21 
28  if (device.address_uint64() != this->address_) {
29  return false;
30  }
31 
32  ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str());
33 
34  const auto &manu_datas = device.get_manufacturer_datas();
35 
36  if (manu_datas.size() != 1) {
37  ESP_LOGE(TAG, "Unexpected manu_datas size (%d)", manu_datas.size());
38  return false;
39  }
40 
41  const auto &manu_data = manu_datas[0];
42 
43  ESP_LOGVV(TAG, "Manufacturer data:");
44  for (const uint8_t byte : manu_data.data) {
45  ESP_LOGVV(TAG, "0x%02x", byte);
46  }
47 
48  if (manu_data.data.size() != MANUFACTURER_DATA_LENGTH) {
49  ESP_LOGE(TAG, "Unexpected manu_data size (%d)", manu_data.data.size());
50  return false;
51  }
52 
53  // Now parse the data - See Datasheet for definition
54 
55  if (static_cast<SensorType>(manu_data.data[0]) != STANDARD_BOTTOM_UP &&
56  static_cast<SensorType>(manu_data.data[0]) != PLUS_BOTTOM_UP) {
57  ESP_LOGE(TAG, "Unsupported Sensor Type (0x%X)", manu_data.data[0]);
58  return false;
59  }
60 
61  // Get battery level first
62  if (this->battery_level_ != nullptr) {
63  uint8_t level = this->parse_battery_level_(manu_data.data);
64  this->battery_level_->publish_state(level);
65  }
66 
67  // Get distance and level if either are sensors
68  if ((this->distance_ != nullptr) || (this->level_ != nullptr)) {
69  uint32_t distance_value = this->parse_distance_(manu_data.data);
70  SensorReadQuality quality_value = this->parse_read_quality_(manu_data.data);
71  ESP_LOGD(TAG, "Distance Sensor: Quality (0x%X) Distance (%dmm)", quality_value, distance_value);
72  if (quality_value < QUALITY_HIGH) {
73  ESP_LOGW(TAG, "Poor read quality.");
74  }
75  if (quality_value < QUALITY_MED) {
76  // if really bad reading set to 0
77  ESP_LOGW(TAG, "Setting distance to 0");
78  distance_value = 0;
79  }
80 
81  // update distance sensor
82  if (this->distance_ != nullptr) {
83  this->distance_->publish_state(distance_value);
84  }
85 
86  // update level sensor
87  if (this->level_ != nullptr) {
88  uint8_t tank_level = 0;
89  if (distance_value >= this->full_mm_) {
90  tank_level = 100; // cap at 100%
91  } else if (distance_value > this->empty_mm_) {
92  tank_level = ((100.0f / (this->full_mm_ - this->empty_mm_)) * (distance_value - this->empty_mm_));
93  }
94  this->level_->publish_state(tank_level);
95  }
96  }
97 
98  // Get temperature of sensor
99  if (this->temperature_ != nullptr) {
100  uint8_t temp_in_c = this->parse_temperature_(manu_data.data);
101  this->temperature_->publish_state(temp_in_c);
102  }
103 
104  return true;
105 }
106 
107 uint8_t MopekaProCheck::parse_battery_level_(const std::vector<uint8_t> &message) {
108  float v = (float) ((message[1] & 0x7F) / 32.0f);
109  // convert voltage and scale for CR2032
110  float percent = (v - 2.2f) / 0.65f * 100.0f;
111  if (percent < 0.0f) {
112  return 0;
113  }
114  if (percent > 100.0f) {
115  return 100;
116  }
117  return (uint8_t) percent;
118 }
119 
120 uint32_t MopekaProCheck::parse_distance_(const std::vector<uint8_t> &message) {
121  uint16_t raw = (message[4] * 256) + message[3];
122  double raw_level = raw & 0x3FFF;
123  double raw_t = (message[2] & 0x7F);
124 
125  return (uint32_t)(raw_level * (MOPEKA_LPG_COEF[0] + MOPEKA_LPG_COEF[1] * raw_t + MOPEKA_LPG_COEF[2] * raw_t * raw_t));
126 }
127 
128 uint8_t MopekaProCheck::parse_temperature_(const std::vector<uint8_t> &message) { return (message[2] & 0x7F) - 40; }
129 
130 SensorReadQuality MopekaProCheck::parse_read_quality_(const std::vector<uint8_t> &message) {
131  return static_cast<SensorReadQuality>(message[4] >> 6);
132 }
133 
134 } // namespace mopeka_pro_check
135 } // namespace esphome
136 
137 #endif
uint8_t raw[35]
Definition: bl0939.h:19
SensorReadQuality parse_read_quality_(const std::vector< uint8_t > &message)
uint32_t parse_distance_(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.
const std::vector< ServiceData > & get_manufacturer_datas() const
uint8_t parse_battery_level_(const std::vector< uint8_t > &message)
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:72
uint8_t parse_temperature_(const std::vector< uint8_t > &message)
Definition: a4988.cpp:4