Add setup/install script
This commit is contained in:
@@ -3,30 +3,70 @@ Raspberry Pi temperature humidity monitor
|
|||||||
|
|
||||||
## Python 3 setup
|
## 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.
|
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
|
```bash
|
||||||
sudo apt install python3 python3-pip python3-dev default-libmysqlclient-dev build-essential
|
cd rpi-temp-humid-monitor
|
||||||
cd python_code
|
sudo ./install.sh
|
||||||
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
|
|
||||||
```
|
```
|
||||||
|
|
||||||
The loop daemon runs with `/usr/bin/python3 /usr/local/bin/thMonitor/temp-humid-read-loop.py`.
|
This will:
|
||||||
The single-run script is suitable for cron:
|
|
||||||
|
|
||||||
```cron
|
- install required system packages (`python3-venv`, `default-libmysqlclient-dev`, etc.)
|
||||||
* * * * * /usr/bin/python3 /usr/local/bin/thMonitor/temp-humid-read-single.py
|
- 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.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
Executable
+343
@@ -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" <<EOF
|
||||||
|
#!/bin/sh
|
||||||
|
exec "${venv_python}" "${INSTALL_ROOT}/temp-humid-read-single.py" "\$@"
|
||||||
|
EOF
|
||||||
|
cat >"$BIN_LOOP" <<EOF
|
||||||
|
#!/bin/sh
|
||||||
|
exec "${venv_python}" "${INSTALL_ROOT}/temp-humid-read-loop.py" "\$@"
|
||||||
|
EOF
|
||||||
|
chmod 755 "$BIN_SINGLE" "$BIN_LOOP"
|
||||||
|
}
|
||||||
|
|
||||||
|
install_systemd_service() {
|
||||||
|
local venv_python="${INSTALL_ROOT}/venv/bin/python"
|
||||||
|
|
||||||
|
if [[ ! -f "$SERVICE_TEMPLATE" ]]; then
|
||||||
|
die "Missing systemd template: ${SERVICE_TEMPLATE}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
log "Installing systemd unit ${SYSTEMD_UNIT}"
|
||||||
|
sed \
|
||||||
|
-e "s|@INSTALL_ROOT@|${INSTALL_ROOT}|g" \
|
||||||
|
-e "s|@VENV_PYTHON@|${venv_python}|g" \
|
||||||
|
"$SERVICE_TEMPLATE" >"$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" <<EOF
|
||||||
|
# rpi-temp-humid-monitor: run a single sensor read every minute
|
||||||
|
* * * * * root ${BIN_SINGLE} >/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 <<EOF
|
||||||
|
|
||||||
|
Installation complete.
|
||||||
|
|
||||||
|
App directory : ${INSTALL_ROOT}
|
||||||
|
Virtual env : ${INSTALL_ROOT}/venv
|
||||||
|
Config file : ${CONFIG_FILE}
|
||||||
|
Log file : ${LOG_FILE}
|
||||||
|
Run once : ${BIN_SINGLE}
|
||||||
|
Run loop : ${BIN_LOOP}
|
||||||
|
|
||||||
|
Next steps:
|
||||||
|
1. Edit ${CONFIG_FILE} (GPIO pin, MySQL credentials, limits).
|
||||||
|
2. Ensure the TempHumid table exists in your MySQL database.
|
||||||
|
EOF
|
||||||
|
|
||||||
|
if [[ "$RUN_MODE" == "systemd" ]]; then
|
||||||
|
cat <<EOF
|
||||||
|
3. Check service status: systemctl status thmonitor
|
||||||
|
4. Follow logs: tail -f ${LOG_FILE}
|
||||||
|
EOF
|
||||||
|
else
|
||||||
|
cat <<EOF
|
||||||
|
3. Cron runs ${BIN_SINGLE} every minute.
|
||||||
|
4. Follow logs: tail -f ${LOG_FILE}
|
||||||
|
EOF
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
do_uninstall() {
|
||||||
|
need_root
|
||||||
|
|
||||||
|
log "Uninstalling thmonitor"
|
||||||
|
disable_systemd_service
|
||||||
|
remove_cron_job
|
||||||
|
rm -f "$SYSTEMD_UNIT" "$BIN_SINGLE" "$BIN_LOOP"
|
||||||
|
rm -rf "$INSTALL_ROOT"
|
||||||
|
remove_legacy_install
|
||||||
|
|
||||||
|
if command -v systemctl >/dev/null 2>&1; then
|
||||||
|
systemctl daemon-reload
|
||||||
|
fi
|
||||||
|
|
||||||
|
cat <<EOF
|
||||||
|
|
||||||
|
Uninstall complete.
|
||||||
|
|
||||||
|
Removed:
|
||||||
|
${INSTALL_ROOT}
|
||||||
|
${SYSTEMD_UNIT}
|
||||||
|
${BIN_SINGLE}
|
||||||
|
${BIN_LOOP}
|
||||||
|
${CRON_FILE}
|
||||||
|
|
||||||
|
Kept (not removed):
|
||||||
|
${CONFIG_FILE}
|
||||||
|
${LOG_FILE}
|
||||||
|
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
parse_args "$@"
|
||||||
|
|
||||||
|
if [[ "$DO_UNINSTALL" -eq 1 ]]; then
|
||||||
|
do_uninstall
|
||||||
|
else
|
||||||
|
do_install
|
||||||
|
fi
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Raspberry Pi Temperature and Humidity Monitor
|
||||||
|
After=network-online.target
|
||||||
|
Wants=network-online.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
User=root
|
||||||
|
WorkingDirectory=@INSTALL_ROOT@
|
||||||
|
ExecStart=@VENV_PYTHON@ @INSTALL_ROOT@/temp-humid-read-loop.py
|
||||||
|
Restart=on-failure
|
||||||
|
RestartSec=10
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
Reference in New Issue
Block a user