supla-device
Toggle main menu visibility
Loading...
Searching...
No Matches
src
supla
sensor
particle_meter_pm1006k.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 License898
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/kevinlutzer/Arduino-PM1006K
21
22
#ifndef SRC_SUPLA_SENSOR_PARTICLE_METER_PM1006K_H_
23
#define SRC_SUPLA_SENSOR_PARTICLE_METER_PM1006K_H_
24
25
#include <supla/sensor/particle_meter.h>
26
27
#include <PM1006K.h>
28
29
namespace
Supla {
30
namespace
Sensor {
31
class
ParticleMeterPM1006K
:
public
Supla::Sensor::ParticleMeter
{
32
public
:
39
explicit
ParticleMeterPM1006K
(
int
rx_pin,
int
tx_pin,
int
fan_pin = -1,
40
int
refresh = 600,
int
fan = 15)
41
: ParticleMeter() {
42
rxPin = rx_pin;
43
txPin = tx_pin;
44
if
(refresh < 60) {
45
refresh = 600;
46
}
else
if
(refresh > 86400) {
47
refresh = 600;
48
}
49
if
(fan < 15) {
50
fan = 15;
51
}
else
if
(fan > 120) {
52
fan = 120;
53
}
else
if
(fan > refresh) {
54
fan = refresh;
55
}
56
refreshIntervalMs = refresh * 1000;
57
fanTime = fan * 1000;
58
59
// FAN setup
60
fanPin = fan_pin;
61
if
(fanPin >= 0) {
62
pinMode(fanPin, OUTPUT);
63
fanOff =
false
;
64
digitalWrite(fanPin, HIGH);
65
SUPLA_LOG_DEBUG(
"PM1006K FAN: started & on"
);
66
}
67
68
// create GPM channel for PM2.5
69
createPM2_5Channel();
70
}
71
72
void
onInit
()
override
{
73
// Setup and create instance of the PM1006K driver
74
// The baud rate for the serial connection must be PM1006K::BAUD_RATE.
75
Serial1.begin(PM1006K::BAUD_RATE, SERIAL_8N1, rxPin, txPin);
76
sensor =
new
PM1006K(&Serial1);
77
}
78
79
void
iterateAlways
()
override
{
80
// enable fan fanTime before reading sensor
81
if
(millis() - lastReadTime > refreshIntervalMs-fanTime) {
82
if
((fanPin >= 0) && fanOff) {
83
fanOff =
false
;
84
digitalWrite(fanPin, HIGH);
85
SUPLA_LOG_DEBUG(
"PM1006K FAN: on"
);
86
}
87
}
88
89
if
(millis() - lastReadTime > refreshIntervalMs) {
90
double
valuePM1 = NAN;
91
double
valuePM2_5 = NAN;
92
double
valuePM10 = NAN;
93
if
(!sensor->takeMeasurement()) {
94
SUPLA_LOG_DEBUG(
"PM1006K: failed to take measurement"
);
95
}
else
{
96
valuePM1 = sensor->getPM1_0();
97
SUPLA_LOG_DEBUG(
"PM1006K: PM1 read: %.0f"
, valuePM1);
98
valuePM2_5 = sensor->getPM2_5();
99
SUPLA_LOG_DEBUG(
"PM1006K: PM2.5 read: %.0f"
, valuePM2_5);
100
valuePM10 = sensor->getPM10();
101
SUPLA_LOG_DEBUG(
"PM1006K: PM10 read: %.0f"
, valuePM10);
102
}
103
104
if
(isnan(valuePM2_5) || valuePM2_5 <= 0) {
105
if
(invalidCounter < 3) {
106
invalidCounter++;
107
}
else
{
108
pm1value = NAN;
109
pm2_5value = NAN;
110
pm10value = NAN;
111
}
112
}
else
{
113
invalidCounter = 0;
114
pm1value = valuePM1;
115
pm2_5value = valuePM2_5;
116
pm10value = valuePM10;
117
118
// switch faan off after successfull reading
119
if
((fanPin >= 0) && (refreshIntervalMs !=fanTime)) {
120
fanOff =
true
;
121
digitalWrite(fanPin, LOW);
122
SUPLA_LOG_DEBUG(
"PM1006K FAN: off"
);
123
}
124
}
125
if
(pm1channel !=
nullptr
) {
126
pm1channel->setValue(pm1value);
127
}
128
pm2_5channel->setValue(pm2_5value);
129
if
(pm10channel !=
nullptr
) {
130
pm10channel->setValue(pm10value);
131
}
132
lastReadTime = millis();
133
}
134
}
135
136
protected
:
137
::PM1006K* sensor =
nullptr
;
138
int
fanPin = -1;
139
int
rxPin = 0;
140
int
txPin = 0;
141
bool
fanOff =
true
;
142
uint32_t fanTime = 15;
143
int
invalidCounter = 0;
144
};
145
146
}
// namespace Sensor
147
}
// namespace Supla
148
149
#endif
// SRC_SUPLA_SENSOR_PARTICLE_METER_PM1006K_H_
Supla::Sensor::ParticleMeterPM1006K::iterateAlways
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition
particle_meter_pm1006k.h:79
Supla::Sensor::ParticleMeterPM1006K::onInit
void onInit() override
Third method called on element in SuplaDevice.begin().
Definition
particle_meter_pm1006k.h:72
Supla::Sensor::ParticleMeterPM1006K::ParticleMeterPM1006K
ParticleMeterPM1006K(int rx_pin, int tx_pin, int fan_pin=-1, int refresh=600, int fan=15)
class constructor for PM1006K sensor
Definition
particle_meter_pm1006k.h:39
Supla::Sensor::ParticleMeter
Definition
particle_meter.h:26
Generated by
1.17.0