Skip to content

Commit 631f20b

Browse files
committed
improve stability by introducing a watchdog
1 parent 1b51d26 commit 631f20b

1 file changed

Lines changed: 34 additions & 6 deletions

File tree

  • examples/demo/I2C/MHZ19XI2C-SLV/src

examples/demo/I2C/MHZ19XI2C-SLV/src/main.cpp

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
#include <Wire.h>
44
#include <MHZ19X.h>
55
#include <MHZ19XI2C.hpp>
6+
#include <avr/wdt.h>
7+
8+
constexpr bool enableSelfCalibrationInitialValue = true;
69

710
// MHZ19XI2C_register_t::enable_self_calibration
8-
volatile bool enableSelfCalibration = true;
11+
volatile bool enableSelfCalibration = enableSelfCalibrationInitialValue;
912

1013
// MHZ19XI2C_register_t::latest_co2ppm
1114
uint16_t latestCO2ppm = 0;
@@ -16,6 +19,11 @@ volatile uint8_t getCO2ConcentrationIntervalInSeconds = 5;
1619
// MHZ19XI2C_register_t::error_code
1720
MHZ19X_error_t getCO2ConcentrationResult = MHZ19X_error_t::success;
1821

22+
// If errors on measurement reading occur consecutively beyond the following count,
23+
// a software reset using watchdog will be triggered as there could be
24+
// an inconsistency in serial reading.
25+
constexpr size_t MaxConsecutiveErrorCount = 10;
26+
1927
auto sensor = MHZ19C<PIN_PA6, PIN_PA7>();
2028

2129
// requested register address for reading
@@ -26,20 +34,28 @@ void requestEvent();
2634

2735
void setup()
2836
{
37+
// enable 1-seconds watchdog timer to trigger a software reset if any operation hangs
38+
wdt_enable(WDTO_1S);
39+
2940
sensor.begin();
3041

31-
auto switchSelfCalibrationTo = enableSelfCalibration;
42+
sensor.switchSelfCalibration(enableSelfCalibrationInitialValue);
3243

33-
sensor.switchSelfCalibration(switchSelfCalibrationTo);
44+
wdt_reset();
3445

3546
Wire.begin(MHZ19XI2C_DEVICE_ADDRESS);
3647
Wire.onReceive(receiveEvent);
3748
Wire.onRequest(requestEvent);
49+
50+
wdt_reset();
3851
}
3952

4053
void loop()
4154
{
42-
static bool isSelfCalibrationEnabled = enableSelfCalibration;
55+
static bool isSelfCalibrationEnabled = enableSelfCalibrationInitialValue;
56+
static size_t getCO2ConcentrationConsecutiveErrorCount = 0;
57+
58+
wdt_reset();
4359

4460
noInterrupts();
4561

@@ -60,15 +76,27 @@ void loop()
6076

6177
delay(intervalInMilliseconds);
6278

79+
wdt_reset();
80+
6381
// retrieve the latest measurement value from sensor
6482
uint16_t co2ppm = 0;
6583

6684
getCO2ConcentrationResult = sensor.getCO2Concentration(co2ppm);
6785

68-
if (MHZ19X_error_t::success == getCO2ConcentrationResult)
86+
if (MHZ19X_error_t::success == getCO2ConcentrationResult) {
87+
getCO2ConcentrationConsecutiveErrorCount = 0; // reset the number of consecutive errors
6988
latestCO2ppm = co2ppm; // set the latest measurement value
70-
else
89+
}
90+
else {
91+
getCO2ConcentrationConsecutiveErrorCount++; // increment the number of consecutive errors
7192
latestCO2ppm = 0;
93+
}
94+
95+
if (MaxConsecutiveErrorCount <= getCO2ConcentrationConsecutiveErrorCount) {
96+
// trigger a software reset using watchdog timer
97+
wdt_enable(WDTO_15MS);
98+
delay(10000);
99+
}
72100
}
73101

74102
void receiveEvent(int numberOfBytes)

0 commit comments

Comments
 (0)