-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreadsensor.py
More file actions
executable file
·35 lines (28 loc) · 992 Bytes
/
readsensor.py
File metadata and controls
executable file
·35 lines (28 loc) · 992 Bytes
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
#!/usr/bin/env python3
import time
import board
import busio
from adafruit_htu21d import HTU21D
# Create library object using our Bus I2C port
i2c = busio.I2C(board.SCL, board.SDA)
sensor_i2c_address = None
scanning_frequency = 5
reading_frequency = 1
while True:
print("\nScanning for I2C device address to appear\n")
sensor_i2c_address = next(iter(i2c.scan()), None)
if sensor_i2c_address:
print(f"\nFound sensor address at {sensor_i2c_address}\n")
break
else:
print("\nFailed to detect any connected I2C sensors. "
f"Sleeping for {scanning_frequency} seconds before rescan...\n")
time.sleep(scanning_frequency)
sensor = HTU21D(i2c, sensor_i2c_address)
while True:
try:
print("\nTemperature: %0.1f C" % sensor.temperature)
print("Relative Humidity: %0.1f %%" % sensor.relative_humidity)
except ValueError as e:
print(f"Error while reading sensor: {e}\n")
time.sleep(reading_frequency)