@@ -34,8 +34,6 @@ class SensorPlantowerPMS: public Sensor {
34
34
int8_t _rx_pin = 3 ;
35
35
PMS *_pms;
36
36
PMS::DATA _data;
37
- bool _valuesRead = false ;
38
- bool _valuesReadError = false ;
39
37
40
38
public:
41
39
SensorPlantowerPMS (int8_t rxpin, int8_t txpin, uint8_t child_id = 0 ): Sensor(rxpin) {
@@ -58,35 +56,30 @@ class SensorPlantowerPMS: public Sensor {
58
56
59
57
// define what to do during loop
60
58
void onLoop (Child* child) {
59
+ // for the first child only, read the values
61
60
if (child == children.get (1 )) {
62
- _valuesRead = false ;
63
- _valuesReadError = false ;
64
- }
65
- // Read the ppm values
66
- if (!_valuesRead || _valuesReadError) {
67
- _valuesReadError = !_pms->readUntil (_data, 1000 );
68
- if (_valuesReadError) {
69
- debug (PSTR (" !" LOG_SENSOR " %s:READ\n " ),_name);
70
- return ;
61
+ // keep reading untile we get valid data
62
+ while (true ) {
63
+ if (_pms->read (_data)) break ;
71
64
}
72
- _valuesRead = true ;
73
65
}
66
+ // get the value based on the requested child
74
67
int val = 0 ;
68
+ // PM1.0 values
75
69
if (child == children.get (1 )) {
76
- // PM1.0 values
77
70
val = _data.PM_AE_UG_1_0 ;
71
+ // PM 2.5 values
78
72
} else if (child == children.get (2 )) {
79
- // PM 2.5 values
80
73
val = _data.PM_AE_UG_2_5 ;
74
+ // PM 10.0 values
81
75
} else if (child == children.get (3 )) {
82
- // PM 10.0 values
83
76
val = _data.PM_AE_UG_10_0 ;
84
77
} else {
85
78
debug (PSTR (" !" LOG_SENSOR " %s:CHILD\n " ),_name);
86
79
return ;
87
80
}
88
- // store the value
89
- child->setValue (val);
81
+ // send the value
82
+ if (val >= 0 ) child->setValue (val);
90
83
};
91
84
};
92
85
#endif
0 commit comments