Migrate monitoring scripts from Python 2 to Python 3.

Replace the dhtreader.so extension with adafruit-circuitpython-dht and run the daemon with python3.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-14 19:53:00 +08:00
co-authored by Cursor
parent 6f12d2c459
commit adb8573833
12 changed files with 113 additions and 28 deletions
+44
View File
@@ -0,0 +1,44 @@
"""DHT sensor reader with the same API as the legacy dhtreader.so extension."""
DHT11 = 11
DHT22 = 22
_devices = {}
def init():
"""Initialize the DHT reader backend (no-op for adafruit_dht)."""
return None
def _get_device(dev_type, pin):
import adafruit_dht
from adafruit_blinka.microcontroller.bcm283x.pin import Pin
key = (dev_type, pin)
if key not in _devices:
gpio = Pin(pin)
if dev_type == DHT11:
_devices[key] = adafruit_dht.DHT11(gpio)
else:
_devices[key] = adafruit_dht.DHT22(gpio)
return _devices[key]
def read(dev_type, pin):
"""
Read temperature (C) and humidity (%).
Returns (temperature, humidity) on success, or (None, None) on failure.
"""
device = _get_device(dev_type, pin)
try:
temperature = device.temperature
humidity = device.humidity
except RuntimeError:
return None, None
if temperature is None or humidity is None:
return None, None
return float(temperature), float(humidity)
Binary file not shown.
+3 -2
View File
@@ -1,8 +1,9 @@
#!/usr/bin/python
#!/usr/bin/env python3
##
## Script to read the last temperature and humidity readings from a MySQL database
##
import time
import MySQLdb
NO_PREVIOUS = ('NULL', 'NULL')
@@ -37,7 +38,7 @@ def main(host, db, user, passwd, logging, sql_retries, sql_timeout):
)
return oldtemp, oldhumid
except MySQLdb.Error as e:
logging.warn(
logging.warning(
'MySQL read failed (attempt %d/%d): %d: %s',
attempt + 1, sql_retries, e.args[0], e.args[1],
)
+4
View File
@@ -0,0 +1,4 @@
adafruit-blinka>=8.0.0
adafruit-circuitpython-dht>=4.0.0
lgpio>=0.2.2.0; platform_machine == "aarch64" or platform_machine == "armv7l"
mysqlclient>=2.2.0
+5 -4
View File
@@ -1,8 +1,9 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
import logging
import time
import dhtreader
import updateMysql
import thmonitor_common as common
@@ -91,7 +92,7 @@ def main():
logging.info(
'Multiple (infinite loop) temperature and humidity reading '
'[version: 1.1, Jonathan Ervine, 2015-06-17]'
'[version: 2.0, Python 3]'
)
run_loop(cfg, dev_type, dhtpin)
+5 -4
View File
@@ -1,11 +1,12 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
import time
import dhtreader
import updateMysql
import readMysql
import updateMysql
import thmonitor_common as common
@@ -77,7 +78,7 @@ def main():
logging.info(
'Single run temperature and humidity reading '
'[version: 1.1, Jonathan Ervine, 2015-06-17]'
'[version: 2.0, Python 3]'
)
return sensor_read(cfg, dev_type, dhtpin)
+5 -4
View File
@@ -10,9 +10,10 @@
# Description: Temperature and Humidity Monitoring daemon
### END INIT INFO
# Change the next 3 lines to suit where you install your script and what you want to call it
# Change the next lines to suit where you install your script and what you want to call it
DIR=/usr/local/bin/thMonitor
DAEMON=$DIR/temp-humid-python-loop.py
PYTHON=/usr/bin/python3
SCRIPT=$DIR/temp-humid-read-loop.py
DAEMON_NAME=thMonitor
# Add any command line options for your daemon here
@@ -29,7 +30,7 @@ PIDFILE=/var/run/$DAEMON_NAME.pid
do_start () {
log_daemon_msg "Starting system $DAEMON_NAME daemon"
start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON -- $DAEMON_OPTS
start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $PYTHON -- $SCRIPT $DAEMON_OPTS
log_end_msg $?
}
do_stop () {
@@ -50,7 +51,7 @@ case "$1" in
;;
status)
status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
status_of_proc "$DAEMON_NAME" "$PYTHON" && exit 0 || exit $?
;;
*)
+6 -8
View File
@@ -1,12 +1,10 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Shared configuration, logging, and sensor validation for thMonitor scripts."""
from __future__ import print_function
import sys
import logging
import ConfigParser
import configparser
DHT11 = 11
DHT22 = 22
@@ -17,9 +15,9 @@ DEFAULT_CONFIG = '/etc/thMonitor.conf'
def load_config(path=DEFAULT_CONFIG):
"""Load and return thMonitor configuration as a dict."""
config = ConfigParser.ConfigParser()
config = configparser.ConfigParser()
if not config.read(path):
print('ERROR: configuration file not found: {0}'.format(path), file=sys.stderr)
print(f'ERROR: configuration file not found: {path}', file=sys.stderr)
sys.exit(1)
return {
@@ -72,7 +70,7 @@ def resolve_dht_type(hwtype):
'2302': (AM2302, 'AM2302'),
}
if hwtype not in types:
logging.warn(
logging.warning(
'Invalid hardware type %r; supported values are 11, 22, and 2302',
hwtype,
)
@@ -86,7 +84,7 @@ def validate_pin(pin):
"""Return GPIO pin number or exit if invalid."""
dhtpin = int(pin)
if dhtpin <= 0:
logging.warn('Invalid GPIO pin number: %s', pin)
logging.warning('Invalid GPIO pin number: %s', pin)
sys.exit(3)
logging.info('using pin #%d', dhtpin)
return dhtpin
+3 -2
View File
@@ -1,8 +1,9 @@
#!/usr/bin/python
#!/usr/bin/env python3
##
## Script to import the temperature and humidity into a MySQL database
##
import time
import MySQLdb
@@ -31,7 +32,7 @@ def main(temp, humid, host, db, user, passwd, logging, sql_retries, sql_timeout)
logging.debug('The MySQL database was successfully updated.')
return 0
except MySQLdb.Error as e:
logging.warn(
logging.warning(
'MySQL update failed (attempt %d/%d): %d: %s',
attempt + 1, sql_retries, e.args[0], e.args[1],
)