Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add read frequency check #6

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions examples/AM2302_sensor_health_check/AM2302_sensor_health_check.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand All @@ -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());
Expand Down
13 changes: 10 additions & 3 deletions src/AM2302-Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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}
{}

/**
Expand All @@ -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 {
Expand All @@ -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 ===
// ****************************
Expand Down
5 changes: 4 additions & 1 deletion src/AM2302-Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
Expand Down