forked from jaques/sht21_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsht21.py
More file actions
executable file
·131 lines (106 loc) · 4.39 KB
/
sht21.py
File metadata and controls
executable file
·131 lines (106 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python
import fcntl
import time
import unittest
class SHT21:
"""Class to read temperature and humidity from SHT21, much of class was
derived from: #http://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity/Sensirion_Humidity_SHT21_Datasheet_V3.pdf
and Martin Steppuhn's code from http://www.emsystech.de/raspi-sht21"""
#control constants
_SOFTRESET = 0xFE
_I2C_ADDRESS = 0x40
_TRIGGER_TEMPERATURE_NO_HOLD = 0xF3
_TRIGGER_HUMIDITY_NO_HOLD = 0xF5
#From: /linux/i2c-dev.h
I2C_SLAVE = 0x0703
I2C_SLAVE_FORCE = 0x0706
def __init__(self, device_number=0):
"""Opens the i2c device (assuming that the kernel modules have been
loaded). Note that this has only been tested on first revision
raspberry pi where the device_number = 0, but it should work
where device_number=1"""
self.i2c = open('/dev/i2c-%s'%(device_number),'r+',0)
fcntl.ioctl(self.i2c, self.I2C_SLAVE,0x40)
self.i2c.write(chr(self._SOFTRESET))
time.sleep(0.050)
def read_temperature(self):
"""Reads the temperature from the sensor. Not that this call blocks
for 250ms to allow the sensor to return the data"""
self.i2c.write(chr(self._TRIGGER_TEMPERATURE_NO_HOLD))
time.sleep(0.250)
data = self.i2c.read(3)
if _calculate_checksum(data,2) == ord(data[2]):
return _get_temperature_from_buffer(data)
def read_humidity(self):
"""Reads the humidity from the sensor. Not that this call blocks
for 250ms to allow the sensor to return the data"""
self.i2c.write(chr(self._TRIGGER_HUMIDITY_NO_HOLD))
time.sleep(0.250)
data = self.i2c.read(3)
if _calculate_checksum(data,2) == ord(data[2]):
return _get_humidity_from_buffer(data)
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, type, value, traceback):
"""with support"""
self.close()
def _calculate_checksum(data, nbrOfBytes):
"""5.7 CRC Checksum using teh polynomial given in the datasheet"""
# CRC
POLYNOMIAL = 0x131 # //P(x)=x^8+x^5+x^4+1 = 100110001
crc = 0
#calculates 8-Bit checksum with given polynomial
for byteCtr in range(nbrOfBytes):
crc ^= (ord(data[byteCtr]))
for bit in range(8,0,-1):
if (crc & 0x80):
crc = (crc << 1) ^ POLYNOMIAL
else:
crc = (crc << 1)
return crc
def _get_temperature_from_buffer(data):
"""This function reads the first two bytes of data and
returns the temperature in C by using the following function:
T = =46.82 + (172.72 * (ST/2^16))
where ST is the value from the sensor
"""
unadjusted = (ord(data[0]) << 8) + ord(data[1])
unadjusted *= 175.72
unadjusted /= 1 << 16 # divide by 2^16
unadjusted -= 46.85
return unadjusted
def _get_humidity_from_buffer(data):
"""This function reads the first two bytes of data and returns
the relative humidity in percent by using the following function:
RH = -6 + (125 * (SRH / 2 ^16))
where SRH is the value read from the sensor
"""
unadjusted = (ord(data[0]) << 8) + ord(data[1])
unadjusted *= 125
unadjusted /= 1 << 16 # divide by 2^16
unadjusted -= 6
return unadjusted
class SHT21Test(unittest.TestCase):
"""simple sanity test. Run from the command line with
python -m unittest sht21 to check they are still good"""
def test_temperature(self):
"""Unit test to check the checksum method"""
calc_temp = _get_temperature_from_buffer([chr(99),chr(172)])
#floating point comparison, not pretty.
self.failUnless(abs(calc_temp - 21.5653979492) < 0.1)
def test_checksum(self):
"""Unit test to check the checksum method. Uses values read"""
self.failUnless(_calculate_checksum([chr(99),chr(172)] , 2) == 249)
self.failUnless(_calculate_checksum([chr(99),chr(160)] , 2) == 132)
if __name__ == "__main__":
try:
with SHT21(0) as sht21:
print "Temperature: %s"%sht21.read_temperature()
print "Humidity: %s"%sht21.read_humidity()
except IOError, e:
print e
print 'Error creating connection to i2c. This must be run as root'