library for SHT25 humidity and temperature sensor https://www.sensirion.com/products/humidity-sensor/

Dependents:   lib_SHT25_example

Files at this revision

API Documentation at this revision

Comitter:
YSI
Date:
Wed Oct 06 13:41:00 2021 +0000
Parent:
8:bb3dbc86a180
Child:
10:18e6cd816998
Commit message:
Add wait function compatible with mbed2.0, mbed5.0 and mbed6.0 and more.; Default wait for request is typical value and up to maximum value if necessary/

Changed in this revision

lib_SHT25.cpp Show annotated file Show diff for this revision Revisions of this file
lib_SHT25.h Show annotated file Show diff for this revision Revisions of this file
--- a/lib_SHT25.cpp	Wed Sep 22 13:26:04 2021 +0000
+++ b/lib_SHT25.cpp	Wed Oct 06 13:41:00 2021 +0000
@@ -9,22 +9,19 @@
 * Example:
 * @code
 * #include "lib_SHT25.h"
-*  
-* Serial pc(USBTX, USBRX);
+* 
 * SHT25  sensor(I2C_SDA, I2C_SCL);
 * 
 * int main()
 * {
-*     float temperature, humidity;
 *     while(1)
 *     {
-*         temperature = sensor.getTemperature();
-*         humidity = sensor.getTemperature();
-*         pc.printf("\r\ntemperature = %6.2f%cC -|- humidity = %6.2f%%RH", temperature, 248, humidity);
+*         sensor.waitSafeHeat();
+*         float temperature = sensor.getTemperature(), humidity = sensor.getHumidity();
+*         printf("\r\ntemperature = %6.2f%cC -|- humidity = %6.2f%%RH", temperature, 248, humidity);
 *         sensor.waitSafeHeat();
 *         sensor.getData(&temperature, &humidity);
-*         pc.printf("\ntemperature = %6.2f%cC -|- humidity = %6.2f%%RH", temperature, 248, humidity);
-*         sensor.waitSafeHeat();
+*         printf("\r\ntemperature = %6.2f%cC -|- humidity = %6.2f%%RH", temperature, 248, humidity);
 *     }
 * }
 * @endcode
