ESPHome  2024.7.2
pmsa003i.cpp
Go to the documentation of this file.
1 #include "pmsa003i.h"
2 #include "esphome/core/log.h"
3 #include <cstring>
4 
5 namespace esphome {
6 namespace pmsa003i {
7 
8 static const char *const TAG = "pmsa003i";
9 
11  ESP_LOGCONFIG(TAG, "Setting up pmsa003i...");
12 
13  PM25AQIData data;
14  bool successful_read = this->read_data_(&data);
15 
16  if (!successful_read) {
17  for (int i = 0; i < 3; i++) {
18  successful_read = this->read_data_(&data);
19  if (successful_read) {
20  break;
21  }
22  }
23  }
24 
25  if (!successful_read) {
26  this->mark_failed();
27  return;
28  }
29 }
30 
31 void PMSA003IComponent::dump_config() { LOG_I2C_DEVICE(this); }
32 
34  PM25AQIData data;
35 
36  bool successful_read = this->read_data_(&data);
37 
38  // Update sensors
39  if (successful_read) {
40  this->status_clear_warning();
41  ESP_LOGV(TAG, "Read success. Updating sensors.");
42 
43  if (this->standard_units_) {
44  if (this->pm_1_0_sensor_ != nullptr)
46  if (this->pm_2_5_sensor_ != nullptr)
48  if (this->pm_10_0_sensor_ != nullptr)
50  } else {
51  if (this->pm_1_0_sensor_ != nullptr)
53  if (this->pm_2_5_sensor_ != nullptr)
55  if (this->pm_10_0_sensor_ != nullptr)
57  }
58 
59  if (this->pmc_0_3_sensor_ != nullptr)
61  if (this->pmc_0_5_sensor_ != nullptr)
63  if (this->pmc_1_0_sensor_ != nullptr)
65  if (this->pmc_2_5_sensor_ != nullptr)
67  if (this->pmc_5_0_sensor_ != nullptr)
69  if (this->pmc_10_0_sensor_ != nullptr)
71  } else {
72  this->status_set_warning();
73  ESP_LOGV(TAG, "Read failure. Skipping update.");
74  }
75 }
76 
78  const uint8_t num_bytes = 32;
79  uint8_t buffer[num_bytes];
80 
81  this->read_bytes_raw(buffer, num_bytes);
82 
83  // https://github.com/adafruit/Adafruit_PM25AQI
84 
85  // Check that start byte is correct!
86  if (buffer[0] != 0x42) {
87  return false;
88  }
89 
90  // get checksum ready
91  int16_t sum = 0;
92  for (uint8_t i = 0; i < 30; i++) {
93  sum += buffer[i];
94  }
95 
96  // The data comes in endian'd, this solves it so it works on all platforms
97  uint16_t buffer_u16[15];
98  for (uint8_t i = 0; i < 15; i++) {
99  buffer_u16[i] = buffer[2 + i * 2 + 1];
100  buffer_u16[i] += (buffer[2 + i * 2] << 8);
101  }
102 
103  // put it into a nice struct :)
104  memcpy((void *) data, (void *) buffer_u16, 30);
105 
106  return (sum == data->checksum);
107 }
108 
109 } // namespace pmsa003i
110 } // namespace esphome
uint16_t particles_25um
1.0um Particle Count
Definition: pmsa003i.h:20
bool read_data_(PM25AQIData *data)
Definition: pmsa003i.cpp:77
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
sensor::Sensor * pmc_2_5_sensor_
Definition: pmsa003i.h:62
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition: i2c.h:225
uint16_t pm10_standard
Standard PM1.0.
Definition: pmsa003i.h:14
uint16_t pm10_env
Environmental PM1.0.
Definition: pmsa003i.h:17
uint16_t particles_05um
0.3um Particle Count
Definition: pmsa003i.h:20
uint16_t pm25_standard
Standard PM2.5.
Definition: pmsa003i.h:14
sensor::Sensor * pm_10_0_sensor_
Definition: pmsa003i.h:57
uint16_t pm25_env
Environmental PM2.5.
Definition: pmsa003i.h:17
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
sensor::Sensor * pmc_0_3_sensor_
Definition: pmsa003i.h:59
sensor::Sensor * pmc_5_0_sensor_
Definition: pmsa003i.h:63
! Structure holding Plantower&#39;s standard packet
Definition: pmsa003i.h:12
uint16_t pm100_standard
Standard PM10.0.
Definition: pmsa003i.h:14
sensor::Sensor * pmc_0_5_sensor_
Definition: pmsa003i.h:60
uint16_t particles_10um
0.5um Particle Count
Definition: pmsa003i.h:20
sensor::Sensor * pmc_10_0_sensor_
Definition: pmsa003i.h:64
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
sensor::Sensor * pmc_1_0_sensor_
Definition: pmsa003i.h:61
uint16_t particles_50um
2.5um Particle Count
Definition: pmsa003i.h:20
uint16_t particles_100um
5.0um Particle Count
Definition: pmsa003i.h:20
uint16_t pm100_env
Environmental PM10.0.
Definition: pmsa003i.h:17