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