ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
grove_gas_mc_v2.cpp
Go to the documentation of this file.
1#include "grove_gas_mc_v2.h"
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
4
6
7ESPHOME_LOG_TAG(TAG, "grove_gas_mc_v2");
8
9// I2C Commands for Grove Gas Multichannel V2 Sensor
10// Taken from:
11// https://github.com/Seeed-Studio/Seeed_Arduino_MultiGas/blob/master/src/Multichannel_Gas_GroveGasMultichannelV2.h
12static const uint8_t GROVE_GAS_MC_V2_HEAT_ON = 0xFE;
13static const uint8_t GROVE_GAS_MC_V2_HEAT_OFF = 0xFF;
14static const uint8_t GROVE_GAS_MC_V2_READ_GM102B = 0x01;
15static const uint8_t GROVE_GAS_MC_V2_READ_GM302B = 0x03;
16static const uint8_t GROVE_GAS_MC_V2_READ_GM502B = 0x05;
17static const uint8_t GROVE_GAS_MC_V2_READ_GM702B = 0x07;
18
20 if (sensor == nullptr) {
21 return true;
22 }
23 uint32_t value = 0;
24 if (!this->read_bytes(address, (uint8_t *) &value, 4)) {
25 ESP_LOGW(TAG, "Reading Grove Gas Sensor data failed!");
26 this->status_set_warning();
27 return false;
28 }
29 sensor->publish_state(value);
30 return true;
31}
32
34 // Before reading sensor values, must preheat sensor
35 if (!(this->write_bytes(GROVE_GAS_MC_V2_HEAT_ON, {}))) {
36 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
37 }
38}
39
40void GroveGasMultichannelV2Component::update() {
41 // Read from each of the gas sensors
42 if (!this->read_sensor_(GROVE_GAS_MC_V2_READ_GM102B, this->nitrogen_dioxide_sensor_))
43 return;
44 if (!this->read_sensor_(GROVE_GAS_MC_V2_READ_GM302B, this->ethanol_sensor_))
45 return;
46 if (!this->read_sensor_(GROVE_GAS_MC_V2_READ_GM502B, this->tvoc_sensor_))
47 return;
48 if (!this->read_sensor_(GROVE_GAS_MC_V2_READ_GM702B, this->carbon_monoxide_sensor_))
49 return;
50
52}
53
54void GroveGasMultichannelV2Component::dump_config() {
55 ESP_LOGCONFIG(TAG, "Grove Multichannel Gas Sensor V2");
56 LOG_I2C_DEVICE(this)
57 LOG_UPDATE_INTERVAL(this);
58 LOG_SENSOR(" ", "Nitrogen Dioxide", this->nitrogen_dioxide_sensor_);
59 LOG_SENSOR(" ", "Ethanol", this->ethanol_sensor_);
60 LOG_SENSOR(" ", "Carbon Monoxide", this->carbon_monoxide_sensor_);
61 LOG_SENSOR(" ", "TVOC", this->tvoc_sensor_);
62}
63
64} // namespace esphome::grove_gas_mc_v2
uint8_t address
Definition bl0906.h:4
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
void status_clear_warning()
Definition component.h:296
bool read_sensor_(uint8_t address, sensor::Sensor *sensor)
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:298
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:249
Base-class for all sensors.
Definition sensor.h:47
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
ESPHOME_LOG_TAG(TAG, "grove_gas_mc_v2")
static void uint32_t