Skip to content

Commit 19d208e

Browse files
committed
moved watchdog routine to an example
1 parent a4b0a00 commit 19d208e

File tree

4 files changed

+109
-71
lines changed

4 files changed

+109
-71
lines changed
Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,46 @@
1010
#include "sdpsensor.h"
1111

1212

13-
/* If you don't know the I2C address of a sensor,
14-
* scan all addresses with an I2C scanner first.
13+
/* An SDP sensor instance.
14+
* If you don't know the I2C address of a sensor,
15+
* scan all addresses with an I2C scanner first.
1516
*/
1617
SDPSensor sdp(0x21);
1718

1819

19-
void setup() {
20-
Serial.begin(115200);
21-
delay(1000); // let serial console settle
22-
sdp.initI2C(19, 23); // same as Wire.begin(19, 23)
20+
/* Try until succeeds. */
21+
void initSensorUntilSuccess() {
22+
while (sdp.stopContinuous() != ESP_OK); // sdp.reset() is also possible
23+
while (sdp.begin() != ESP_OK);
24+
while (sdp.startContinuous() != ESP_OK);
25+
}
26+
27+
28+
/* Try until the first error occurs. */
29+
void initSensorSingleTrial() {
2330
sdp.stopContinuous(); // sdp.reset() is also possible
2431
sdp.begin();
2532
sdp.startContinuous();
2633
}
2734

2835

36+
void setup() {
37+
Serial.begin(115200);
38+
delay(1000); // let serial console settle
39+
sdp.initI2C(19, 23); // same as Wire.begin(SDA, SCL)
40+
41+
// choose how you want to initialize the sensor
42+
initSensorSingleTrial();
43+
//initSensorUntilSuccess();
44+
}
45+
46+
2947
void loop() {
3048
delay(1000);
3149
int16_t pressure;
3250
esp_err_t err = sdp.readDiffPressure(&pressure);
51+
if (err == ESP_OK) {
52+
// success; process the 'pressure' here
53+
}
3354
ESP_LOGI("sdp", "raw pressure: %d, err code: %s", pressure, esp_err_to_name(err));
34-
35-
/*
36-
* Optionally, check the last result code.
37-
* If enough errors occur, a software reset is issued.
38-
*/
39-
sdp.watchdogCheck(err);
4055
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* An example to use an SDP3x sensor with an ESP32 board
3+
* with a watchdog that barks each time three errors are
4+
* encountered in a row.
5+
*
6+
* Prior setup:
7+
* 1) install the arduino-esp library
8+
* 2) select your ESP board in "Tools" -> "Board"
9+
* 3) select the right port in "Tools" -> "Port"
10+
* 4) turn on the logs by setting "Tools" -> "Core Debug Level" -> "Info"
11+
*/
12+
#include "sdpsensor.h"
13+
14+
15+
/**
16+
* An SDP Sensor instance.
17+
* If you don't know the I2C address of a sensor,
18+
* scan all addresses with an I2C scanner first.
19+
*/
20+
SDPSensor sdp(0x21);
21+
22+
/* Failed reads count */
23+
uint32_t failsCount = 0;
24+
25+
/* Max SDP successive failed reads before a SW reset */
26+
const uint32_t maxSuccessiveFailsCount = 3;
27+
28+
/**
29+
* Check the error code status of the last read measurement.
30+
* If enough errors occurred, a software reset is issued.
31+
*
32+
* Usage:
33+
* esp_err_t err = sdp.readContinuousRaw(&diff_pressure);
34+
* sdp.watchdogCheck(err);
35+
*
36+
* @param status - the error code of the last measurement
37+
*/
38+
void watchdogCheck(esp_err_t status) {
39+
if (status == ESP_OK) {
40+
failsCount = 0;
41+
} else {
42+
failsCount++;
43+
}
44+
45+
if (failsCount >= maxSuccessiveFailsCount) {
46+
ESP_LOGW("watchdog", "SDPWatchdog barked. Resetting...");
47+
esp_err_t err = ESP_FAIL;
48+
do {
49+
err = sdp.reset(); // sdp.stopContinuous() is a safer version
50+
if (err == ESP_OK) {
51+
err = sdp.startContinuous();
52+
}
53+
} while (err != ESP_OK);
54+
}
55+
56+
}
57+
58+
59+
void setup() {
60+
Serial.begin(115200);
61+
delay(1000); // let serial console settle
62+
sdp.initI2C(19, 23); // same as Wire.begin(SDA, SCL)
63+
64+
// try until succeeds
65+
while (sdp.stopContinuous() != ESP_OK); // sdp.reset() is also possible
66+
while (sdp.begin() != ESP_OK);
67+
while (sdp.startContinuous() != ESP_OK);
68+
}
69+
70+
71+
void loop() {
72+
delay(1000);
73+
int16_t pressure;
74+
esp_err_t err = sdp.readDiffPressure(&pressure);
75+
ESP_LOGI("sdp", "raw pressure: %d, err code: %s", pressure, esp_err_to_name(err));
76+
77+
/*
78+
* Optionally, check the last result code.
79+
* If enough errors occur, a software reset is issued.
80+
*/
81+
watchdogCheck(err);
82+
}

