13
13
# t_raw, t_val, h_raw, h_val, isvalid = sensor.get_measure_results()
14
14
# if isvalid is not None:
15
15
# break
16
- # print(f' {t_raw}, {t_val} °C, {h_raw}, {h_val} %RH, isvalid' )
16
+ # print(f" {t_raw}, {t_val} °C, {h_raw}, {h_val} %RH, { isvalid}" )
17
17
# @endcode
18
18
#
19
19
22
22
23
23
24
24
class SHT :
25
-
26
- SHT3x = (0x30 )
27
- SHT4x = (0x40 )
25
+ SHT3x = 0x30
26
+ SHT4x = 0x40
28
27
29
28
# Init SHT
30
29
# @param i2c I2C interface
@@ -69,22 +68,22 @@ def detect_sht_type(self):
69
68
sht = 0xFF
70
69
# try reading status of SHT3x
71
70
try :
72
- self .i2c .writeto (self .i2c_addr , b' \xF3 \x2D ' , False )
71
+ self .i2c .writeto (self .i2c_addr , b" \xf3 \x2d " , False )
73
72
time .sleep (0.01 )
74
73
response = self .i2c .readfrom (self .i2c_addr , 3 )
75
- if self ._check_crc (response [0 :2 ],response [2 ]):
74
+ if self ._check_crc (response [0 :2 ], response [2 ]):
76
75
sht = self .SHT3x
77
- except OSError as error :
76
+ except OSError :
78
77
pass
79
78
if sht == 0xFF :
80
79
# try reading serial number of SHT4x
81
80
try :
82
- self .i2c .writeto (self .i2c_addr , b' \x89 ' , False )
81
+ self .i2c .writeto (self .i2c_addr , b" \x89 " , False )
83
82
time .sleep (0.01 )
84
83
response = self .i2c .readfrom (self .i2c_addr , 6 )
85
- if self ._check_crc (response [3 :5 ],response [5 ]):
84
+ if self ._check_crc (response [3 :5 ], response [5 ]):
86
85
sht = self .SHT4x
87
- except OSError as error :
86
+ except OSError :
88
87
pass
89
88
return sht
90
89
@@ -93,30 +92,30 @@ def detect_sht_type(self):
93
92
# @return None
94
93
def start_measure (self , precision ):
95
94
if self .sht == self .SHT3x :
96
- p_byte = [b' \x16 ' , b' \x0B ' , b' \x00 ' ]
97
- self .i2c .writeto (self .i2c_addr , b' \x24 ' + p_byte [precision ], False )
95
+ p_byte = [b" \x16 " , b" \x0b " , b" \x00 " ]
96
+ self .i2c .writeto (self .i2c_addr , b" \x24 " + p_byte [precision ], False )
98
97
if self .sht == self .SHT4x :
99
- cmd = [b' \xE0 ' , b' \xF6 ' , b' \xFD ' ]
98
+ cmd = [b" \xe0 " , b" \xf6 " , b" \xfd " ]
100
99
self .i2c .writeto (self .i2c_addr , cmd [precision ], False )
101
100
102
101
# Get the measurement values
103
102
# @details
104
103
# As long as no values available all return parameter are None.
105
104
# If values not equal None are returned the measurement has been completed
106
105
# and needs to be restarted again for a new measurement.
107
- # @return temperature[raw], temperature[°C], humidity[raw], humidity[%RH]
106
+ # @return temperature[raw], temperature[°C], humidity[raw], humidity[%RH], valid
108
107
def get_measure_results (self ):
109
108
try :
110
109
response = self .i2c .readfrom (self .i2c_addr , 6 )
111
110
t_bytes = response [0 :2 ]
112
111
t_raw = int .from_bytes (t_bytes , "big" )
113
112
t_val = (175 * t_raw ) / 0xFFFF - 45
114
- isvalid = self ._check_crc (t_bytes ,response [2 ])
113
+ isvalid = self ._check_crc (t_bytes , response [2 ])
115
114
h_bytes = response [3 :5 ]
116
115
h_raw = int .from_bytes (h_bytes , "big" )
117
116
h_val = (100 * h_raw ) / 0xFFFF
118
- isvalid &= self ._check_crc (h_bytes ,response [5 ])
119
- return t_raw , round (t_val ,2 ), h_raw , round (h_val ,2 ), bool (isvalid )
120
- except OSError as error :
117
+ isvalid &= self ._check_crc (h_bytes , response [5 ])
118
+ return t_raw , round (t_val , 2 ), h_raw , round (h_val , 2 ), bool (isvalid )
119
+ except OSError :
121
120
# OSError: [Errno 5] EIO as long as measurement has not completed
122
121
return None , None , None , None , None
0 commit comments