mirror of
https://github.com/airgradienthq/arduino.git
synced 2025-07-15 17:52:08 +02:00
Update sht3x
This commit is contained in:
@ -14,21 +14,16 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
float temp = ag.sht3x.getTemperature();
|
if (ag.sht3x.measure()) {
|
||||||
if (temp <= -256.0f) {
|
float hum = ag.sht3x.getRelativeHumidity();
|
||||||
Serial.println("Get temperature failed");
|
float temp = ag.sht3x.getTemperature();
|
||||||
} else {
|
|
||||||
Serial.printf("Get temperature: %f\r\n", temp);
|
Serial.printf("Get temperature: %f\r\n", temp);
|
||||||
}
|
Serial.printf(" Get humidity: %f\r\n", hum);
|
||||||
|
|
||||||
float hum = ag.sht3x.getRelativeHumidity();
|
|
||||||
if (hum < 0) {
|
|
||||||
Serial.println("Get humidity failed");
|
|
||||||
} else {
|
} else {
|
||||||
Serial.printf("Get humidity: %f\r\n", hum);
|
Serial.println("Measure sht3x failed");
|
||||||
}
|
}
|
||||||
|
delay(5000);
|
||||||
delay(1000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void failedHandler(String msg) {
|
void failedHandler(String msg) {
|
||||||
|
@ -35,26 +35,6 @@ bool Sht3x::boardSupported(void) {
|
|||||||
return true;
|
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
|
* @brief Construct a new Sht 3x:: Sht 3x object
|
||||||
*
|
*
|
||||||
@ -106,8 +86,15 @@ bool Sht3x::begin(TwoWire &wire) {
|
|||||||
/** Create sensor and init */
|
/** Create sensor and init */
|
||||||
_sensor = new SensirionI2cSht3x();
|
_sensor = new SensirionI2cSht3x();
|
||||||
sht3x()->begin(wire, SHT30_I2C_ADDR_44);
|
sht3x()->begin(wire, SHT30_I2C_ADDR_44);
|
||||||
if (sht3x()->softReset() != NO_ERROR) {
|
sht3x()->stopMeasurement();
|
||||||
AgLog("Reset sensor fail, look like sensor is not on I2C bus");
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,25 +126,30 @@ void Sht3x::end(void) {
|
|||||||
* @return float value <= 256.0f is invalid, that mean sensor has issue or
|
* @return float value <= 256.0f is invalid, that mean sensor has issue or
|
||||||
* communication to sensor not worked as well
|
* communication to sensor not worked as well
|
||||||
*/
|
*/
|
||||||
float Sht3x::getTemperature(void) {
|
float Sht3x::getTemperature(void) { return temp; }
|
||||||
float temp;
|
|
||||||
float hum;
|
|
||||||
if (measure(temp, hum)) {
|
|
||||||
return temp;
|
|
||||||
}
|
|
||||||
return -256.0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get humidity
|
* @brief Get humidity
|
||||||
*
|
*
|
||||||
* @return float Percent(0 - 100), value < 0 is invalid.
|
* @return float Percent(0 - 100), value < 0 is invalid.
|
||||||
*/
|
*/
|
||||||
float Sht3x::getRelativeHumidity(void) {
|
float Sht3x::getRelativeHumidity(void) { return hum; }
|
||||||
float temp;
|
|
||||||
float hum;
|
/**
|
||||||
if (measure(temp, hum)) {
|
* @brief Measure temperature and humidity. Must call @getTemperature and
|
||||||
return hum;
|
* @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;
|
||||||
}
|
}
|
||||||
|
@ -20,10 +20,11 @@ private:
|
|||||||
const char *TAG = "SHT3x";
|
const char *TAG = "SHT3x";
|
||||||
#else
|
#else
|
||||||
#endif
|
#endif
|
||||||
|
float temp;
|
||||||
|
float hum;
|
||||||
|
|
||||||
bool isBegin(void);
|
bool isBegin(void);
|
||||||
bool boardSupported(void);
|
bool boardSupported(void);
|
||||||
bool measure(float &temp, float &hum);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Sht3x(BoardType type);
|
Sht3x(BoardType type);
|
||||||
@ -35,6 +36,7 @@ public:
|
|||||||
#endif
|
#endif
|
||||||
bool begin(TwoWire &wire);
|
bool begin(TwoWire &wire);
|
||||||
void end(void);
|
void end(void);
|
||||||
|
bool measure(void);
|
||||||
float getTemperature(void);
|
float getTemperature(void);
|
||||||
float getRelativeHumidity(void);
|
float getRelativeHumidity(void);
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user