From 21190f0e08161af62fa620a96582dc085919a071 Mon Sep 17 00:00:00 2001 From: dakkar Date: Sun, 27 Mar 2022 13:32:52 +0100 Subject: fix pm low power reading --- pm.h | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/pm.h b/pm.h index 9c8a508..4bfc10c 100644 --- a/pm.h +++ b/pm.h @@ -19,11 +19,13 @@ private: uint8_t auto_clean_days; unsigned long last_measurment_millis; unsigned long read_interval_millis; + bool awake; public: PM(uint8_t acd=4, unsigned long ri=30000) : auto_clean_days(acd), last_measurment_millis(0), - read_interval_millis(ri) + read_interval_millis(ri), + awake(false) {} void start() { @@ -47,6 +49,8 @@ public: if (ret < 0) { Serial.print("PM starting measurement error\n"); } + + awake=true; } bool dataReady() { @@ -55,13 +59,19 @@ public: // have we read recently? unsigned long now=millis(); - // that first check is in case of overflow & rollover - if (now > last_measurment_millis && (now - last_measurment_millis) < read_interval_millis) { + if ( + last_measurment_millis > 0 && // have we measured once at least? + now > last_measurment_millis && // has there been no overflow & rollover? + (now - last_measurment_millis) < read_interval_millis // is it too soon? + ) { return false; } - sps30_wake_up(); - sps30_start_measurement(); + if (!awake) { + sps30_wake_up(); + sps30_start_measurement(); + awake=true; + } ret = sps30_read_data_ready(&data_ready); if (ret < 0) { @@ -90,5 +100,6 @@ public: last_measurment_millis = millis(); sps30_stop_measurement(); sps30_sleep(); + awake=false; } }; -- cgit v1.2.3