Skip to content

Commit 5ac5dfb

Browse files
committed
temp and humidity working
1 parent 9bcffd9 commit 5ac5dfb

File tree

2 files changed

+46
-49
lines changed

2 files changed

+46
-49
lines changed

adafruit_shtc3.py

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
_SHTC3_SLEEP = 0xB098 # Enter sleep mode
6464
_SHTC3_WAKEUP = 0x3517 # Wakeup mode
6565
_SHTC3_CHIP_ID = 0x807
66+
def pb(buffer):
67+
print("buf:", [hex(i) for i in buffer])
6668

6769
class SHTC3:
6870
def __init__(self, i2c_bus, address = _SHTC3_DEFAULT_ADDR):
@@ -80,20 +82,25 @@ def _write_command(self, command):
8082
self._buffer[1] = command & 0xFF
8183

8284
with self.i2c_device as i2c:
83-
i2c.write(self._buffer, start=0, end=0)
85+
i2c.write(self._buffer, start=0, end=2)
8486

8587
@property
8688
def _chip_id(self): # readCommand(SHTC3_READID, data, 3);
8789
self._buffer[0] = _SHTC3_READID >> 8
8890
self._buffer[1] = _SHTC3_READID & 0xFF
8991

9092
with self.i2c_device as i2c:
91-
i2c.write_then_readinto(self._buffer, self._buffer, out_start=0, out_end=2, in_start=0)
93+
i2c.write_then_readinto(self._buffer, self._buffer, out_start=0, out_end=2, in_start=0, in_end=2)
9294

9395
return unpack_from(">H", self._buffer)[0]
9496

9597
def reset(self):
96-
self._write_command(_SHTC3_SOFTRESET)
98+
try:
99+
self._write_command(_SHTC3_SOFTRESET)
100+
101+
except RuntimeError as run_err:
102+
if run_err.args and run_err.args[0] != "I2C slave address was NACK'd":
103+
raise run_err
97104
delay_seconds(0.001)
98105

99106
@property
@@ -108,69 +115,56 @@ def sleep(self, sleep_enabled):
108115
else:
109116
self._write_command(_SHTC3_WAKEUP)
110117
delay_seconds(0.001)
118+
self._cached_sleep = sleep_enabled
111119

112120
# lowPowerMode(bool readmode) { _lpMode = readmode
113121

114122
@property
115-
def humidity(self):
123+
def relative_humidity(self):
116124
"""Current relative humidity in % rH"""
117-
# sensors_event_t *humidity,
118-
# sensors_event_t *temp) {
119-
t = monotonic()
120-
print("reading hum/temp")
121-
readbuffer = bytearray(6)
125+
self.measurements[1]
126+
return self._humidity
127+
128+
@property
129+
def temperature(self):
130+
"""Current temperature in degrees celcius"""
131+
self.measurements[0]
132+
133+
@property
134+
def measurements(self):
135+
"""both `temperature` and `relative_humidity`, read simultaneously"""
122136

123137
self.sleep = False
124-
read_bytes = []
138+
raw_readings = []
125139

126-
# self._write_command(_SHTC3_LOWPOW_MEAS_TFIRST)
127-
if False: #lowPower
140+
if False: # check for lowPower
128141
self._write_command(_SHTC3_LOWPOW_MEAS_TFIRST)
129142
delay_seconds(0.001)
130143
else:
131144
self._write_command(_SHTC3_NORMAL_MEAS_TFIRST)
132145
delay_seconds(0.013)
133146

134-
# while (!i2c_dev->read(readbuffer, sizeof(readbuffer))) {
135-
# delay(1)
136-
while True:
137-
with self.i2c_device as i2c:
138-
print("reading")
139-
i2c.readinto(self._buffer)
140-
print("buf:", [hex(i) for i in self._buffer])
141-
read_bytes = unpack_from(">hbh", self._buffer)
142-
print("unpacked:", read_bytes)
143-
if read_bytes[0] != 0 and read_bytes[1] != 0 and read_bytes[2] != 0:
144-
print ("not all zeros, breaking")
145-
break
146-
print ("all zeros, continuing")
147+
self._buffer = bytearray(6)
148+
with self.i2c_device as i2c:
149+
i2c.readinto(self._buffer)
150+
raw_readings = unpack_from(">hbh", self._buffer)
151+
147152

153+
# check CRC of bytes
148154
# if (self._buffer[2] != crc8(self._buffer, 2) or
149155
# self._buffer[5] != crc8(self._buffer + 3, 2)):
150156
# print("NOT CHECKING")
151157

152-
stemp = read_bytes[0]
153-
stemp = ((4375 * stemp) >> 14) - 4500
154-
temperature = stemp / 100.0
155-
print("temp:", temperature, "stemp:", stemp)
158+
raw_temp = raw_readings[0]
159+
raw_temp = ((4375 * raw_temp) >> 14) - 4500
160+
temperature = raw_temp / 100.0
156161

157-
shum = read_bytes[2]
158-
shum = (625 * shum) >> 12
159-
humidity = shum / 100.0
162+
raw_humidity = raw_readings[2]
163+
raw_humidity = (625 * raw_humidity) >> 12
164+
humidity = raw_humidity / 100.0
160165

161166
self.sleep = True
162-
163-
print("shum:", shum, "humidity:", humidity)
164167
return (temperature, humidity)
165-
# delay_seconds(true)
166-
167-
# // use helpers to fill in the events
168-
# if (temp)
169-
# fillTempEvent(temp, t)
170-
# if (humidity)
171-
# fillHumidityEvent(humidity, t)
172-
# return true
173-
#
174168

175169

176170

@@ -203,7 +197,7 @@ def humidity(self):
203197

204198
# for (int i = 8 i --i) {
205199
# crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1)
206-
#
207-
#
200+
#
201+
#
208202
# return crc
209203
#

examples/shtc3_simpletest.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import busio
44
import board
55
import adafruit_shtc3
6-
from adafruit_debug_i2c import DebugI2C
76
i2c = busio.I2C(board.SCL, board.SDA)
8-
i2c = DebugI2C(i2c)
97
sht = adafruit_shtc3.SHTC3(i2c)
10-
print("got out")
11-
print("hum:", sht.humidity)
8+
9+
while True:
10+
temperature, relative_humidity = sht.measurements
11+
print("Temperature: %0.1f C" % temperature)
12+
print("Humidity: %0.1f %%" % relative_humidity)
13+
print("")
14+
time.sleep(1)

0 commit comments

Comments
 (0)