ESPHome 2026.5.0b1
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
8static const char *const TAG = "mics_4514";
9
10static const uint8_t SENSOR_REGISTER = 0x04;
11static const uint8_t POWER_MODE_REGISTER = 0x0a;
12
14 uint8_t power_mode;
15 this->read_register(POWER_MODE_REGISTER, &power_mode, 1);
16 if (power_mode == 0x00) {
17 ESP_LOGCONFIG(TAG, "Waking up MICS 4514, sensors will have data after 3 minutes");
18 power_mode = 0x01;
19 this->write_register(POWER_MODE_REGISTER, &power_mode, 1);
20 delay(100); // NOLINT
21 this->set_timeout("warmup", 3 * 60 * 1000, [this]() { this->warmed_up_ = true; });
22 this->status_set_warning();
23 return;
24 }
25 ESP_LOGCONFIG(TAG, "Device already awake.");
26 this->warmed_up_ = true;
27}
28void MICS4514Component::dump_config() {
29 ESP_LOGCONFIG(TAG, "MICS 4514:");
30 LOG_I2C_DEVICE(this);
31 LOG_UPDATE_INTERVAL(this);
32 LOG_SENSOR(" ", "Nitrogen Dioxide", this->nitrogen_dioxide_sensor_);
33 LOG_SENSOR(" ", "Carbon Monoxide", this->carbon_monoxide_sensor_);
34 LOG_SENSOR(" ", "Methane", this->methane_sensor_);
35 LOG_SENSOR(" ", "Ethanol", this->ethanol_sensor_);
36 LOG_SENSOR(" ", "Hydrogen", this->hydrogen_sensor_);
37 LOG_SENSOR(" ", "Ammonia", this->ammonia_sensor_);
38}
39void MICS4514Component::update() {
40 if (!this->warmed_up_) {
41 return;
42 }
43 uint8_t data[6];
44 if (this->read_register(SENSOR_REGISTER, data, 6) != i2c::ERROR_OK) {
45 this->status_set_warning();
46 return;
47 }
49 ESP_LOGV(TAG, "Got data: %02X %02X %02X %02X %02X %02X", data[0], data[1], data[2], data[3], data[4], data[5]);
50 uint16_t ox = encode_uint16(data[0], data[1]);
51 uint16_t red = encode_uint16(data[2], data[3]);
52 uint16_t power = encode_uint16(data[4], data[5]);
53
54 if (this->initial_) {
55 this->initial_ = false;
56 this->ox_calibration_ = (float) (power - ox);
57 this->red_calibration_ = (float) (power - red);
58 return;
59 }
60
61 if (this->red_calibration_ == 0 || this->ox_calibration_ == 0) {
62 ESP_LOGW(TAG, "Calibration values are zero, retrying");
63 this->status_set_warning();
64 this->initial_ = true;
65 return;
66 }
67 float red_f = (float) (power - red) / this->red_calibration_;
68 float ox_f = (float) (power - ox) / this->ox_calibration_;
69
70 if (this->carbon_monoxide_sensor_ != nullptr) {
71 float co = 0.0f;
72 if (red_f > 3.4f) {
73 co = 0.0;
74 } else if (red_f < 0.01) {
75 co = 1000.0;
76 } else {
77 co = 4.2 / pow(red_f, 1.2);
78 }
79 this->carbon_monoxide_sensor_->publish_state(co);
80 }
81
82 if (this->nitrogen_dioxide_sensor_ != nullptr) {
83 float nitrogendioxide = 0.0f;
84 if (ox_f < 0.3f) {
85 nitrogendioxide = 0.0;
86 } else {
87 nitrogendioxide = 0.164 * pow(ox_f, 0.975);
88 }
89 this->nitrogen_dioxide_sensor_->publish_state(nitrogendioxide);
90 }
91
92 if (this->methane_sensor_ != nullptr) {
93 float methane = 0.0f;
94 if (red_f > 0.9f || red_f < 0.5) { // outside the range->unlikely
95 methane = 0.0;
96 } else {
97 methane = 630 / pow(red_f, 4.4);
98 }
99 this->methane_sensor_->publish_state(methane);
100 }
101
102 if (this->ethanol_sensor_ != nullptr) {
103 float ethanol = 0.0f;
104 if (red_f > 1.0f || red_f < 0.02) { // outside the range->unlikely
105 ethanol = 0.0;
106 } else {
107 ethanol = 1.52 / pow(red_f, 1.55);
108 }
109 this->ethanol_sensor_->publish_state(ethanol);
110 }
111
112 if (this->hydrogen_sensor_ != nullptr) {
113 float hydrogen = 0.0f;
114 if (red_f > 0.9f || red_f < 0.02) { // outside the range->unlikely
115 hydrogen = 0.0;
116 } else {
117 hydrogen = 0.85 / pow(red_f, 1.75);
118 }
119 this->hydrogen_sensor_->publish_state(hydrogen);
120 }
121
122 if (this->ammonia_sensor_ != nullptr) {
123 float ammonia = 0.0f;
124 if (red_f > 0.98f || red_f < 0.2532) { // outside the ammonia range->unlikely
125 ammonia = 0.0;
126 } else {
127 ammonia = 0.9 / pow(red_f, 4.6);
128 }
129 this->ammonia_sensor_->publish_state(ammonia);
130 }
131}
132
133} // namespace esphome::mics_4514
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:89
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:510
void status_clear_warning()
Definition component.h:306
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:34
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:25
PowerMode power_mode
Definition msa3xx.h:3
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
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:859
void HOT delay(uint32_t ms)
Definition hal.cpp:82