@@ -34,8 +34,6 @@ class SensorPlantowerPMS: public Sensor {
3434 int8_t _rx_pin = 3 ;
3535 PMS *_pms;
3636 PMS::DATA _data;
37- bool _valuesRead = false ;
38- bool _valuesReadError = false ;
3937
4038public:
4139 SensorPlantowerPMS (int8_t rxpin, int8_t txpin, uint8_t child_id = 0 ): Sensor(rxpin) {
@@ -58,35 +56,30 @@ class SensorPlantowerPMS: public Sensor {
5856
5957 // define what to do during loop
6058 void onLoop (Child* child) {
59+ // for the first child only, read the values
6160 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 ;
7164 }
72- _valuesRead = true ;
7365 }
66+ // get the value based on the requested child
7467 int val = 0 ;
68+ // PM1.0 values
7569 if (child == children.get (1 )) {
76- // PM1.0 values
7770 val = _data.PM_AE_UG_1_0 ;
71+ // PM 2.5 values
7872 } else if (child == children.get (2 )) {
79- // PM 2.5 values
8073 val = _data.PM_AE_UG_2_5 ;
74+ // PM 10.0 values
8175 } else if (child == children.get (3 )) {
82- // PM 10.0 values
8376 val = _data.PM_AE_UG_10_0 ;
8477 } else {
8578 debug (PSTR (" !" LOG_SENSOR " %s:CHILD\n " ),_name);
8679 return ;
8780 }
88- // store the value
89- child->setValue (val);
81+ // send the value
82+ if (val >= 0 ) child->setValue (val);
9083 };
9184};
9285#endif
0 commit comments