ESPHome  2024.12.2
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 
5 namespace esphome {
6 namespace grove_gas_mc_v2 {
7 
8 static const char *const TAG = "grove_gas_mc_v2";
9 
10 // I2C Commands for Grove Gas Multichannel V2 Sensor
11 // Taken from:
12 // https://github.com/Seeed-Studio/Seeed_Arduino_MultiGas/blob/master/src/Multichannel_Gas_GroveGasMultichannelV2.h
13 static const uint8_t GROVE_GAS_MC_V2_HEAT_ON = 0xFE;
14 static const uint8_t GROVE_GAS_MC_V2_HEAT_OFF = 0xFF;
15 static const uint8_t GROVE_GAS_MC_V2_READ_GM102B = 0x01;
16 static const uint8_t GROVE_GAS_MC_V2_READ_GM302B = 0x03;
17 static const uint8_t GROVE_GAS_MC_V2_READ_GM502B = 0x05;
18 static const uint8_t GROVE_GAS_MC_V2_READ_GM702B = 0x07;
19 
21  if (sensor == nullptr) {
22  return true;
23  }
24  uint32_t value = 0;
25  if (!this->read_bytes(address, (uint8_t *) &value, 4)) {
26  ESP_LOGW(TAG, "Reading Grove Gas Sensor data failed!");
27  this->error_code_ = COMMUNICATION_FAILED;
28  this->status_set_warning();
29  return false;
30  }
31  sensor->publish_state(value);
32  return true;
33 }
34 
36  ESP_LOGCONFIG(TAG, "Setting up Grove Multichannel Gas Sensor V2...");
37 
38  // Before reading sensor values, must preheat sensor
39  if (!(this->write_bytes(GROVE_GAS_MC_V2_HEAT_ON, {}))) {
40  this->mark_failed();
41  this->error_code_ = APP_START_FAILED;
42  }
43 }
44 
45 void GroveGasMultichannelV2Component::update() {
46  // Read from each of the gas sensors
47  if (!this->read_sensor_(GROVE_GAS_MC_V2_READ_GM102B, this->nitrogen_dioxide_sensor_))
48  return;
49  if (!this->read_sensor_(GROVE_GAS_MC_V2_READ_GM302B, this->ethanol_sensor_))
50  return;
51  if (!this->read_sensor_(GROVE_GAS_MC_V2_READ_GM502B, this->tvoc_sensor_))
52  return;
53  if (!this->read_sensor_(GROVE_GAS_MC_V2_READ_GM702B, this->carbon_monoxide_sensor_))
54  return;
55 
56  this->status_clear_warning();
57 }
58 
59 void GroveGasMultichannelV2Component::dump_config() {
60  ESP_LOGCONFIG(TAG, "Grove Multichannel Gas Sensor V2");
61  LOG_I2C_DEVICE(this)
62  LOG_UPDATE_INTERVAL(this)
63  LOG_SENSOR(" ", "Nitrogen Dioxide", this->nitrogen_dioxide_sensor_)
64  LOG_SENSOR(" ", "Ethanol", this->ethanol_sensor_)
65  LOG_SENSOR(" ", "Carbon Monoxide", this->carbon_monoxide_sensor_)
66  LOG_SENSOR(" ", "TVOC", this->tvoc_sensor_)
67 
68  if (this->is_failed()) {
69  switch (this->error_code_) {
71  ESP_LOGW(TAG, "Communication failed! Is the sensor connected?");
72  break;
73  case APP_INVALID:
74  ESP_LOGW(TAG, "Sensor reported invalid APP installed.");
75  break;
76  case APP_START_FAILED:
77  ESP_LOGW(TAG, "Sensor reported APP start failed.");
78  break;
79  case UNKNOWN:
80  default:
81  ESP_LOGW(TAG, "Unknown setup error!");
82  break;
83  }
84  }
85 }
86 
87 } // namespace grove_gas_mc_v2
88 } // namespace esphome
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
bool is_failed() const
Definition: component.cpp:143
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:212
virtual void setup()
Where the component's initialization should happen.
Definition: component.cpp:48
bool read_sensor_(uint8_t address, sensor::Sensor *sensor)
void status_clear_warning()
Definition: component.cpp:166
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
enum esphome::grove_gas_mc_v2::GroveGasMultichannelV2Component::ErrorCode UNKNOWN
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t address
Definition: bl0906.h:211
Base-class for all sensors.
Definition: sensor.h:57
esphome::sensor::Sensor * sensor
Definition: statsd.h:38
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:248