-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patham2315.py
More file actions
80 lines (70 loc) · 2.63 KB
/
Copy patham2315.py
File metadata and controls
80 lines (70 loc) · 2.63 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
import time
import machine #micropython specific
DEBUG = False
#DEBUG = True
CMD_NULL_REQUEST = bytes(())
CMD_REQUEST_HUMID_TEMP = bytes((0x03,0x00,0x04))
class AM2315(object):
"""driver for Aosong AM2315 - Encased I2C Temperature/Humidity Sensor
based on github.com/adafruit/Adafruit_AM2315
"""
def __init__(self, scl = 5, sda = 4, i2c_addr = 0x5C):
#if scl is None:
# scl = board.GPIO5
#if sda is None:
# sda = board.GPIO4
#self._i2c = bitbangio.I2C(scl,sda) #FIXME wakeup doesn't work yet
self._i2c = machine.I2C(-1,machine.Pin(scl),machine.Pin(sda))
self._addr = i2c_addr
self._data_buff = bytearray(8)
self.active = False
def init(self):
for i in range(5):
try:
self._wakeup()
self.active = True
break
except Exception:
print("failed attempt #%d to wakup addr: 0x%02x" % (i,self._addr))
time.sleep(1.0)
print("WARNING: Initialization of AM2315 sensor failed!\n\tsetting attribute active=False")
self.active = False
def _wakeup(self):
## Wake up the sensor by writing its address on the bus
try:
self._i2c.writeto(self._addr, CMD_NULL_REQUEST)
except OSError: #this is expected from a sleeping sensor with no ACK
if DEBUG:
print("on first attempt, no ACK from addr: 0x%02x" % self._addr)
time.sleep(0.01)
#repeat to confirm ACK
try:
self._i2c.writeto(self._addr, CMD_NULL_REQUEST)
except OSError: #not expected
raise Exception("on second attempt, no ACK from addr: 0x%02x" % self._addr)
def get_data(self, d = None):
if d is None:
d = {}
if not self.active:
return d
else:
self._wakeup()
self._i2c.writeto(self._addr,CMD_REQUEST_HUMID_TEMP)
time.sleep(0.01)
self._i2c.readfrom_into(self._addr,self._data_buff)
db = self._data_buff
d['humid'] = (256*db[2] + db[3])/10.0
temp = (256*(db[4] & 0x7F) + db[5])/10.0
if db[4] >= 128:
temp = -temp
d['temp'] = temp
return d
################################################################################
# TEST CODE
################################################################################
if __name__ == "__main__":
ht_sensor = AM2315()
ht_sensor.init()
d = {}
ht_sensor.get_data(d)
print(d)