ESPHome  2022.8.0
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  this->mark_failed();
18  return;
19  }
20 }
21 
22 void PMSA003IComponent::dump_config() { LOG_I2C_DEVICE(this); }
23 
25  PM25AQIData data;
26 
27  bool successful_read = this->read_data_(&data);
28 
29  // Update sensors
30  if (successful_read) {
31  this->status_clear_warning();
32  ESP_LOGV(TAG, "Read success. Updating sensors.");
33 
34  if (this->standard_units_) {
35  if (this->pm_1_0_sensor_ != nullptr)
37  if (this->pm_2_5_sensor_ != nullptr)
39  if (this->pm_10_0_sensor_ != nullptr)
41  } else {
42  if (this->pm_1_0_sensor_ != nullptr)
44  if (this->pm_2_5_sensor_ != nullptr)
46  if (this->pm_10_0_sensor_ != nullptr)
48  }
49 
50  if (this->pmc_0_3_sensor_ != nullptr)
52  if (this->pmc_0_5_sensor_ != nullptr)
54  if (this->pmc_1_0_sensor_ != nullptr)
56  if (this->pmc_2_5_sensor_ != nullptr)
58  if (this->pmc_5_0_sensor_ != nullptr)
60  if (this->pmc_10_0_sensor_ != nullptr)
62  } else {
63  this->status_set_warning();
64  ESP_LOGV(TAG, "Read failure. Skipping update.");
65  }
66 }
67 
69  const uint8_t num_bytes = 32;
70  uint8_t buffer[num_bytes];
71 
72  this->read_bytes_raw(buffer, num_bytes);
73 
74  // https://github.com/adafruit/Adafruit_PM25AQI
75 
76  // Check that start byte is correct!
77  if (buffer[0] != 0x42) {
78  return false;
79  }
80 
81  // get checksum ready
82  int16_t sum = 0;
83  for (uint8_t i = 0; i < 30; i++) {
84  sum += buffer[i];
85  }
86 
87  // The data comes in endian'd, this solves it so it works on all platforms
88  uint16_t buffer_u16[15];
89  for (uint8_t i = 0; i < 15; i++) {
90  buffer_u16[i] = buffer[2 + i * 2 + 1];
91  buffer_u16[i] += (buffer[2 + i * 2] << 8);
92  }
93 
94  // put it into a nice struct :)
95  memcpy((void *) data, (void *) buffer_u16, 30);
96 
97  return (sum == data->checksum);
98 }
99 
100 } // namespace pmsa003i
101 } // namespace esphome
uint16_t particles_03um
0.3um Particle Count
Definition: pmsa003i.h:20
uint16_t particles_25um
2.5um Particle Count
Definition: pmsa003i.h:20
bool read_data_(PM25AQIData *data)
Definition: pmsa003i.cpp:68
sensor::Sensor * pmc_2_5_sensor_
Definition: pmsa003i.h:62
optional< std::array< uint8_t, N > > read_bytes_raw()
Definition: i2c.h:80
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.5um 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:148
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:72
sensor::Sensor * pmc_0_3_sensor_
Definition: pmsa003i.h:59
void status_set_warning()
Definition: component.cpp:140
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
1.0um 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:111
Definition: a4988.cpp:4
sensor::Sensor * pmc_1_0_sensor_
Definition: pmsa003i.h:61
uint16_t particles_50um
5.0um Particle Count
Definition: pmsa003i.h:20
uint16_t particles_100um
10.0um Particle Count
Definition: pmsa003i.h:20
uint16_t pm100_env
Environmental PM10.0.
Definition: pmsa003i.h:17