From 97622fbe3d1b02b3b38be46f34580f6a17406bdc Mon Sep 17 00:00:00 2001 From: hasenradball Date: Fri, 15 Dec 2023 16:52:22 +0100 Subject: [PATCH] add read frequency check --- .../AM2302_sensor_health_check.ino | 15 +++++++++++---- src/AM2302-Sensor.cpp | 13 ++++++++++--- src/AM2302-Sensor.h | 5 ++++- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/examples/AM2302_sensor_health_check/AM2302_sensor_health_check.ino b/examples/AM2302_sensor_health_check/AM2302_sensor_health_check.ino index 65bf4c5..99921d5 100644 --- a/examples/AM2302_sensor_health_check/AM2302_sensor_health_check.ino +++ b/examples/AM2302_sensor_health_check/AM2302_sensor_health_check.ino @@ -20,6 +20,7 @@ void setup() { } Serial.print(F("\n >>> AM2302-sensor Health Check <<<\n\n")); + // put your setup code here, to run once: // set pin and check for sensor if (am2302.begin()) { // this delay is needed to receive valid data, @@ -35,7 +36,7 @@ void setup() { } void loop() { - static int checksum_err{0}, timeout_err {0}; + static int checksum_err{0}, timeout_err {0}, read_freq_err {0}; // put your main code here, to run repeatedly: auto status = am2302.read(); if (status == AM2302::AM2302_ERROR_CHECKSUM) { @@ -44,16 +45,22 @@ void loop() { if (status == AM2302::AM2302_ERROR_TIMEOUT) { ++timeout_err; } + if (status == AM2302::AM2302_ERROR_READ_FREQ) { + ++read_freq_err; + } Serial.print("\n\n"); - Serial.print("Sensor status: "); + Serial.print("Sensor status: "); Serial.println(status); - Serial.print("Number checksum errors: "); + Serial.print("Number checksum errors: "); Serial.println(checksum_err); - Serial.print("Number timeout errors: "); + Serial.print("Number timeout errors: "); Serial.println(timeout_err); + Serial.print("Number read freq errors: "); + Serial.println(read_freq_err); + Serial.print("\n\n"); Serial.print("Temperature: "); Serial.println(am2302.get_Temperature()); diff --git a/src/AM2302-Sensor.cpp b/src/AM2302-Sensor.cpp index e97ff0b..f2bcc91 100644 --- a/src/AM2302-Sensor.cpp +++ b/src/AM2302-Sensor.cpp @@ -14,7 +14,7 @@ * * @param pin Pin for AM2302 sensor */ -AM2302::AM2302_Sensor::AM2302_Sensor(uint8_t pin) : _pin{pin} +AM2302::AM2302_Sensor::AM2302_Sensor(uint8_t pin) : _millis_last_read{0}, _pin{pin} {} /** @@ -28,10 +28,12 @@ bool AM2302::AM2302_Sensor::begin() { // required delay() for a secure sensor check, // if you reset the mcu very fast one after another auto tic{millis()}; - while ((millis() - tic) < 2000U) { + while ( millis() - tic < READ_FREQUENCY ) { yield(); } - if (read() == AM2302_READ_OK) { + auto status{read()}; + _millis_last_read = millis(); + if (status == AM2302_READ_OK) { return true; } else { @@ -41,6 +43,11 @@ bool AM2302::AM2302_Sensor::begin() { int8_t AM2302::AM2302_Sensor::read() { + // check read frequency + if ( millis() - _millis_last_read < READ_FREQUENCY) { + return AM2302_ERROR_READ_FREQ; + } + _millis_last_read = millis(); // ***************************** // === send start sequence === // **************************** diff --git a/src/AM2302-Sensor.h b/src/AM2302-Sensor.h index 599b1d5..bf08aea 100644 --- a/src/AM2302-Sensor.h +++ b/src/AM2302-Sensor.h @@ -18,9 +18,11 @@ namespace AM2302 { constexpr int8_t AM2302_READ_OK {0}; constexpr int8_t AM2302_ERROR_CHECKSUM {-1}; constexpr int8_t AM2302_ERROR_TIMEOUT {-2}; + constexpr int8_t AM2302_ERROR_READ_FREQ {-3}; // define timeout in 100 µs - constexpr uint8_t READ_TIMEOUT {100U}; + constexpr uint8_t READ_TIMEOUT {100U}; + constexpr uint16_t READ_FREQUENCY {2000U}; class AM2302_Sensor { @@ -32,6 +34,7 @@ namespace AM2302 { float get_Humidity() const {return _hum * 0.1F;} private: + unsigned long _millis_last_read; uint16_t _hum {0}; int16_t _temp {0}; uint8_t _pin;