forked from jbeale1/pico-misc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAHT10-Triple.py
More file actions
100 lines (82 loc) · 3.08 KB
/
Copy pathAHT10-Triple.py
File metadata and controls
100 lines (82 loc) · 3.08 KB
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
# Read two AHT10 Temp/RH sensors
# Uses 1.3" SH1106 OLED display
# Pi Pico, uPython v1.19.1
# 14-Dec-2022 J.Beale
from machine import Pin, ADC, PWM, I2C, SoftI2C
from time import sleep, time, ticks_ms
import utime
import sh1106 # OLED driver: github.com/robert-hh/SH1106
import ahtx0 # AHT10 driver: github.com/targetblank/micropython_ahtx0
swVersion = "RH Readout 0.3"
# I2C connection to OLED display (addr = 0x3c)
i2c1 = I2C(1, sda=Pin(14,Pin.PULL_UP), scl=Pin(15,Pin.PULL_UP), freq=400_000)
# I2C connection to AHT10 sensor1 (addr = 0x38)
i2c0 = I2C(0, sda=Pin(16,Pin.PULL_UP), scl=Pin(17,Pin.PULL_UP), freq=400_000)
# I2C connection to AHT10 sensor2 (addr = 0x3c)
i2c2 = SoftI2C(scl=Pin(19,Pin.PULL_UP), sda=Pin(18,Pin.PULL_UP), freq=400_000)
# I2C connection to AHT10 sensor3 (addr = 0x3c)
i2c3 = SoftI2C(scl=Pin(13,Pin.PULL_UP), sda=Pin(12,Pin.PULL_UP), freq=400_000)
#devices = i2c3.scan()
#if devices:
# for d in devices:
# print(hex(d))
sensor1 = ahtx0.AHT10(i2c0) # AHT10 sensor #1
sensor2 = ahtx0.AHT10(i2c2) # AHT10 sensor #2
sensor3 = ahtx0.AHT10(i2c3) # AHT10 sensor #3
width = 128 # OLED size
height=64
# Set up OLED with sh1106 library
display = sh1106.SH1106_I2C(width, height, i2c1, None, addr=0x3c, rotate=180)
display.sleep(False)
display.fill(0)
display.text(swVersion,1,1, color=1)
display.show()
avgCount = 4 # how many readings to average together
#readInterval = 0.156 # 0.163 seconds between each reading (1 ch)
#readInterval = 0.07 # 0.163 seconds between each reading (2 ch)
tStart = ticks_ms()
print("sec, T1, T2, T3, RH1, RH2, RH3") # CSV column headers
while True:
try:
Tsum1 = 0
Hsum1 = 0
Tsum2 = 0
Hsum2 = 0
Tsum3 = 0
Hsum3 = 0
for i in range(avgCount):
Tsum1 += sensor1.temperature
Hsum1 += sensor1.relative_humidity
Tsum2 += sensor2.temperature
Hsum2 += sensor2.relative_humidity
Tsum3 += sensor3.temperature
Hsum3 += sensor3.relative_humidity
# utime.sleep(readInterval)
degC = Tsum1 / avgCount
RH = Hsum1 / avgCount
degC2 = Tsum2 / avgCount
RH2 = Hsum2 / avgCount
degC3 = Tsum3 / avgCount
RH3 = Hsum3 / avgCount
et = (ticks_ms() - tStart)/1000.0 # units of seconds
print("%.1f, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f"
% (et, degC, degC2, degC3, RH, RH2, RH3))
msg1 = ("1 %4.2fC %4.2f%%" % (degC, RH))
msg2 = ("2 %4.2fC %4.2f%%" % (degC2, RH2))
msg3 = ("3 %4.2fC %4.2f%%" % (degC3, RH3))
msg4 = ("%.1f s" % (et))
display.fill(0)
display.text(msg1,1,10, color=1)
display.text(msg2,1,20, color=1)
display.text(msg3,1,30, color=1)
display.text(msg4,1,50, color=1)
display.show()
except OSError as e:
print("Encountered OSError in main loop")
print(e)
write.clear() # update OLED display
write.line1("ERROR")
write.line2(e)
write.show() # refresh OLED display
time.sleep(5)
reset()