Temperature&humidity sensor (High Accuracy&Mini) v1.0 fixed Python script

After reading posts like Temperature&humidity sensor (High Accuracy&Mini) v1.0 Bad readings

And much Googling I fixed python script /home/pi/Dexter/GrovePi/Software/Python/grove_i2c_temp_hum_sensor_mini/grove_i2c_temp_hum_mini.py

With this script I get correct temp and hum values! And all working fine.

#!/usr/bin/env python
#
# GrovePi Library for using the Grove - Temperature&Humidity Sensor (http://www.seeedstudio.com/depot/Grove-TemperatureHumidity-Sensor-HighAccuracy-Mini-p-1921.html)
#
# The GrovePi connects the Raspberry Pi and Grove sensors.  You can learn more about GrovePi here:  http://www.dexterindustries.com/GrovePi
#
# Have a question about this library?  Ask on the forums here:  http://forum.dexterindustries.com/c/grovepi
#
# Released under the MIT license (http://choosealicense.com/licenses/mit/).
# For more information see https://github.com/DexterInd/GrovePi/blob/master/LICENSE
#################################################################################################################################################
# NOTE:
# The software for this sensor is still in development and might make your GrovePi unuable as long as this sensor is connected with the GrovePi
#################################################################################################################################################
import time,sys
import RPi.GPIO as GPIO
import smbus

debug = 0

# use the bus that matches your raspi version
rev = GPIO.RPI_REVISION
if rev == 2 or rev == 3:
    bus = smbus.SMBus(1)
else:
    bus = smbus.SMBus(0)

class th02:

    ADDRESS = 0x40

    TH02_REG_STATUS = 0x00
    TH02_REG_DATA_H = 0x01
    TH02_REG_DATA_L = 0x02
    TH02_REG_CONFIG = 0x03
    TH02_REG_ID = 0x11

    TH02_STATUS_RDY_MASK = 0x01

    TH02_CMD_MEASURE_HUMI = [0x01]
    TH02_CMD_MEASURE_TEMP = [0x11]

    SUCCESS = 0

    def getTemperature(self):
        bus.write_i2c_block_data(self.ADDRESS, self.TH02_REG_CONFIG, self.TH02_CMD_MEASURE_TEMP)

        for i in range(0,100):
            status=self.getStatus()
            if debug:
               print("st_temp:",status)
            if i == 99:
                print("can't get response for Temp command for 99 retries:")
                exit()               
            if status:
                break
            time.sleep(.5)
        t_raw_h=bus.read_byte_data(self.ADDRESS, self.TH02_REG_DATA_H)
        t_raw_l=bus.read_byte_data(self.ADDRESS, self.TH02_REG_DATA_L)
        if debug:
            print("raw_temp:",t_raw_h)
            print("raw2_temp:",t_raw_l)
        temperature = t_raw_h<<8
        #print("temp1",temperature)
        temperature |= t_raw_l
        #print("temp2",temperature)
        temperature >>= 2
        #print("temp3",temperature)
        return (temperature/32.0)-50.0

    def getHumidity(self):
        bus.write_i2c_block_data(self.ADDRESS, self.TH02_REG_CONFIG, self.TH02_CMD_MEASURE_HUMI)
        
        for i in range(0,100):
            status=self.getStatus()
            if debug:
                print("st:",status)
            if i == 99:
                print("can't get response for Hum command for 99 retries:")
                exit()
            if status:
                break
            time.sleep(.5)            
        t_raw_h=bus.read_byte_data(self.ADDRESS, self.TH02_REG_DATA_H)
        t_raw_l=bus.read_byte_data(self.ADDRESS, self.TH02_REG_DATA_L)
        if debug:
            print(t_raw)
        humidity = t_raw_h<<8
        #print("humidity1",humidity)
        humidity |= t_raw_l
        #print("humidity2",humidity)
        humidity >>= 4
        #print("humidity3",humidity)
        return (humidity/16.0)-24.0

    def getStatus(self):
        status=bus.read_i2c_block_data(self.ADDRESS, self.TH02_REG_STATUS,1)
        #if debug:
         #   print(status)
        if status[0] & self.TH02_STATUS_RDY_MASK != 1:
            return 1
        else:
            return 0

if __name__ == "__main__":
    t= th02()
    while True:
        print(t.getTemperature(),t.getHumidity())
        time.sleep(2)
2 Likes

Excellent!

Thanks for sharing! This will be helpful, I’m sure.

Jim “JR”

P.S.,
@cyclicalobsessive
What do you think? Any advice or additions?

1 Like

What IS that thing?

I see both a “Dexter” and a “Seeed Studios” logo on it but have no idea what it is .

Are you about GrovePi+ shield?

1 Like

Is that a GrovePi shield? (Hat is the correct term for a “shield” for the Raspberry Pi.)

I’ve only messed with the GoPiGo, not the GrovePi.

Oh, sorry me for my English.
Yes , this is GrovePi Plus board.

1 Like

Suggest a small delay in the wait-for-status loops such as time.sleep(0.05).

Also, you may want to protect against the program locking up without indication of a problem, by adding a counter in the wait-for-status loops, and exiting with an error message if the counter goes over some big number.

1 Like

Don’t worry, you’re doing fine.

My Russian is far worse than your English!
:woozy_face: :wink:

Thanks! Updated script in the first post.

1 Like