libgpiod_pulsein fails with "Unable to set line N to input" on Raspberry Pi OS, so read via bitbang and release the pin after a single-shot run. Co-authored-by: Cursor <cursoragent@cursor.com>
93 lines
2.5 KiB
Python
93 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import logging
|
|
import time
|
|
|
|
import dhtreader
|
|
import readMysql
|
|
import updateMysql
|
|
import thmonitor_common as common
|
|
|
|
|
|
def sensor_read(cfg, dev_type, dhtpin):
|
|
"""Perform a single sensor read using the last DB values for validation."""
|
|
oldtemp, oldhumid = readMysql.main(
|
|
cfg['host'], cfg['db'], cfg['table'],
|
|
cfg['username'], cfg['password'],
|
|
logging, cfg['sql_retries'], cfg['sql_timeout'],
|
|
)
|
|
logging.debug(
|
|
'Previous temperature %s and humidity %s loaded from database.',
|
|
oldtemp, oldhumid,
|
|
)
|
|
|
|
limits = {
|
|
'maxtemp': cfg['maxtemp'],
|
|
'mintemp': cfg['mintemp'],
|
|
'tempdiff': cfg['tempdiff'],
|
|
'maxhumid': cfg['maxhumid'],
|
|
'minhumid': cfg['minhumid'],
|
|
'humiddiff': cfg['humiddiff'],
|
|
}
|
|
|
|
for num in range(cfg['retries']):
|
|
try:
|
|
t, h = dhtreader.read(dev_type, dhtpin)
|
|
except Exception:
|
|
if num + 1 < cfg['retries']:
|
|
logging.warning(
|
|
'Exception during sensor read; retry %d/%d',
|
|
num + 1, cfg['retries'],
|
|
)
|
|
time.sleep(cfg['timeout'])
|
|
else:
|
|
logging.error(
|
|
'Sensor read failed after %d attempts; giving up.',
|
|
cfg['retries'],
|
|
)
|
|
continue
|
|
|
|
if not t or not h:
|
|
logging.warning('Sensor returned empty reading; retrying.')
|
|
if num + 1 < cfg['retries']:
|
|
time.sleep(cfg['timeout'])
|
|
continue
|
|
|
|
logging.debug('Temperature and humidity read as %s and %s', t, h)
|
|
if common.reading_is_plausible(t, h, oldtemp, oldhumid, limits):
|
|
updateMysql.main(
|
|
t, h,
|
|
cfg['host'], cfg['db'], cfg['table'],
|
|
cfg['username'], cfg['password'],
|
|
logging, cfg['sql_retries'], cfg['sql_timeout'],
|
|
)
|
|
return 0
|
|
|
|
if num + 1 < cfg['retries']:
|
|
time.sleep(cfg['timeout'])
|
|
|
|
return 1
|
|
|
|
|
|
def main():
|
|
cfg = common.load_config()
|
|
common.setup_logging(cfg['logfile'], cfg['loglevel'])
|
|
|
|
dhtreader.init()
|
|
dev_type = common.resolve_dht_type(cfg['hwtype'])
|
|
dhtpin = common.validate_pin(cfg['pin'])
|
|
|
|
logging.info(
|
|
'Single run temperature and humidity reading '
|
|
'[version: 2.0, Python 3]'
|
|
)
|
|
try:
|
|
return sensor_read(cfg, dev_type, dhtpin)
|
|
finally:
|
|
dhtreader.close()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main())
|