-
Notifications
You must be signed in to change notification settings - Fork 0
/
HDC1000.py
223 lines (171 loc) · 7.08 KB
/
HDC1000.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""
Library for HDC1000.py from https://github.com/switchdoclabs/SDL_Pi_HDC1000/blob/master/SDL_Pi_HDC1000.py
"""
#
# SDL_Pi_HDC1000
# Raspberry Pi Driver for the SwitchDoc Labs HDC1000 Breakout Board
#
# SwitchDoc Labs
# January 2017
#
# Version 1.1
# constants
# I2C Address
HDC1000_ADDRESS = (0x40) # 1000000
# Registers
HDC1000_TEMPERATURE_REGISTER = (0x00)
HDC1000_HUMIDITY_REGISTER = (0x01)
HDC1000_CONFIGURATION_REGISTER = (0x02)
HDC1000_MANUFACTURERID_REGISTER = (0xFE)
HDC1000_DEVICEID_REGISTER = (0xFF)
HDC1000_SERIALIDHIGH_REGISTER = (0xFB)
HDC1000_SERIALIDMID_REGISTER = (0xFC)
HDC1000_SERIALIDBOTTOM_REGISTER = (0xFD)
# Configuration Register Bits
HDC1000_CONFIG_RESET_BIT = (0x8000)
HDC1000_CONFIG_HEATER_ENABLE = (0x2000)
HDC1000_CONFIG_ACQUISITION_MODE = (0x1000)
HDC1000_CONFIG_BATTERY_STATUS = (0x0800)
HDC1000_CONFIG_TEMPERATURE_RESOLUTION = (0x0400)
HDC1000_CONFIG_HUMIDITY_RESOLUTION_HBIT = (0x0200)
HDC1000_CONFIG_HUMIDITY_RESOLUTION_LBIT = (0x0100)
HDC1000_CONFIG_TEMPERATURE_RESOLUTION_14BIT = (0x0000)
HDC1000_CONFIG_TEMPERATURE_RESOLUTION_11BIT = (0x0400)
HDC1000_CONFIG_HUMIDITY_RESOLUTION_14BIT = (0x0000)
HDC1000_CONFIG_HUMIDITY_RESOLUTION_11BIT = (0x0100)
HDC1000_CONFIG_HUMIDITY_RESOLUTION_8BIT = (0x0200)
import array, time
class HDC1000:
def __init__(self, i2c, addr=HDC1000_ADDRESS):
self.i2c = i2c
self.addr = addr
time.sleep(0.015) # 15ms startup time
config = HDC1000_CONFIG_ACQUISITION_MODE
s = [HDC1000_CONFIGURATION_REGISTER, config >> 8, 0x00]
s2 = bytearray(s)
self.i2c.writeto(self.addr, s2) # sending config register bytes
time.sleep(0.015) # From the data sheet
# 0x10(48) Temperature, Humidity enabled, Resolultion = 14-bits, Heater off
# config = HDC1000_CONFIG_ACQUISITION_MODE
# self._bus.write_byte_data(HDC1000_ADDRESS,HDC1000_CONFIGURATION_REGISTER, config>>8)
# public functions
def readTemperature(self):
s = [HDC1000_TEMPERATURE_REGISTER] # temp
s2 = bytearray(s)
self.i2c.writeto(self.addr, s2)
time.sleep(0.0625) # From the data sheet
data = self.i2c.readfrom(self.addr, 2) # read 2 byte temperature data
buf = array.array('B', data)
#print("Temp: %f 0x%X %X" % (((((buf[0] << 8) + (buf[1])) / 65536.0) * 165.0) - 40.0, buf[0], buf[1]))
# Convert the data
temp = (buf[0] * 256) + buf[1]
cTemp = (temp / 65536.0) * 165.0 - 40
return cTemp
def readHumidity(self):
# Send humidity measurement command, 0x01(01)
time.sleep(0.015) # From the data sheet
s = [HDC1000_HUMIDITY_REGISTER] # hum
s2 = bytearray(s)
self.i2c.writeto(self.addr, s2)
time.sleep(0.0625) # From the data sheet
data = self.i2c.readfrom(self.addr, 2) # read 2 byte humidity data
buf = array.array('B', data)
# print ( "Humidity: %f 0x%X %X " % ( ((((buf[0]<<8) + (buf[1]))/65536.0)*100.0 ), buf[0], buf[1] ) )
humidity = (buf[0] * 256) + buf[1]
humidity = (humidity / 65536.0) * 100.0
return humidity
def readConfigRegister(self):
# Read config register
s = [HDC1000_CONFIGURATION_REGISTER] # temp
s2 = bytearray(s)
self.i2c.writeto(self.addr, s2)
time.sleep(0.0625) # From the data sheet
data = self.i2c.readfrom(self.addr, 2) # read 2 byte config data
buf = array.array('B', data)
# print "register=%X %X"% (buf[0], buf[1])
return buf[0] * 256 + buf[1]
def turnHeaterOn(self):
# Read config register
config = self.readConfigRegister()
config = config | HDC1000_CONFIG_HEATER_ENABLE
s = [HDC1000_CONFIGURATION_REGISTER, config >> 8, 0x00]
s2 = bytearray(s)
self.i2c.writeto(self.addr, s2) # sending config register bytes
time.sleep(0.015) # From the data sheet
return
def turnHeaterOff(self):
# Read config register
config = self.readConfigRegister()
config = config & ~HDC1000_CONFIG_HEATER_ENABLE
s = [HDC1000_CONFIGURATION_REGISTER, config >> 8, 0x00]
s2 = bytearray(s)
self.i2c.writeto(self.addr, s2) # sending config register bytes
time.sleep(0.015) # From the data sheet
return
def setHumidityResolution(self, resolution):
# Read config register
config = self.readConfigRegister()
config = (config & ~0x0300) | resolution
s = [HDC1000_CONFIGURATION_REGISTER, config >> 8, 0x00]
s2 = bytearray(s)
self.i2c.writeto(self.addr, s2) # sending config register bytes
time.sleep(0.015) # From the data sheet
return
def setTemperatureResolution(self, resolution):
# Read config register
config = self.readConfigRegister()
config = (config & ~0x0400) | resolution
s = [HDC1000_CONFIGURATION_REGISTER, config >> 8, 0x00]
s2 = bytearray(s)
self.i2c.writeto(self.addr, s2) # sending config register bytes
time.sleep(0.015) # From the data sheet
return
def readBatteryStatus(self):
# Read config register
config = self.readConfigRegister()
config = config & HDC1000_CONFIG_BATTERY_STATUS
if (config == 0):
return True
else:
return False
return 0
def readManufacturerID(self):
s = [HDC1000_MANUFACTURERID_REGISTER] # temp
s2 = bytearray(s)
self.i2c.writeto(self.addr, s2)
time.sleep(0.0625) # From the data sheet
data = self.i2c.readfrom(self.addr, 2) # read 2 byte config data
buf = array.array('B', data)
return buf[0] * 256 + buf[1]
def readDeviceID(self):
s = [HDC1000_DEVICEID_REGISTER] # temp
s2 = bytearray(s)
self.i2c.writeto(self.addr, s2)
time.sleep(0.0625) # From the data sheet
data = self.i2c.readfrom(self.addr, 2) # read 2 byte config data
buf = array.array('B', data)
return buf[0] * 256 + buf[1]
def readSerialNumber(self):
serialNumber = 0
s = [HDC1000_SERIALIDHIGH_REGISTER] # temp
s2 = bytearray(s)
self.i2c.writeto(self.addr, s2)
time.sleep(0.0625) # From the data sheet
data = self.i2c.readfrom(self.addr, 2) # read 2 byte config data
buf = array.array('B', data)
serialNumber = buf[0] * 256 + buf[1]
s = [HDC1000_SERIALIDMID_REGISTER] # temp
s2 = bytearray(s)
self.i2c.writeto(self.addr, s2)
time.sleep(0.0625) # From the data sheet
data = self.i2c.readfrom(self.addr, 2) # read 2 byte config data
buf = array.array('B', data)
serialNumber = serialNumber * 256 + buf[0] * 256 + buf[1]
s = [HDC1000_SERIALIDBOTTOM_REGISTER] # temp
s2 = bytearray(s)
self.i2c.writeto(self.addr, s2)
time.sleep(0.0625) # From the data sheet
data = self.i2c.readfrom(self.addr, 2) # read 2 byte config data
buf = array.array('B', data)
serialNumber = serialNumber * 256 + buf[0] * 256 + buf[1]
return serialNumber