-
Notifications
You must be signed in to change notification settings - Fork 6
/
temperhum.py
executable file
·281 lines (219 loc) · 7.72 KB
/
temperhum.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python3
#Reads the temperature and humidity from a PCSensor, TEMPerHum, USB sensor
#Copyright (C) 2019 Colin J Mair
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://gnu.org/licenses/gpl.html>
import usb.core
import usb.util
import sys
VERSION = "1.5"
Temperhum_Vendor = 0x413d
Temperhum_Product = 0x2107
Temperhum_Interface = 1
Temperhum_ID = hex(Temperhum_Vendor) + ':' + hex(Temperhum_Product)
Temperhum_ID = Temperhum_ID.replace( '0x', '')
# Function to return a string of hex character representing a byte array
def byte_array_to_hex_string( byte_array ):
array_size = len(byte_array)
if array_size == 0:
s = ""
else:
s = ""
for var in list(range(array_size)):
b = hex(byte_array[var])
b = b.replace( "0x", "")
if len(b) == 1:
b = "0" + b
b = "0x" + b
s = s + b + " "
return (s.strip())
# The temperature is a 16 bit signed integer, this function converts it to signed decimal
def twos_complement(value,bits):
# value = int(hexstr,16)
if value & (1 << (bits-1)):
value -= 1 << bits
return value
# Check the parameters passed
params = [x.lower() for x in sys.argv]
if "--help" in params:
print ("")
print ("Usage: temperhum.py [OPTION]")
print ("Reads the temperature and humidity from a PCSensor, TEMPerHum, USB sensor, USB ID", Temperhum_ID)
print ("")
print ("--help shows this :-)")
print ("--version displays version information and exits")
print ("--f output temperature in Fahrenheit, default is Celsius")
print ("--nosymbols do not show C, F or %")
print ("--raw include the raw data from the sensor in the output, as hex bytes")
print ("--debug turn on debugging output")
print ("--reattach if the usb device is attached to a kernel driver, default is to detach it, and leave it that way")
print (" this option forces a reattach to the kernel driver on exit")
print ("")
exit(0)
if "--version" in params:
print ("")
print ("temperhum.py version", VERSION)
print ("Copyright (C) 2019 Colin J Mair")
print ("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>")
print ("This is free software: you are free to change and redistribute it")
print ("There is NO WARRANTY, to the extent permitted by law")
print ("")
exit(0)
if "--debug" in params:
DEBUG = True
else:
DEBUG = False
if "--f" in params:
CELSIUS = False
else:
CELSIUS = True
if "--nosymbols" in params:
NOSYMBOLS = True
else:
NOSYMBOLS = False
if "--reattach" in params:
REATTACH = True
else:
REATTACH = False
if "--raw" in params:
RAW = True
else:
RAW = False
# If debug is true tell the user
if DEBUG == True:
print ("--debug =", DEBUG, " --f =", not CELSIUS, " --nosymbols =", NOSYMBOLS, " --reattach =", REATTACH)
# Try to find the Temperhum usb device
device = usb.core.find(idVendor = Temperhum_Vendor, idProduct = Temperhum_Product)
# If it was not found report the error and exit
if device is None:
print ("Error: Device", Temperhum_ID, "not found")
exit(0)
else:
if DEBUG == True:
print ("Found Device ID", Temperhum_ID)
print ("-" * 20, "Device Information", "-" * 20)
print (device)
print ("-" * 20, "Device Information", "-" * 20)
# check if it has a kernal driver, if so set a reattach flag and detach it
reattach = False
if device.is_kernel_driver_active(1):
reattach = True
if DEBUG == True:
print ("Warning: kernal driver attached to this device, will try to detach it", end='')
if REATTACH:
print (" and reattach at the end")
else:
print (" and leave it detached")
result = device.detach_kernel_driver(1)
if result != None:
print ("Error: unable to detach kernal driver from device")
exit(0)
else:
if DEBUG == True:
print ("Kernal driver detached ok")
# Extract the correct interface information from the device information
cfg = device[0]
inf = cfg[Temperhum_Interface,0]
if DEBUG == True:
print ("Claiming the device interface", Temperhum_Interface, "for use")
result = usb.util.claim_interface(device, Temperhum_Interface)
if result != None:
print ("Error: unable to claim the interface")
exit(0)
else:
if DEBUG == True:
print ("Claimed interface ok")
# Extract the read and write endpoint information
ep_read = inf[0]
ep_write = inf[1]
if DEBUG == True:
print ("-" * 20, "Read Endpoint Information", "-" * 20)
print (ep_read)
print ("-" * 20, "Read Endpoint Information", "-" * 20)
print ("-" * 20, "Write Endpoint Information", "-" * 20)
print (ep_write)
print ("-" * 20, "Write Endpoint Information", "-" * 20)
# Extract the addresses to read from and write to
ep_read_addr = ep_read.bEndpointAddress
ep_write_addr = ep_write.bEndpointAddress
if DEBUG == True:
print ("Read endpoint address =", hex(ep_read_addr))
print ("Write endpoint address =", hex(ep_write_addr))
print ("Sending request for temperature/humidity data to device")
try:
msg = b'\x01\x80\x33\x01\0\0\0\0'
sendit = device.write(ep_write_addr, msg)
except:
print ("Error: sending request to device")
exit(0)
if DEBUG == True:
print ("Sending request went ok")
print ("Reading data from device")
try:
data = device.read(ep_read_addr, 0x8)
except:
print ("Error: reading data from device")
exit(0)
else:
if DEBUG == True:
print ("Data returned from device =", data)
# Decode the temperature and humidity
if CELSIUS == True:
temperature = round( ( twos_complement( (data[2] * 256) + data[3],16 ) ) / 100, 1 )
else:
temperature = round( ( twos_complement( (data[2] * 256) + data[3],16 ) ) / 100 * 9/5 + 32, 1 )
humidity = int( ( (data[4] * 256) + data[5] ) / 100 )
# Add symbols unless turned off by --nosymbols parameter
if NOSYMBOLS == False:
if CELSIUS == True:
temperature = str(temperature) + "C"
else:
temperature = str(temperature) + "F"
humidity = str(humidity) + "%"
# Output the temperature and humidity
if DEBUG == True:
print ("")
if RAW == True:
dashes = 50
else:
dashes = 12
print ("-" * dashes)
print (temperature, humidity, end="")
if RAW == True:
print ("", byte_array_to_hex_string(data))
else:
print ("")
print ("-" * dashes)
print ("")
else:
print (temperature, humidity, end="")
if RAW == True:
print ("", byte_array_to_hex_string(data))
else:
print ("")
# Release the usb resources
if DEBUG == True:
print ("Releasing USB resources")
result = usb.util.dispose_resources(device)
if result != None:
print ("Error: releasing USB resources")
else:
if DEBUG == True:
print ("Resources released ok")
# Reattach device to the kernel driver if requested by parameter
if REATTACH:
if DEBUG == True:
print ("Reattaching the kernel driver to device")
result = device.attach_kernel_driver(1)
if result != None:
print ("Error: reattaching the kernel driver to device")
exit(0)
exit(0)