From 05594441b82275c659607cf71d9af2f92970a612 Mon Sep 17 00:00:00 2001 From: Phat Nguyen Date: Wed, 7 Feb 2024 09:18:02 +0700 Subject: [PATCH] Update sht3x --- examples/TestSht3x/TestSht3x.ino | 19 ++++------ src/sht/sht3x.cpp | 64 ++++++++++++++------------------ src/sht/sht3x.h | 4 +- 3 files changed, 38 insertions(+), 49 deletions(-) diff --git a/examples/TestSht3x/TestSht3x.ino b/examples/TestSht3x/TestSht3x.ino index 7684fdc..d7d6c21 100644 --- a/examples/TestSht3x/TestSht3x.ino +++ b/examples/TestSht3x/TestSht3x.ino @@ -14,21 +14,16 @@ void setup() { } void loop() { - float temp = ag.sht3x.getTemperature(); - if (temp <= -256.0f) { - Serial.println("Get temperature failed"); - } else { + if (ag.sht3x.measure()) { + float hum = ag.sht3x.getRelativeHumidity(); + float temp = ag.sht3x.getTemperature(); + Serial.printf("Get temperature: %f\r\n", temp); - } - - float hum = ag.sht3x.getRelativeHumidity(); - if (hum < 0) { - Serial.println("Get humidity failed"); + Serial.printf(" Get humidity: %f\r\n", hum); } else { - Serial.printf("Get humidity: %f\r\n", hum); + Serial.println("Measure sht3x failed"); } - - delay(1000); + delay(5000); } void failedHandler(String msg) { diff --git a/src/sht/sht3x.cpp b/src/sht/sht3x.cpp index 62aa5e3..acbc748 100644 --- a/src/sht/sht3x.cpp +++ b/src/sht/sht3x.cpp @@ -35,26 +35,6 @@ bool Sht3x::boardSupported(void) { return true; } -/** - * @brief Get temperature and humidity data - * - * @param temp Tempreature read out - * @param hum Humidity read out - * @return true Success - * @return false Failure - */ -bool Sht3x::measure(float &temp, float &hum) { - if (isBegin() == false) { - return false; - } - - if (sht3x()->measureSingleShot(REPEATABILITY_MEDIUM, false, temp, hum) == - NO_ERROR) { - return true; - } - return false; -} - /** * @brief Construct a new Sht 3x:: Sht 3x object * @@ -106,8 +86,15 @@ bool Sht3x::begin(TwoWire &wire) { /** Create sensor and init */ _sensor = new SensirionI2cSht3x(); sht3x()->begin(wire, SHT30_I2C_ADDR_44); - if (sht3x()->softReset() != NO_ERROR) { - AgLog("Reset sensor fail, look like sensor is not on I2C bus"); + sht3x()->stopMeasurement(); + delay(1); + sht3x()->softReset(); + delay(100); + + uint16_t statusRegister = 0; + uint16_t err = sht3x()->readStatusRegister(statusRegister); + if (err != NO_ERROR) { + AgLog("Read status register invalid"); return false; } @@ -139,25 +126,30 @@ void Sht3x::end(void) { * @return float value <= 256.0f is invalid, that mean sensor has issue or * communication to sensor not worked as well */ -float Sht3x::getTemperature(void) { - float temp; - float hum; - if (measure(temp, hum)) { - return temp; - } - return -256.0f; -} +float Sht3x::getTemperature(void) { return temp; } /** * @brief Get humidity * * @return float Percent(0 - 100), value < 0 is invalid. */ -float Sht3x::getRelativeHumidity(void) { - float temp; - float hum; - if (measure(temp, hum)) { - return hum; +float Sht3x::getRelativeHumidity(void) { return hum; } + +/** + * @brief Measure temperature and humidity. Must call @getTemperature and + * @getRelativeHumidity after this function return success + * + * @return true Success + * @return false Failure + */ +bool Sht3x::measure(void) { + if (isBegin() == false) { + return false; } - return -1.0f; + + if (sht3x()->measureSingleShot(REPEATABILITY_MEDIUM, false, temp, hum) == + NO_ERROR) { + return true; + } + return false; } diff --git a/src/sht/sht3x.h b/src/sht/sht3x.h index 971aaa6..b41db02 100644 --- a/src/sht/sht3x.h +++ b/src/sht/sht3x.h @@ -20,10 +20,11 @@ private: const char *TAG = "SHT3x"; #else #endif + float temp; + float hum; bool isBegin(void); bool boardSupported(void); - bool measure(float &temp, float &hum); public: Sht3x(BoardType type); @@ -35,6 +36,7 @@ public: #endif bool begin(TwoWire &wire); void end(void); + bool measure(void); float getTemperature(void); float getRelativeHumidity(void); };