diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2022-04-29 16:41:59 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2022-04-29 17:05:35 +0300 |
commit | e93ebaad0a01c90237280cbdf98099abef6adefa (patch) | |
tree | 878050ad240ca6af4286a198439205d291677be9 /src/si7021d.py | |
parent | c412bf2ee0a3fbf9032fc32a26837d4fbc7585c5 (diff) |
temphum: support multiple temp/relhum sensors
Diffstat (limited to 'src/si7021d.py')
-rwxr-xr-x | src/si7021d.py | 79 |
1 files changed, 0 insertions, 79 deletions
diff --git a/src/si7021d.py b/src/si7021d.py deleted file mode 100755 index fe11787..0000000 --- a/src/si7021d.py +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/env python3 -import smbus -import argparse -import asyncio -import json -import logging - -from home.config import config -from home.util import parse_addr - -logger = logging.getLogger(__name__) -bus = None -lock = asyncio.Lock() -delay = 0.01 - - -async def si7021_read(): - async with lock: - await asyncio.sleep(delay) - - # these are still blocking... meh - raw = bus.read_i2c_block_data(0x40, 0xE3, 2) - temp = 175.72 * (raw[0] << 8 | raw[1]) / 65536.0 - 46.85 - - raw = bus.read_i2c_block_data(0x40, 0xE5, 2) - rh = 125.0 * (raw[0] << 8 | raw[1]) / 65536.0 - 6.0 - - return rh, temp - - -async def handle_client(reader, writer): - request = None - while request != 'quit': - try: - request = await reader.read(255) - if request == b'\x04': - break - request = request.decode('utf-8').strip() - except Exception: - break - - if request == 'read': - try: - rh, temp = await asyncio.wait_for(si7021_read(), timeout=3) - data = dict(humidity=rh, temp=temp) - except asyncio.TimeoutError as e: - logger.exception(e) - data = dict(error='i2c call timed out') - else: - data = dict(error='invalid request') - - writer.write((json.dumps(data) + '\r\n').encode('utf-8')) - try: - await writer.drain() - except ConnectionResetError: - pass - - writer.close() - - -async def run_server(host, port): - server = await asyncio.start_server(handle_client, host, port) - async with server: - logger.info('Server started.') - await server.serve_forever() - - -if __name__ == '__main__': - config.load() - - host, port = parse_addr(config['server']['listen']) - - delay = float(config['smbus']['delay']) - bus = smbus.SMBus(int(config['smbus']['bus'])) - - try: - asyncio.run(run_server(host, port)) - except KeyboardInterrupt: - logging.info('Exiting...') |