supla-device
Toggle main menu visibility
Loading...
Searching...
No Matches
src
supla
sensor
particle_meter_sds011.h
1
/*
2
Copyright (C) malarz
3
4
This program is free software; you can redistribute it and/or
5
modify it under the terms of the GNU General Public License
6
as published by the Free Software Foundation; either version 2
7
of the License, or (at your option) any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
*/
18
19
// Dependencies:
20
// https://github.com/boeserfrosch/GuL_NovaFitness/tree/main
21
22
#ifndef SRC_SUPLA_SENSOR_PARTICLE_METER_SDS011_H_
23
#define SRC_SUPLA_SENSOR_PARTICLE_METER_SDS011_H_
24
25
#include <supla/sensor/particle_meter.h>
26
#include <SDS011.h>
27
28
namespace
Supla {
29
namespace
Sensor {
30
31
class
ParticleMeterSDS011 :
public
Supla::Sensor::ParticleMeter
{
32
public
:
33
// rx_pin, tx_pin: pins to which the sensor is connected
34
// refresh: time between readings (in minutes: 1-1440)
35
explicit
ParticleMeterSDS011(
int
rx_pin,
int
tx_pin,
int
refresh = 3)
36
: ParticleMeter() {
37
rxPin = rx_pin;
38
txPin = tx_pin;
39
if
(refresh < 1) {
40
refresh = 10;
41
}
else
if
(refresh > 1440) {
42
refresh = 10;
43
}
44
refreshIntervalMs = refresh * 60 * 1000;
45
46
// create GPM channel for PM2.5
47
createPM2_5Channel();
48
// create GPM channel for PM10
49
createPM10Channel();
50
}
51
52
void
onInit
()
override
{
53
// Setup and create instance of the PM1006K driver
54
// The baud rate for the serial connection must be PM1006K::BAUD_RATE.
55
Serial1.begin(9600, SERIAL_8N1, rxPin, txPin);
56
sensor =
new
GuL::SDS011(Serial1);
57
sensor->setToActiveReporting();
58
}
59
60
void
iterateAlways
()
override
{
61
if
(millis() - lastReadTime > refreshIntervalMs) {
62
sensor->read();
63
float
valuePM2_5 = sensor->getPM2_5();
64
float
valuePM10 = sensor->getPM10();
65
66
SUPLA_LOG_DEBUG(
"SDS011: PM2.5 read: %.0f"
, valuePM2_5);
67
SUPLA_LOG_DEBUG(
"SDS011: PM10 read: %.0f"
, valuePM10);
68
69
if
(valuePM2_5 < 0 || valuePM10 < 0) {
70
if
(invalidCounter < 3) {
71
invalidCounter++;
72
}
else
{
73
pm2_5value = NAN;
74
pm10value = NAN;
75
}
76
}
else
{
77
invalidCounter = 0;
78
pm2_5value = valuePM2_5;
79
pm10value = valuePM10;
80
}
81
82
lastReadTime = millis();
83
pm2_5channel->setValue(pm2_5value);
84
pm10channel->setValue(pm10value);
85
}
86
}
87
88
protected
:
89
GuL::SDS011* sensor;
90
int
rxPin = 0;
91
int
txPin = 0;
92
uint32_t refreshIntervalMs = 600000;
93
uint32_t lastReadTime = 0;
94
int
invalidCounter = 0;
95
};
96
97
}
// namespace Sensor
98
}
// namespace Supla
99
100
#endif
// SRC_SUPLA_SENSOR_PARTICLE_METER_SDS011_H_
Supla::Sensor::ParticleMeterSDS011::onInit
void onInit() override
Third method called on element in SuplaDevice.begin().
Definition
particle_meter_sds011.h:52
Supla::Sensor::ParticleMeterSDS011::iterateAlways
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition
particle_meter_sds011.h:60
Supla::Sensor::ParticleMeter
Definition
particle_meter.h:26
Generated by
1.17.0