-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_async.py
86 lines (73 loc) · 2.67 KB
/
read_async.py
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
import asyncio
from atlas_i2c import atlas_i2c
from w1thermsensor import AsyncW1ThermSensor, Unit
import paho.mqtt.publish as publish
# TODO: split these into a proper config file
HOSTNAME = "192.168.1.104"
DS18B20 = {"id": "00000be11b73", "mqtt": "sensorpi/medium/T"}
EZO_PH = {"address": 99, "read_delay": 0.9, "mqtt": "sensorpi/medium/pH"}
EZO_EC = {"address": 100, "read_delay": 0.6, "mqtt": "sensorpi/medium/TDS"}
EZO_HUM = {"address": 111, "read_delay": 0.3, "mqtt": "sensorpi/air"}
def connect_ezo(sensor_cfg):
sensor = atlas_i2c.AtlasI2C()
sensor.set_i2c_address(sensor_cfg["address"])
sensor.read_delay = sensor_cfg["read_delay"]
sensor.mqtt = sensor_cfg["mqtt"]
return sensor
def connect_onewire(sensor_cfg):
try:
sensor = AsyncW1ThermSensor(sensor_id=sensor_cfg["id"])
sensor.mqtt = sensor_cfg["mqtt"]
return sensor
except Exception as exc:
print(f"1W error: {exc}")
return None
async def read_ezo(sensor):
try:
sensor.write("r")
await asyncio.sleep(sensor.read_delay)
response = sensor.read("r")
if response.status_code == 1:
data = response.data.decode("utf-8")
publish.single(sensor.mqtt, payload=data, hostname=HOSTNAME)
except Exception as err:
print(f"Ezo error: {err}")
async def read_onewire(sensor):
try:
data = await sensor.get_temperature(Unit.DEGREES_F)
publish.single(sensor.mqtt, payload=data, hostname=HOSTNAME)
except Exception as err:
print(f"Onewire error: {err}")
async def get_reading(sensor):
if isinstance(sensor, atlas_i2c.AtlasI2C):
try:
sensor.write("r")
await asyncio.sleep(sensor.read_delay)
response = sensor.read("r")
if response.status_code == 1:
data = response.data.decode("utf-8")
except OSError as err:
print(f"OSError: {err}")
except Exception as err:
print(f"Other error: {err}")
elif isinstance(sensor, AsyncW1ThermSensor):
data = await sensor.get_temperature(Unit.DEGREES_F)
publish.single(sensor.mqtt, payload=data, hostname=HOSTNAME)
async def main():
ph = connect_ezo(EZO_PH)
ec = connect_ezo(EZO_EC)
hum = connect_ezo(EZO_HUM)
ds18b20 = connect_onewire(DS18B20)
tasks = [
asyncio.create_task(read_ezo(ph)),
asyncio.create_task(read_ezo(ec)),
asyncio.create_task(read_ezo(hum)),
asyncio.create_task(read_onewire(ds18b20)),
]
await asyncio.gather(*tasks)
if __name__ == "__main__":
import time
start = time.time()
asyncio.run(main())
end = time.time()
print(f"Execution time: {end-start}s")