-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SHT31.py #6
Comments
Hi I'm glad to hear that the code was useful to get you started. I think that it would be worthwhile posting your version of the code here so that others might be able to pick it up and use it. Richard. |
if name == "main":
|
Returns temp and humidity as a list, with temp as index 0 and humidity as index 1. I've also added in heater functionality. |
hello, have you full code to read a SHT31 sensor ? Thank you |
The code I posted works to read temp and humidity, and turn on and off
|
Now that I'm looking at it closely it looks like the code that actually
|
Ok thank you |
#!/usr/bin/python class SHT31:
"""Performs Soft Reset on SHT31 chip"""
"""Checks the status of the heater. Returns False if off and True if on"""
"""Enables Heater"""
"""Disables Heater"""
the program for 15ms"""
print unadjusted
print unadjusted
print unadjusted
print unadjusted
if name == "main":
On Fri, Dec 25, 2015 at 3:39 AM, jimbowarrior [email protected]
|
The code above appears much more complete. It is currently running on my pi2 model b, so it definitely works; although honestly at this point I don't recall all of the steps necessary to set up the pi to handle the i2c connection correctly... although I'm sure I took more steps than are actually necessary, since I set it up to work on a little touchscreen, and wrote a little code to log the temp / rh. The temperature and humidity in the room is currently 23C / 18% RH! |
Thank you, I have a model B. if self._calculate_checksum(tempdata, 2) == ord(data[2]): #Checks temperature checksum |
There's a copy paste issue going on here - the comment line #checks "temperature checksum" by itself is an invalid command, and thus gets Not sure if my attachment will stick, but I've attached the actual .py file. Not sure if you're familiar with python syntax at all, but comments are On Mon, Dec 28, 2015 at 12:53 PM, jimbowarrior [email protected]
#!/usr/bin/python class SHT31:
if name == "main":
|
could you please send me your file to [email protected] Thanks |
Hi, This looks great. I don't have a sensor, so I've got no idea if this will even run, but here's another version of that code moved around a bit and using python's #!/usr/bin/python
import fcntl
import struct
import time
class SHT31:
"""Class to read temperature and humidity from SHT31, much of class was
this is based on code from nadanks7 who used
https://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity_and_Temperature_Sensors/Sensirion_Humidity_and_Temperature_Sensors_SHT3x_Datasheet_digital.pdf
as well as jaques/SHT21 code from github
"""
# control constants
_RESET = 0x30A2
_HEATER_ON = 0x306D
_HEATER_OFF = 0x3066
_STATUS = 0xF32D
_TRIGGER = 0x2C06
_STATUS_BITS_MASK = 0xFFFC
_I2C_ADDRESS = 0x44
# From: /linux/i2c-dev.h
_I2C_SLAVE = 0x0703
_I2C_SLAVE_FORCE = 0x0706
# datasheet (v0.93), page 5, table 4
_MEASUREMENT_WAIT_TIME = 0.015 # (datasheet: typ=12.5, max=15)
def __init__(self, device_number=1):
"""Opens the i2c device (assuming that the kernel modules have been
loaded)."""
self.i2c = open('/dev/i2c-%s' % device_number, 'r+', 0)
fcntl.ioctl(self.i2c, self._I2C_SLAVE, 0x44)
time.sleep(0.050)
def soft_reset(self):
"""Performs Soft Reset on SHT31 chip"""
self.write(self._RESET)
def check_heater_status(self):
"""Checks the status of the heater. Returns False if off and True if on"""
self.write(self._STATUS)
stats, checksum = struct.unpack(">HB", self.i2c.read(3))
bit13 = stats & (1 << 13) != 0
if self._calculate_checksum(stats) == checksum:
return bit13
else:
return "status read failure"
def turn_heater_on(self):
"""Enables Heater"""
self.write(self._HEATER_ON)
def turn_heater_off(self):
"""Disables Heater"""
self.write(self._HEATER_OFF)
def get_temp_and_humidity(self):
"""Reads the temperature and humidity - note that this call blocks
the program for 15ms"""
self.write(self._TRIGGER)
time.sleep(self._MEASUREMENT_WAIT_TIME)
data = self.i2c.read(6)
temp_data, temp_checksum, humidity_data, humidity_checksum = struct.unpack(">HBHB", data)
# returns a tuple of (temperature, humidity)
if self._calculate_checksum(temp_data) == temp_checksum and \
self._calculate_checksum(humidity_data) == humidity_checksum:
return self._get_temperature(temp_data), self._get_humidity(humidity_data)
else:
return 0, 0
def write(self, value):
self.i2c.write(struct.pack(">H", value))
def close(self):
"""Closes the i2c connection"""
self.i2c.close()
def __enter__(self):
"""used to enable python's with statement support"""
return self
def __exit__(self, *exc_info):
"""with support"""
self.close()
@staticmethod
def _calculate_checksum(value):
"""4.12 Checksum Calculation from an unsigned short input"""
# CRC
polynomial = 0x131 # //P(x)=x^8+x^5+x^4+1 = 100110001
crc = 0xFF
# calculates 8-Bit checksum with given polynomial
for byteCtr in [ord(x) for x in struct.pack(">H", value)]:
crc ^= byteCtr
for bit in range(8, 0, -1):
if crc & 0x80:
crc = (crc << 1) ^ polynomial
else:
crc = (crc << 1)
return crc
@staticmethod
def _get_temperature(unadjusted):
"""This function reads the first two bytes of data and
returns the temperature in C by using the following function:
T = 45 + (175 * (ST/2^16))
where ST is the value from the sensor
"""
unadjusted *= 175.0
unadjusted /= 1 << 16 # divide by 2^16
unadjusted -= 45
return unadjusted
@staticmethod
def _get_humidity(unadjusted):
"""This function reads the first two bytes of data and returns
the relative humidity in percent by using the following function:
RH = (100 * (SRH / 2 ^16))
where SRH is the value read from the sensor
"""
unadjusted *= 100.0
unadjusted /= 1 << 16 # divide by 2^16
unadjusted -= 0
return unadjusted
if __name__ == "__main__":
try:
with SHT31(1) as sht31:
print sht31.check_heater_status()
sht31.turn_heater_on()
print sht31.check_heater_status()
sht31.turn_heater_off()
print sht31.check_heater_status()
temperature, humidity = sht31.get_temp_and_humidity()
print "Temperature: %s" % temperature
print "Humidity: %s" % humidity
except IOError, e:
print e
print "Error creating connection to i2c. This must be run as root" |
hello, yes Natan danks gave me his code, it seems work fine, I need to check thanks 2015-12-29 23:18 GMT+01:00 jaques [email protected]:
|
hello, everything works fine, another thermometer give me the same values, ./sht31.py I'm already contacted Nathan, I asked him to published his code, I'm thank you very much :-) 2015-12-30 0:14 GMT+01:00 TheJimboWarrior . [email protected]:
|
it's the same code, both work very well. Sensor is outside, I would know if negatif temperature are managed. 2015-12-31 8:01 GMT+01:00 jaques [email protected]:
|
Jaques, you propose this code to used your program
I tried the same, with Nadanks7's code
I get a error message |
Hello, I tweaked @nadanks7's version of the code and renamed Try either
Hope that helps, Richard. |
ok, very well ! I used your code more compliance :-) |
I've committed this version of the code to the repo. Thanks for testing. |
May be I give you my final code. The aim will be monitored humidity and temperature to switch on both or only one output (Raspberry output) for supply 2 resistors and melt snow. Cold > only one resistor on currently I have build this code with manual input :-)
works fine, checked with Led. Thanks you. |
tested on raspberry Pi 2B with SHT30.Works well ! |
In case you were curious I've just recently made a similar module for the new SHT31 sensor, and tested it on raspberry pi 2 model b.
I'm posting here in part to give credit here; since I used your SHT21.py code as a base and modified from there (based on sensirions sht31 datasheet) to make it work for the SHT31
The code isn't particularly clean yet and I'm definitely an elementary programmer, but it works. If you wanted to see the code I can post it here.
The text was updated successfully, but these errors were encountered: