ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
mics_4514.cpp
Go to the documentation of this file.
1#include "mics_4514.h"
2
4#include "esphome/core/log.h"
5
7
8ESPHOME_LOG_TAG(TAG, "mics_4514");
9
10static constexpr uint32_t WARMUP_TIMEOUT_ID = 0;
11
12static const uint8_t SENSOR_REGISTER = 0x04;
13static const uint8_t POWER_MODE_REGISTER = 0x0a;
14
16 uint8_t power_mode;
17 this->read_register(POWER_MODE_REGISTER, &power_mode, 1);
18 if (power_mode == 0x00) {
19 ESP_LOGCONFIG(TAG, "Waking up MICS 4514, sensors will have data after 3 minutes");
20 power_mode = 0x01;
21 this->write_register(POWER_MODE_REGISTER, &power_mode, 1);
22 delay(100); // NOLINT
23 this->set_timeout(WARMUP_TIMEOUT_ID, 3 * 60 * 1000, [this]() { this->warmed_up_ = true; });
24 this->status_set_warning();
25 return;
26 }
27 ESP_LOGCONFIG(TAG, "Device already awake.");
28 this->warmed_up_ = true;
29}
30void MICS4514Component::dump_config() {
31 ESP_LOGCONFIG(TAG, "MICS 4514:");
32 LOG_I2C_DEVICE(this);
33 LOG_UPDATE_INTERVAL(this);
34 LOG_SENSOR(" ", "Nitrogen Dioxide", this->nitrogen_dioxide_sensor_);
35 LOG_SENSOR(" ", "Carbon Monoxide", this->carbon_monoxide_sensor_);
36 LOG_SENSOR(" ", "Methane", this->methane_sensor_);
37 LOG_SENSOR(" ", "Ethanol", this->ethanol_sensor_);
38 LOG_SENSOR(" ", "Hydrogen", this->hydrogen_sensor_);
39 LOG_SENSOR(" ", "Ammonia", this->ammonia_sensor_);
40}
41void MICS4514Component::update() {
42 if (!this->warmed_up_) {
43 return;
44 }
45 uint8_t data[6];
46 if (!this->status_comm_result(this->read_register(SENSOR_REGISTER, data, 6) == i2c::ERROR_OK))
47 return;
48 ESP_LOGV(TAG, "Got data: %02X %02X %02X %02X %02X %02X", data[0], data[1], data[2], data[3], data[4], data[5]);
49 uint16_t ox = encode_uint16(data[0], data[1]);
50 uint16_t red = encode_uint16(data[2], data[3]);
51 uint16_t power = encode_uint16(data[4], data[5]);
52
53 if (this->initial_) {
54 this->initial_ = false;
55 this->ox_calibration_ = (float) (power - ox);
56 this->red_calibration_ = (float) (power - red);
57 return;
58 }
59
60 if (this->red_calibration_ == 0 || this->ox_calibration_ == 0) {
61 ESP_LOGW(TAG, "Calibration values are zero, retrying");
62 this->status_set_warning();
63 this->initial_ = true;
64 return;
65 }
66 float red_f = (float) (power - red) / this->red_calibration_;
67 float ox_f = (float) (power - ox) / this->ox_calibration_;
68
69 if (this->carbon_monoxide_sensor_ != nullptr) {
70 float co = 0.0f;
71 if (red_f > 3.4f) {
72 co = 0.0;
73 } else if (red_f < 0.01f) {
74 co = 1000.0;
75 } else {
76 co = 4.2f / powf(red_f, 1.2f);
77 }
78 this->carbon_monoxide_sensor_->publish_state(co);
79 }
80
81 if (this->nitrogen_dioxide_sensor_ != nullptr) {
82 float nitrogendioxide = 0.0f;
83 if (ox_f < 0.3f) {
84 nitrogendioxide = 0.0;
85 } else {
86 nitrogendioxide = 0.164f * powf(ox_f, 0.975f);
87 }
88 this->nitrogen_dioxide_sensor_->publish_state(nitrogendioxide);
89 }
90
91 if (this->methane_sensor_ != nullptr) {
92 float methane = 0.0f;
93 if (red_f > 0.9f || red_f < 0.5f) { // outside the range->unlikely
94 methane = 0.0;
95 } else {
96 methane = 630 / powf(red_f, 4.4f);
97 }
98 this->methane_sensor_->publish_state(methane);
99 }
100
101 if (this->ethanol_sensor_ != nullptr) {
102 float ethanol = 0.0f;
103 if (red_f > 1.0f || red_f < 0.02f) { // outside the range->unlikely
104 ethanol = 0.0;
105 } else {
106 ethanol = 1.52f / powf(red_f, 1.55f);
107 }
108 this->ethanol_sensor_->publish_state(ethanol);
109 }
110
111 if (this->hydrogen_sensor_ != nullptr) {
112 float hydrogen = 0.0f;
113 if (red_f > 0.9f || red_f < 0.02f) { // outside the range->unlikely
114 hydrogen = 0.0;
115 } else {
116 hydrogen = 0.85f / powf(red_f, 1.75f);
117 }
118 this->hydrogen_sensor_->publish_state(hydrogen);
119 }
120
121 if (this->ammonia_sensor_ != nullptr) {
122 float ammonia = 0.0f;
123 if (red_f > 0.98f || red_f < 0.2532f) { // outside the ammonia range->unlikely
124 ammonia = 0.0;
125 } else {
126 ammonia = 0.9f / powf(red_f, 4.6f);
127 }
128 this->ammonia_sensor_->publish_state(ammonia);
129 }
130}
131
132} // namespace esphome::mics_4514
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:37
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:28
PowerMode power_mode
Definition msa3xx.h:3
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
FLAG_HAS_TRANSITION float
ESPHOME_LOG_TAG(TAG, "mics_4514")
std::span< const uint8_t > data
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1015
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t