supla-device
Loading...
Searching...
No Matches
particle_meter_sds011.h
1// SPDX-FileCopyrightText: malarz
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4// Dependencies:
5// https://github.com/boeserfrosch/GuL_NovaFitness/tree/main
6
7#ifndef SRC_SUPLA_SENSOR_PARTICLE_METER_SDS011_H_
8#define SRC_SUPLA_SENSOR_PARTICLE_METER_SDS011_H_
9
10#include <supla/sensor/particle_meter.h>
11#include <SDS011.h>
12
13namespace Supla {
14namespace Sensor {
15
17 public:
18 // rx_pin, tx_pin: pins to which the sensor is connected
19 // refresh: time between readings (in minutes: 1-1440)
20 explicit ParticleMeterSDS011(int rx_pin, int tx_pin, int refresh = 3)
21 : ParticleMeter() {
22 rxPin = rx_pin;
23 txPin = tx_pin;
24 if (refresh < 1) {
25 refresh = 10;
26 } else if (refresh > 1440) {
27 refresh = 10;
28 }
29 refreshIntervalMs = refresh * 60 * 1000;
30
31 // create GPM channel for PM2.5
32 createPM2_5Channel();
33 // create GPM channel for PM10
34 createPM10Channel();
35 }
36
37 void onInit() override {
38 // Setup and create instance of the PM1006K driver
39 // The baud rate for the serial connection must be PM1006K::BAUD_RATE.
40 Serial1.begin(9600, SERIAL_8N1, rxPin, txPin);
41 sensor = new GuL::SDS011(Serial1);
42 sensor->setToActiveReporting();
43 }
44
45 void iterateAlways() override {
46 if (millis() - lastReadTime > refreshIntervalMs) {
47 sensor->read();
48 float valuePM2_5 = sensor->getPM2_5();
49 float valuePM10 = sensor->getPM10();
50
51 SUPLA_LOG_DEBUG("SDS011: PM2.5 read: %.0f", valuePM2_5);
52 SUPLA_LOG_DEBUG("SDS011: PM10 read: %.0f", valuePM10);
53
54 if (valuePM2_5 < 0 || valuePM10 < 0) {
55 if (invalidCounter < 3) {
56 invalidCounter++;
57 } else {
58 pm2_5value = NAN;
59 pm10value = NAN;
60 }
61 } else {
62 invalidCounter = 0;
63 pm2_5value = valuePM2_5;
64 pm10value = valuePM10;
65 }
66
67 lastReadTime = millis();
68 pm2_5channel->setValue(pm2_5value);
69 pm10channel->setValue(pm10value);
70 }
71 }
72
73 protected:
74 GuL::SDS011* sensor;
75 int rxPin = 0;
76 int txPin = 0;
77 uint32_t refreshIntervalMs = 600000;
78 uint32_t lastReadTime = 0;
79 int invalidCounter = 0;
80};
81
82} // namespace Sensor
83} // namespace Supla
84
85#endif // SRC_SUPLA_SENSOR_PARTICLE_METER_SDS011_H_
virtual void setValue(const double &value)
Method used to set new value for channel with driver which accepts MeasurementDriver::setValue() meth...
Definition general_purpose_channel_base.cpp:560
Definition particle_meter_sds011.h:16
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition particle_meter_sds011.h:37
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition particle_meter_sds011.h:45
Definition particle_meter.h:11