supla-device
Loading...
Searching...
No Matches
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/ricki-z/SDS011
21
22#ifndef SRC_SUPLA_SENSOR_SDS011_H_
23#define SRC_SUPLA_SENSOR_SDS011_H_
24
25#include <supla/sensor/general_purpose_measurement.h>
26
27#include <SDS011.h>
28
29namespace Supla {
30namespace Sensor {
31class sds011 : public Supla::Element {
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 sds011(int rx_pin, int tx_pin, int refresh = 10) {
36 if (refresh < 1) {
37 refresh = 10;
38 } else if (refresh > 1440) {
39 refresh = 10;
40 }
41 refreshIntervalMs = refresh * 60 * 1000;
42 sensor.begin(rx_pin, tx_pin);
43
44 pm25channel = new GeneralPurposeMeasurement();
45 pm25channel->setDefaultUnitAfterValue("μg/m³");
46 pm25channel->setInitialCaption("PM 2.5");
47 pm25channel->getChannel()->setDefaultIcon(8);
48 pm25channel->setDefaultValuePrecision(1);
49
50 pm10channel = new GeneralPurposeMeasurement();
51 pm10channel->setDefaultUnitAfterValue("μg/m³");
52 pm10channel->setInitialCaption("PM 10");
53 pm10channel->getChannel()->setDefaultIcon(8);
54 pm10channel->setDefaultValuePrecision(1);
55 }
56
57 void iterateAlways() override {
58 if (millis() - lastReadTime > refreshIntervalMs) {
59 float pm25 = NAN;
60 float pm10 = NAN;
61 sensor.read(&pm25, &pm10);
62 lastReadTime = millis();
63
64 pm25channel->setValue(pm25);
65 pm10channel->setValue(pm10);
66 }
67 }
68
69 protected:
70 ::SDS011 sensor;
71 uint32_t refreshIntervalMs = 600000;
72 uint32_t lastReadTime = 0;
73
74 GeneralPurposeMeasurement *pm25channel = nullptr;
75 GeneralPurposeMeasurement *pm10channel = nullptr;
76};
77
78} // namespace Sensor
79} // namespace Supla
80
81#endif // SRC_SUPLA_SENSOR_SDS011_H_
Base class for all elements of SuplaDevice.
Definition element.h:33
static Element * begin()
Returns first Element (based on creation order)
Definition element.cpp:51
Definition general_purpose_measurement.h:26
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition sds011.h:57