-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshtxx.py
172 lines (140 loc) · 5.14 KB
/
shtxx.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
#!/usr/bin/env python
# encoding: utf-8
#based on Microwire.py and hackaday buspirate/sht tutorial
import sys,time
from optparse import OptionParser
from pyBusPirateLite.RAW_WIRE import *
def sht_command(rw, data):
##clear shtxx interface
rw.data_high()
for i in range(9):
rw.clk_tick()
#start condition
rw.data_high()
rw.clk_high()
rw.data_low()
rw.clk_low()
rw.clk_high()
rw.data_high()
rw.clk_low()
#command
rw.bulk_trans(1, [data])
#read and return bit
return rw.read_bit()
def sht_acknowledge(rw):
#acknowledge
rw.data_low()
rw.clk_tick()
def sht_wait_conversion_finished(rw, options):
while 1:
mosi_status = (ord(rw.read_pins()) & BBIOPins.MOSI) >> (BBIOPins.MOSI-1)
if mosi_status:
if options.verbose:
print 'waiting...'
else:
if options.verbose:
print 'conversion done'
time.sleep(0.1)
break
def sht_temperature(rw, options):
#soft reset
status = sht_command(rw, 0b00011110)
if options.verbose:
print 'acknowledgment status:', ord(status)
if not status:
print "Error resetting SHT"
#start temperature conversion
status = sht_command(rw, 0b00000011)
if options.verbose:
print 'acknowledgment status:', ord(status)
if not status:
print "Error starting temperature conversion SHT"
sht_wait_conversion_finished(rw, options)
data = list()
for i in range(3):
data.append(ord(rw.read_byte()))
sht_acknowledge(rw)
temp_hb, temp_lb ,temp_crc = data
#temp_hb = 0x17 #for formula testing
#temp_lb = 0xcc #for formula testing
#print (temp_hb<<8)+temp_lb
temp = -39.7 + 0.01 * ((temp_hb<<8)+temp_lb)
if options.verbose:
print 'temp_hb:', temp_hb
print 'temp_lb:', temp_lb
print 'temp_crc:', temp_crc
print 'temp:', temp
return temp
def sht_humidity(rw, options):
#soft reset
status = sht_command(rw, 0b00011110)
if options.verbose:
print 'acknowledgment status:', ord(status)
if not status:
print "Error resetting SHT"
#start humidity conversion
status = sht_command(rw, 0b00000101)
if options.verbose:
print 'acknowledgment status:', ord(status)
if not status:
print "Error starting humidity conversion SHT"
#time.sleep(1)
sht_wait_conversion_finished(rw, options)
data = list()
for i in range(3):
data.append(ord(rw.read_byte()))
sht_acknowledge(rw)
hum_hb, hum_lb ,hum_crc = data
#hum_hb = 0x05 #for formula testing
#hum_lb = 0x80 #for formula testing
#print (hum_hb<<8)+hum_lb
hum = -2.0468 + 0.0367*((hum_hb<<8)+hum_lb) + (-0.0000015955*(((hum_hb<<8)+hum_lb)**2))
if options.verbose:
print 'hum_hb:', hum_hb
print 'hum_lb:', hum_lb
print 'hum_crc:', hum_crc
print 'hum:', hum
return hum
def main():
# First of all parse the command line
parser = OptionParser()
parser.add_option("-d", "--device", dest="device", help="serial interface where bus pirate is in.[/dev/bus_pirate]", default="/dev/bus_pirate")
parser.add_option("-t", "--temperature", action="store_true", dest="temperature", help="get temperature from sht.", default=False)
parser.add_option("-H", "--humidity", dest="humidity", action="store_true", help="get humidity from sht.", default=False)
parser.add_option("-v", "--verbose", dest="verbose", help="don't be quiet.", action="store_true")
(options,args) = parser.parse_args()
if not (options.temperature or options.humidity):
parser.print_help()
exit()
# Create an instance of the RAW_WIRE class as we are using the BitBang/RAW_WIRE mode
rw = RAW_WIRE( options.device, 115200 )
if not rw.BBmode():
print "Can't enter into BitBang mode."
exit()
# We have succesfully activated the BitBang Mode, so we continue with
# the raw-wire mode.
if not rw.enter_rawwire():
print "Can't enable the raw-wire mode."
exit()
# Now we have raw-wire mode enabled, so first configure peripherals
# (Power, PullUps, AUX, CS)
if not rw.raw_cfg_pins( PinCfg.POWER | PinCfg.PULLUPS):
print "Error enabling the internal voltage regulators."
# Configure the raw-wire mode
if not rw.cfg_raw_wire( (RAW_WIRECfg.BIT_ORDER & RAW_WIRE_BIT_ORDER_TYPE.MSB) | (RAW_WIRECfg.WIRES & RAW_WIRE_WIRES_TYPE.TWO) | (RAW_WIRECfg.OUT_TYPE & RAW_WIRE_OUT_TYPE.HIZ) ):
print "Error configuring the raw-wire mode."
# Set raw-wire speed
if not rw.set_speed( RAW_WIRESpeed._5KHZ ):
print "Error setting raw-wire speed."
if options.temperature:
print "Measuring temperature..."
temperature = sht_temperature(rw, options)
print "Temperature: %f°C" % temperature
if options.humidity:
print "Measuring humidity..."
humidity = sht_humidity(rw, options)
print "Humidity: %f%%" % humidity
# Reset the bus pirate
rw.resetBP();
if __name__ == '__main__':
main()