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() {
|
||||
float temp = ag.sht3x.getTemperature();
|
||||
if (temp <= -256.0f) {
|
||||
Serial.println("Get temperature failed");
|
||||
} else {
|
||||
Serial.printf("Get temperature: %f\r\n", temp);
|
||||
}
|
||||
|
||||
if (ag.sht3x.measure()) {
|
||||
float hum = ag.sht3x.getRelativeHumidity();
|
||||
if (hum < 0) {
|
||||
Serial.println("Get humidity failed");
|
||||
} else {
|
||||
Serial.printf("Get humidity: %f\r\n", hum);
|
||||
}
|
||||
float temp = ag.sht3x.getTemperature();
|
||||
|
||||
delay(1000);
|
||||
Serial.printf("Get temperature: %f\r\n", temp);
|
||||
Serial.printf(" Get humidity: %f\r\n", hum);
|
||||
} else {
|
||||
Serial.println("Measure sht3x failed");
|
||||
}
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
void failedHandler(String msg) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
};
|
||||
|
Reference in New Issue
Block a user