@@ -36,7 +33,7 @@
 
 SHT25::SHT25(PinName sda, PinName scl, enum_sht_prec precision, int frequency) : _i2c(sda, scl)
 {
-    _i2c.frequency((frequency<=SHT_I2C_FREQUENCY)?frequency:SHT_I2C_FREQUENCY);
+    _i2c.frequency((frequency<=400e3)?frequency:400e3);
     setPrecision(precision);
     _temperature = _humidity = NAN;
     _selfHeatTemperature = _selfHeatHumidity = false;
@@ -46,70 +43,72 @@
 
 void SHT25::getData(float *tempC, float *relHumidity)
 {
+    if(_selfHeatTemperature && _selfHeatHumidity) readData();
     *tempC = _temperature;
     *relHumidity = _humidity;
-    if(_selfHeatTemperature && _selfHeatHumidity)
-        readData(tempC, relHumidity);
 }
 
-void SHT25::readData(float *tempC, float *relHumidity)
+void SHT25::readData(void)
 {
-    *tempC = _temperature = readTemperature();
-    *relHumidity = _humidity = readHumidity();
+    _temperature = readTemperature();
+    _humidity = readHumidity();
 }
 
 float SHT25::getTemperature(void)
 {
-    if(_selfHeatTemperature)
-        _temperature = readTemperature();
+    if(_selfHeatTemperature) _temperature = readTemperature();
     return _temperature;
 }
 
-float SHT25::readTemperature(void) // if I2C Freezing go down PullUp resistor to 1K or slow frequency
+float SHT25::readTemperature(void) // if I2C Freezing go down PullUp resistor to 2K or slow frequency
 {
-    char cmd[1] = {SHT_TRIG_TEMP_NHOLD}, rx[3] = {0xFF, 0xFF, 0xFF};
-    if(!_i2c.write(SHT_I2C_ADDR_WRITE, cmd, 1, false))
+    char cmd[] = {SHT_TRIG_TEMP_NHOLD}, rx[] = {0xFF, 0xFF, 0xFF};
+    _selfHeatTemperature = false;
+    _t.attach(callback(this, &SHT25::keepSafeTemperature), SHT_SELF_HEATING);
+    if(!_i2c.write(SHT_I2C_ADDR, cmd, 1))
     {
-        wait_us(SHT_TEMP_MEASURE);
-        _i2c.read(SHT_I2C_ADDR_READ, rx, 3, false);
-        _selfHeatTemperature = false;
-        _t.attach(callback(this, &SHT25::keepSafeTemperature), SHT_SELF_HEATING);
-        return -46.85f + 175.72f * ((((rx[0] << 8) | rx[1]) & 0xFFFC) / 65536.0f);
+        SHT_WAIT(66);
+        int ack = _i2c.read(SHT_I2C_ADDR, rx, 3);
+        if(ack)
+        {
+            SHT_WAIT(19);
+            ack = _i2c.read(SHT_I2C_ADDR, rx, 3);    
+        }
+        return ack?NAN:-46.85f + 175.72f * ((((rx[0] << 8) | rx[1]) & 0xFFFC) / 65536.0f);
     }
     return NAN;
 }
 
 float SHT25::getHumidity(void)
 {
-    if(_selfHeatHumidity)
-        _humidity = readHumidity();
+    if(_selfHeatHumidity) _humidity = readHumidity();
     return _humidity;
 }
 
-float SHT25::readHumidity(void) // if I2C Freezing go down PullUp resistor to 1K or slow frequency
+float SHT25::readHumidity(void) // if I2C Freezing go down PullUp resistor to 2K or slow frequency
 {
-    char cmd[1] = {SHT_TRIG_RH_NHOLD}, rx[3] = {0xFF, 0xFF, 0xFF};
-    if(!_i2c.write(SHT_I2C_ADDR_WRITE, cmd, 1, false))
+    char cmd[] = {SHT_TRIG_RH_NHOLD}, rx[] = {0xFF, 0xFF, 0xFF};
+    _selfHeatHumidity = false;
+    _h.attach(callback(this, &SHT25::keepSafeHumidity), SHT_SELF_HEATING);
+    if(!_i2c.write(SHT_I2C_ADDR, cmd, 1))
     {
-        wait_us(SHT_HUM_MEASURE);
-        _i2c.read(SHT_I2C_ADDR_READ, rx, 3, false);
-        _selfHeatHumidity = false;
-        _h.attach(callback(this, &SHT25::keepSafeHumidity), SHT_SELF_HEATING);
-        return -6.0f + 125.0f * ((((rx[0] << 8) | rx[1]) & 0xFFFC) / 65536.0f);
+        SHT_WAIT(29);
+        int ack = _i2c.read(SHT_I2C_ADDR, rx, 3);
+        return ack?NAN:-6.0f + 125.0f * ((((rx[0] << 8) | rx[1]) & 0xFFFC) / 65536.0f);
     }
     return NAN;
 }
 
 bool SHT25::setPrecision(const enum_sht_prec precision)
 {
-    char cmd[2] = {SHT_WRITE_REG_USER, precision};
-    return !_i2c.write(SHT_I2C_ADDR_WRITE, cmd, 2, false);
+    char cmd[] = {SHT_WRITE_REG_USER, precision};
+    return !_i2c.write(SHT_I2C_ADDR, cmd, 2, false);
 }
 
 bool SHT25::softReset()
 {
-    char cmd[1] = {SHT_SOFT_RESET};
-    return !_i2c.write(SHT_I2C_ADDR_WRITE, cmd, 1, false);
+    char cmd[] = {SHT_SOFT_RESET};
+    return !_i2c.write(SHT_I2C_ADDR, cmd, 1, false);
 }
 
 void SHT25::waitSafeHeat(void)
--- a/lib_SHT25.h	Wed Sep 22 13:26:04 2021 +0000
+++ b/lib_SHT25.h	Wed Oct 06 13:41:00 2021 +0000
@@ -9,22 +9,19 @@
 * Example:
 * @code
 * #include "lib_SHT25.h"
-*  
-* Serial pc(USBTX, USBRX);
+* 
 * SHT25  sensor(I2C_SDA, I2C_SCL);
 * 
 * int main()
 * {
-*     float temperature, humidity;
 *     while(1)
 *     {
-*         temperature = sensor.getTemperature();
-*         humidity = sensor.getTemperature();
-*         pc.printf("\r\ntemperature = %6.2f%cC -|- humidity = %6.2f%%RH", temperature, 248, humidity);
+*         sensor.waitSafeHeat();
+*         float temperature = sensor.getTemperature(), humidity = sensor.getHumidity();
+*         printf("\r\ntemperature = %6.2f%cC -|- humidity = %6.2f%%RH", temperature, 248, humidity);
 *         sensor.waitSafeHeat();
 *         sensor.getData(&temperature, &humidity);
-*         pc.printf("\ntemperature = %6.2f%cC -|- humidity = %6.2f%%RH", temperature, 248, humidity);
-*         sensor.waitSafeHeat();
+*         printf("\r\ntemperature = %6.2f%cC -|- humidity = %6.2f%%RH", temperature, 248, humidity);
 *     }
 * }
 * @endcode
@@ -37,9 +34,8 @@
 
 #include "mbed.h"
 
-#define SHT_I2C_FREQUENCY   400e3   //Sensor I2C Frequency max 400KHz
-#define SHT_I2C_ADDR_WRITE  0x80    //Sensor I2C address write
-#define SHT_I2C_ADDR_READ   0x81    //Sensor I2C address read
+#define SHT_I2C_FREQUENCY   100e3   //Sensor I2C Frequency max 400KHz
+#define SHT_I2C_ADDR        0x80    //Sensor I2C address
 #define SHT_TRIG_TEMP_HOLD  0xE3    //Trigger Temp  with hold master
 #define SHT_TRIG_RH_HOLD    0xE5    //Trigger RH    with hold master
 #define SHT_TRIG_TEMP_NHOLD 0xF3    //Trigger Temp  with no hold master
@@ -47,12 +43,12 @@
 #define SHT_WRITE_REG_USER  0xE6    //Write to user register
 #define SHT_READ_REG_USER   0xE7    //Read from user register
 #define SHT_SOFT_RESET      0xFE    //Soft reset the sensor
-#define SHT_TEMP_MEASURE    85e3    //Waiting to measure T on 14bit
-#define SHT_HUM_MEASURE     29e3    //Waiting to measure H on 12bit
 #if MBED_MAJOR_VERSION > 5
-#define SHT_SELF_HEATING    1s      //Keep self heating
+#define SHT_SELF_HEATING    2s      //Keep self heating
+#define SHT_WAIT(ms)        (thread_sleep_for(ms))
 #else
 #define SHT_SELF_HEATING    0x01    //Keep self heating
+#define SHT_WAIT(ms)        (wait_us(1000*ms))
 #endif
 
 
@@ -71,7 +67,7 @@
         * @param sda I2C pin
         * @param scl I2C pin
         * @param precision SHT25 precision for humidity(default 12 bits) and temperature(default 14 bits)
-        * @param frequency I2C frequency, default and maximum 400KHz
+        * @param frequency I2C frequency, default 100KHz and maximum 400KHz
         */
         SHT25(PinName sda, PinName scl, enum_sht_prec precision = SHT_PREC_RH12T14, int frequency = SHT_I2C_FREQUENCY);
         
@@ -121,14 +117,13 @@
         I2C     _i2c;
         Timeout _t, _h;
     private:
-        void  readData(float *, float *);
+        void  readData(void);
         float readTemperature(void);
         float readHumidity(void);
         void  keepSafeTemperature(void);
         void  keepSafeHumidity(void);
         float _temperature, _humidity;
         bool  _selfHeatTemperature, _selfHeatHumidity;
-        char _rxT[3];
 };
 
 #endif
\ No newline at end of file