From 6c73f3e7064ac3cb0382369771a80f0e50c9601a Mon Sep 17 00:00:00 2001 From: Jonny Ervine Date: Fri, 14 Aug 2026 23:08:28 +0800 Subject: [PATCH] Add setup/install script --- README.md | 70 +++++-- install.sh | 343 +++++++++++++++++++++++++++++++++++ systemd/thmonitor.service.in | 15 ++ 3 files changed, 413 insertions(+), 15 deletions(-) create mode 100755 install.sh create mode 100644 systemd/thmonitor.service.in diff --git a/README.md b/README.md index a191d80..33ffffa 100644 --- a/README.md +++ b/README.md @@ -3,30 +3,70 @@ Raspberry Pi temperature humidity monitor ## Python 3 setup -The Python monitoring scripts now require Python 3 and use `adafruit-circuitpython-dht` +The Python monitoring scripts require Python 3 and use `adafruit-circuitpython-dht` instead of the legacy Python 2 `dhtreader.so` extension. -On the Raspberry Pi: +### Recommended install (venv + systemd) + +From a clone of this repository on the Raspberry Pi: ```bash -sudo apt install python3 python3-pip python3-dev default-libmysqlclient-dev build-essential -cd python_code -sudo pip3 install -r requirements.txt -sudo cp temphumid.conf /etc/thMonitor.conf # edit credentials and GPIO pin -sudo cp temp-humid-read-loop.py temp-humid-read-single.py updateMysql.py readMysql.py thmonitor_common.py dhtreader.py /usr/local/bin/thMonitor/ -sudo rm -f /usr/local/bin/thMonitor/dhtreader.so # remove legacy Python 2 extension if present -sudo cp thMonitord /etc/init.d/thMonitor -sudo update-rc.d thMonitor defaults +cd rpi-temp-humid-monitor +sudo ./install.sh ``` -The loop daemon runs with `/usr/bin/python3 /usr/local/bin/thMonitor/temp-humid-read-loop.py`. -The single-run script is suitable for cron: +This will: -```cron -* * * * * /usr/bin/python3 /usr/local/bin/thMonitor/temp-humid-read-single.py +- install required system packages (`python3-venv`, `default-libmysqlclient-dev`, etc.) +- create a virtual environment at `/usr/local/lib/thmonitor/venv` +- install Python dependencies into that venv (not system-wide) +- install app files to `/usr/local/lib/thmonitor` +- install `/etc/thMonitor.conf` if it does not already exist +- install command wrappers: `thmonitor-single`, `thmonitor-loop` +- enable and start the `thmonitor` systemd service (loop daemon) + +Edit `/etc/thMonitor.conf` after install (GPIO pin, MySQL credentials, limits). + +Useful commands: + +```bash +sudo systemctl status thmonitor +sudo systemctl restart thmonitor +tail -f /var/log/th-python.log +thmonitor-single # one-off read (uses venv) ``` -The legacy `dhtreader.so` binary is no longer used and can be removed after upgrading. +Cron-based single-shot mode instead of the loop daemon: + +```bash +sudo ./install.sh --cron +``` + +Install without starting the service: + +```bash +sudo ./install.sh --no-start +``` + +Uninstall: + +```bash +sudo ./install.sh --uninstall +``` + +### Manual install (legacy) + +If you prefer not to use the installer: + +```bash +sudo apt install python3 python3-pip python3-dev python3-venv default-libmysqlclient-dev build-essential +python3 -m venv /usr/local/lib/thmonitor/venv +sudo /usr/local/lib/thmonitor/venv/bin/pip install -r python_code/requirements.txt +sudo cp python_code/temphumid.conf /etc/thMonitor.conf # edit credentials and GPIO pin +# copy python_code/*.py to your chosen install directory and point systemd/cron at the venv python +``` + +Do not copy or keep `dhtreader.so`; use `dhtreader.py` instead. --- diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..ab0b87e --- /dev/null +++ b/install.sh @@ -0,0 +1,343 @@ +#!/bin/bash +# +# Install rpi-temp-humid-monitor as a system app with a Python venv. +# +# Usage: +# sudo ./install.sh # systemd loop daemon (default) +# sudo ./install.sh --cron # cron single-shot mode instead +# sudo ./install.sh --no-start # install only, do not enable/start +# sudo ./install.sh --uninstall # remove installed app files +# +set -euo pipefail + +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +PYTHON_CODE="${REPO_ROOT}/python_code" +SERVICE_TEMPLATE="${REPO_ROOT}/systemd/thmonitor.service.in" + +INSTALL_ROOT="${INSTALL_ROOT:-/usr/local/lib/thmonitor}" +CONFIG_FILE="${CONFIG_FILE:-/etc/thMonitor.conf}" +LOG_FILE="${LOG_FILE:-/var/log/th-python.log}" +BIN_SINGLE="${BIN_SINGLE:-/usr/local/bin/thmonitor-single}" +BIN_LOOP="${BIN_LOOP:-/usr/local/bin/thmonitor-loop}" +SYSTEMD_UNIT="${SYSTEMD_UNIT:-/etc/systemd/system/thmonitor.service}" +CRON_FILE="${CRON_FILE:-/etc/cron.d/thmonitor}" +LEGACY_DIR="/usr/local/bin/thMonitor" + +RUN_MODE="systemd" +DO_START=1 +DO_UNINSTALL=0 + +APT_PACKAGES=( + python3 + python3-venv + python3-dev + python3-pip + default-libmysqlclient-dev + build-essential +) + +PYTHON_MODULES=( + dhtreader.py + readMysql.py + temp-humid-read-loop.py + temp-humid-read-single.py + thmonitor_common.py + updateMysql.py +) + +usage() { + sed -n '3,10p' "$0" | sed 's/^# \?//' + echo + echo "Environment overrides:" + echo " INSTALL_ROOT Install directory (default: ${INSTALL_ROOT})" + echo " CONFIG_FILE Config file path (default: ${CONFIG_FILE})" +} + +log() { + printf '==> %s\n' "$*" +} + +die() { + printf 'ERROR: %s\n' "$*" >&2 + exit 1 +} + +need_root() { + if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then + die "Run this script as root (e.g. sudo $0)" + fi +} + +parse_args() { + while [[ $# -gt 0 ]]; do + case "$1" in + --cron) + RUN_MODE="cron" + shift + ;; + --no-start) + DO_START=0 + shift + ;; + --uninstall) + DO_UNINSTALL=1 + shift + ;; + -h|--help) + usage + exit 0 + ;; + *) + die "Unknown option: $1 (try --help)" + ;; + esac + done +} + +install_apt_deps() { + local missing=() + local pkg + + for pkg in "${APT_PACKAGES[@]}"; do + if ! dpkg -s "$pkg" >/dev/null 2>&1; then + missing+=("$pkg") + fi + done + + if ((${#missing[@]} == 0)); then + log "System packages already installed" + return + fi + + log "Installing system packages: ${missing[*]}" + apt-get update + DEBIAN_FRONTEND=noninteractive apt-get install -y "${missing[@]}" +} + +remove_legacy_install() { + if [[ -d "$LEGACY_DIR" ]]; then + log "Removing legacy install directory ${LEGACY_DIR}" + rm -rf "$LEGACY_DIR" + fi + + if [[ -f /etc/init.d/thMonitor ]]; then + if command -v invoke-rc.d >/dev/null 2>&1; then + invoke-rc.d thMonitor stop >/dev/null 2>&1 || true + fi + if command -v update-rc.d >/dev/null 2>&1; then + update-rc.d thMonitor remove >/dev/null 2>&1 || true + fi + rm -f /etc/init.d/thMonitor + fi + + find "$INSTALL_ROOT" "$LEGACY_DIR" -name 'dhtreader.so' -delete 2>/dev/null || true +} + +install_app_files() { + local module + + log "Installing application to ${INSTALL_ROOT}" + install -d -m 755 "$INSTALL_ROOT" + + for module in "${PYTHON_MODULES[@]}"; do + install -m 644 "${PYTHON_CODE}/${module}" "${INSTALL_ROOT}/${module}" + done + + install -m 644 "${PYTHON_CODE}/requirements.txt" "${INSTALL_ROOT}/requirements.txt" + rm -f "${INSTALL_ROOT}/dhtreader.so" +} + +create_venv() { + local venv_python="${INSTALL_ROOT}/venv/bin/python" + local venv_pip="${INSTALL_ROOT}/venv/bin/pip" + + log "Creating Python virtual environment" + if [[ ! -x "$venv_python" ]]; then + python3 -m venv "${INSTALL_ROOT}/venv" + fi + + log "Installing Python dependencies into venv" + "$venv_pip" install --upgrade pip + "$venv_pip" install -r "${PYTHON_CODE}/requirements.txt" +} + +install_config() { + install -d -m 755 "$(dirname "$LOG_FILE")" + touch "$LOG_FILE" + chmod 644 "$LOG_FILE" + + if [[ -f "$CONFIG_FILE" ]]; then + log "Keeping existing config ${CONFIG_FILE}" + return + fi + + log "Installing sample config ${CONFIG_FILE}" + install -m 640 "${PYTHON_CODE}/temphumid.conf" "$CONFIG_FILE" +} + +install_bin_wrappers() { + local venv_python="${INSTALL_ROOT}/venv/bin/python" + + log "Installing command wrappers" + cat >"$BIN_SINGLE" <"$BIN_LOOP" <"$SYSTEMD_UNIT" + chmod 644 "$SYSTEMD_UNIT" + + if command -v systemctl >/dev/null 2>&1; then + systemctl daemon-reload + else + log "systemctl not found; systemd unit installed but not loaded" + fi +} + +install_cron_job() { + log "Installing cron job ${CRON_FILE}" + cat >"$CRON_FILE" </dev/null 2>&1 +EOF + chmod 644 "$CRON_FILE" +} + +disable_systemd_service() { + if command -v systemctl >/dev/null 2>&1 && [[ -f "$SYSTEMD_UNIT" ]]; then + systemctl disable --now thmonitor.service >/dev/null 2>&1 || true + fi +} + +enable_systemd_service() { + if [[ "$DO_START" -ne 1 ]]; then + log "Skipping service start (--no-start)" + return + fi + + if ! command -v systemctl >/dev/null 2>&1; then + log "systemctl not available; start the loop manually with ${BIN_LOOP}" + return + fi + + log "Enabling and starting thmonitor.service" + systemctl enable thmonitor.service + systemctl restart thmonitor.service + systemctl --no-pager --full status thmonitor.service || true +} + +remove_cron_job() { + rm -f "$CRON_FILE" +} + +do_install() { + need_root + [[ -d "$PYTHON_CODE" ]] || die "Missing python_code directory: ${PYTHON_CODE}" + + install_apt_deps + remove_legacy_install + install_app_files + create_venv + install_config + install_bin_wrappers + + case "$RUN_MODE" in + systemd) + remove_cron_job + install_systemd_service + enable_systemd_service + ;; + cron) + disable_systemd_service + install_cron_job + ;; + *) + die "Unknown run mode: ${RUN_MODE}" + ;; + esac + + cat </dev/null 2>&1; then + systemctl daemon-reload + fi + + cat <