sdpsensor.cpp

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ SDPSensor::SDPSensor(uint8_t i2c_addr, i2c_port_t i2c_port) {
5656
this->i2c_addr = i2c_addr;
5757
this->i2c_port = i2c_port;
5858
this->pressureScale = 0; // will be read & set in the begin()
59-
this->maxSuccessiveFailsCount = 2;
60-
this->failsCount = 0;
6159
this->initialized = false;
6260
}
6361

@@ -246,38 +244,6 @@ esp_err_t SDPSensor::reset() {
246244
}
247245

248246

249-
void SDPSensor::watchdogSetParams(uint32_t maxSuccessiveFailsCount) {
250-
this->maxSuccessiveFailsCount = maxSuccessiveFailsCount;
251-
}
252-
253-
254-
void SDPSensor::watchdogCheck(esp_err_t status) {
255-
if (!initialized) return;
256-
257-
if (status == ESP_OK || status == ESP_ERR_INVALID_CRC) {
258-
// invalid CRC does not mean that the sensor is not functioning
259-
failsCount = 0;
260-
} else {
261-
failsCount++;
262-
}
263-
264-
if (failsCount >= maxSuccessiveFailsCount) {
265-
ESP_LOGW(TAG_SDPSENSOR, "SDPWatchdog barked. Resetting...");
266-
// flush i2c buffers before resetting
267-
i2c_reset_tx_fifo(i2c_port);
268-
i2c_reset_rx_fifo(i2c_port);
269-
270-
esp_err_t err = ESP_FAIL;
271-
do {
272-
err = SDPSensor::reset();
273-
if (err == ESP_OK) {
274-
err = SDPSensor::startContinuous();
275-
}
276-
} while (err != ESP_OK);
277-
}
278-
}
279-
280-
281247
esp_err_t SDPSensor::readDiffPressure(int16_t *diffPressureRaw) {
282248
if (!initialized) return ESP_ERR_INVALID_STATE;
283249

sdpsensor.h

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ class SDPSensor {
1616
uint8_t i2c_addr; /* I2C address */
1717
i2c_port_t i2c_port; /* I2C master port */
1818
uint16_t pressureScale; /* Diff pressure scale */
19-
uint32_t failsCount; /* [watchdog] SDP successive failed reads count */
20-
uint32_t maxSuccessiveFailsCount; /* [watchdog] SDP max successive failed reads before SW reset */
2119
bool initialized;
2220
public:
2321

@@ -92,29 +90,6 @@ class SDPSensor {
9290
esp_err_t reset();
9391

9492

95-
/**
96-
* Set watchdog params to be used in the `watchdogCheck()` function.
97-
* Has no effect if the user does not call the `watchdogCheck()` function.
98-
*
99-
* @param maxSuccessiveFailsCount - the maximum successive failed reads
100-
* until the reset command is issued.
101-
*/
102-
void watchdogSetParams(uint32_t maxSuccessiveFailsCount);
103-
104-
105-
/**
106-
* Check the error code status of the last read measurement.
107-
* If enough errors occurred, a software reset is issued.
108-
*
109-
* Usage:
110-
* esp_err_t err = sdp.readContinuousRaw(&diff_pressure);
111-
* sdp.watchdogCheck(err);
112-
*
113-
* @param status - the error code of the last measurement
114-
*/
115-
void watchdogCheck(esp_err_t status);
116-
117-
11893
/**
11994
* Read the raw (unnormalized) differential pressure value and
12095
* save the result in `diffPressureRaw`. To convert it to a

0 commit comments

Comments
 (0)