Install liblgpio from source when apt packages are missing.
Bookworm does not ship python3-lgpio; the pip module needs liblgpio.so.1. Build Joan's lg library during install and look on system dist-packages at runtime. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+88
-5
@@ -27,6 +27,10 @@ APT_PACKAGES=(
|
|||||||
python3-venv
|
python3-venv
|
||||||
python3-dev
|
python3-dev
|
||||||
python3-pip
|
python3-pip
|
||||||
|
python3-setuptools
|
||||||
|
swig
|
||||||
|
unzip
|
||||||
|
wget
|
||||||
default-libmysqlclient-dev
|
default-libmysqlclient-dev
|
||||||
build-essential
|
build-essential
|
||||||
)
|
)
|
||||||
@@ -158,6 +162,55 @@ install_apt_deps() {
|
|||||||
DEBIAN_FRONTEND=noninteractive apt-get install -y "${missing[@]}"
|
DEBIAN_FRONTEND=noninteractive apt-get install -y "${missing[@]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
install_optional_lgpio_packages() {
|
||||||
|
local pkg
|
||||||
|
local available=()
|
||||||
|
|
||||||
|
for pkg in python3-lgpio liblgpio1 liblgpio-dev; do
|
||||||
|
if apt-cache show "$pkg" >/dev/null 2>&1; then
|
||||||
|
available+=("$pkg")
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
if ((${#available[@]} == 0)); then
|
||||||
|
log "No apt lgpio packages in this distro (normal on Bookworm)"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
log "Installing apt lgpio packages: ${available[*]}"
|
||||||
|
DEBIAN_FRONTEND=noninteractive apt-get install -y "${available[@]}"
|
||||||
|
}
|
||||||
|
|
||||||
|
liblgpio_present() {
|
||||||
|
ldconfig -p 2>/dev/null | grep -q 'liblgpio\.so\.1'
|
||||||
|
}
|
||||||
|
|
||||||
|
install_liblgpio_from_source() {
|
||||||
|
local builddir
|
||||||
|
|
||||||
|
if liblgpio_present; then
|
||||||
|
log "liblgpio.so.1 already present"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
log "Building liblgpio from source (https://github.com/joan2937/lg)"
|
||||||
|
builddir="$(mktemp -d)"
|
||||||
|
(
|
||||||
|
cd "$builddir"
|
||||||
|
wget -q -O lg.zip https://github.com/joan2937/lg/archive/master.zip
|
||||||
|
unzip -q lg.zip
|
||||||
|
cd lg-master
|
||||||
|
make
|
||||||
|
make install
|
||||||
|
)
|
||||||
|
rm -rf "$builddir"
|
||||||
|
ldconfig
|
||||||
|
|
||||||
|
if ! liblgpio_present; then
|
||||||
|
die "liblgpio.so.1 is still missing after building joan2937/lg"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
remove_legacy_install() {
|
remove_legacy_install() {
|
||||||
if [[ -d "$LEGACY_DIR" ]]; then
|
if [[ -d "$LEGACY_DIR" ]]; then
|
||||||
log "Removing legacy install directory ${LEGACY_DIR}"
|
log "Removing legacy install directory ${LEGACY_DIR}"
|
||||||
@@ -194,15 +247,41 @@ install_app_files() {
|
|||||||
create_venv() {
|
create_venv() {
|
||||||
local venv_python="${INSTALL_ROOT}/venv/bin/python"
|
local venv_python="${INSTALL_ROOT}/venv/bin/python"
|
||||||
local venv_pip="${INSTALL_ROOT}/venv/bin/pip"
|
local venv_pip="${INSTALL_ROOT}/venv/bin/pip"
|
||||||
|
local pyvenv_cfg="${INSTALL_ROOT}/venv/pyvenv.cfg"
|
||||||
|
|
||||||
log "Creating Python virtual environment"
|
log "Creating Python virtual environment"
|
||||||
if [[ ! -x "$venv_python" ]]; then
|
if [[ ! -x "$venv_python" ]]; then
|
||||||
python3 -m venv "${INSTALL_ROOT}/venv"
|
python3 -m venv "${INSTALL_ROOT}/venv"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# apt python3-lgpio lives in system dist-packages; the venv must see it
|
||||||
|
# if pip cannot build Joan's lgpio module.
|
||||||
|
if [[ -f "$pyvenv_cfg" ]]; then
|
||||||
|
sed -i 's/^include-system-site-packages = .*/include-system-site-packages = true/' "$pyvenv_cfg"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Also expose apt dist-packages via a .pth so lgpio imports even if the
|
||||||
|
# venv was originally created without system site packages.
|
||||||
|
local pyver
|
||||||
|
pyver="$("$venv_python" -c 'import sys; print("%d.%d" % sys.version_info[:2])')"
|
||||||
|
local site_packages="${INSTALL_ROOT}/venv/lib/python${pyver}/site-packages"
|
||||||
|
if [[ -d "$site_packages" ]]; then
|
||||||
|
printf '%s\n' '/usr/lib/python3/dist-packages' >"${site_packages}/system-dist-packages.pth"
|
||||||
|
fi
|
||||||
|
|
||||||
log "Installing Python dependencies into venv"
|
log "Installing Python dependencies into venv"
|
||||||
"$venv_pip" install --upgrade pip
|
"$venv_pip" install --upgrade pip
|
||||||
"$venv_pip" install -r "${PYTHON_CODE}/requirements.txt"
|
"$venv_pip" install -r "${PYTHON_CODE}/requirements.txt"
|
||||||
|
|
||||||
|
if ! "$venv_python" -c 'import lgpio' >/dev/null 2>&1; then
|
||||||
|
log "lgpio not importable yet; retrying pip install"
|
||||||
|
"$venv_pip" install lgpio || true
|
||||||
|
fi
|
||||||
|
if "$venv_python" -c 'import lgpio' >/dev/null 2>&1; then
|
||||||
|
log "lgpio is available"
|
||||||
|
else
|
||||||
|
log "WARNING: lgpio is not importable; DHT reads will fall back to Adafruit bitbang"
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
install_config() {
|
install_config() {
|
||||||
@@ -296,6 +375,8 @@ do_install() {
|
|||||||
[[ -d "$PYTHON_CODE" ]] || die "Missing python_code directory: ${PYTHON_CODE}"
|
[[ -d "$PYTHON_CODE" ]] || die "Missing python_code directory: ${PYTHON_CODE}"
|
||||||
|
|
||||||
install_apt_deps
|
install_apt_deps
|
||||||
|
install_optional_lgpio_packages
|
||||||
|
install_liblgpio_from_source
|
||||||
remove_legacy_install
|
remove_legacy_install
|
||||||
install_app_files
|
install_app_files
|
||||||
create_venv
|
create_venv
|
||||||
@@ -330,18 +411,20 @@ Installation complete.
|
|||||||
|
|
||||||
Next steps:
|
Next steps:
|
||||||
1. Edit ${CONFIG_FILE} (GPIO pin, MySQL credentials, limits).
|
1. Edit ${CONFIG_FILE} (GPIO pin, MySQL credentials, limits).
|
||||||
2. Ensure the configured table (default: bedRoomTempHumid) exists in your MySQL database.
|
2. If the DHT data wire is on GPIO 4 (physical pin 7), disable 1-Wire
|
||||||
|
(raspi-config → Interface Options → 1-Wire) and reboot.
|
||||||
|
3. Ensure the configured table (default: bedRoomTempHumid) exists in your MySQL database.
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
if [[ "$RUN_MODE" == "systemd" ]]; then
|
if [[ "$RUN_MODE" == "systemd" ]]; then
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
3. Check service status: systemctl status thmonitor
|
4. Check service status: systemctl status thmonitor
|
||||||
4. Follow logs: tail -f ${LOG_FILE}
|
5. Follow logs: tail -f ${LOG_FILE}
|
||||||
EOF
|
EOF
|
||||||
else
|
else
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
3. Cron runs ${BIN_SINGLE} every minute.
|
4. Cron runs ${BIN_SINGLE} every minute.
|
||||||
4. Follow logs: tail -f ${LOG_FILE}
|
5. Follow logs: tail -f ${LOG_FILE}
|
||||||
EOF
|
EOF
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,7 +43,8 @@ def close():
|
|||||||
|
|
||||||
if _chip is not None:
|
if _chip is not None:
|
||||||
try:
|
try:
|
||||||
import lgpio
|
lgpio = _import_lgpio()
|
||||||
|
if lgpio is not None:
|
||||||
lgpio.gpiochip_close(_chip)
|
lgpio.gpiochip_close(_chip)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
@@ -93,22 +94,53 @@ def _warn_if_one_wire_conflict(pin):
|
|||||||
if _one_wire_warned or pin != 4:
|
if _one_wire_warned or pin != 4:
|
||||||
return
|
return
|
||||||
if os.path.isdir('/sys/bus/w1/devices'):
|
if os.path.isdir('/sys/bus/w1/devices'):
|
||||||
logging.warning(
|
logging.error(
|
||||||
'1-Wire appears enabled; GPIO 4 is the default 1-Wire pin '
|
'1-Wire is enabled and PIN is 4 (physical pin 7). '
|
||||||
'and will conflict with a DHT data wire on physical pin 7'
|
'The kernel owns GPIO 4, so the DHT sensor cannot be read. '
|
||||||
|
'Disable 1-Wire (raspi-config → Interface Options → 1-Wire) and reboot, '
|
||||||
|
'or move the data wire and set PIN to that BCM GPIO.'
|
||||||
)
|
)
|
||||||
_one_wire_warned = True
|
_one_wire_warned = True
|
||||||
|
|
||||||
|
|
||||||
|
def _import_lgpio():
|
||||||
|
"""Import lgpio, including the Raspberry Pi OS system package if needed."""
|
||||||
|
try:
|
||||||
|
import lgpio
|
||||||
|
return lgpio
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
import glob
|
||||||
|
import sys
|
||||||
|
|
||||||
|
extra_paths = ['/usr/lib/python3/dist-packages']
|
||||||
|
extra_paths.extend(sorted(glob.glob('/usr/lib/python3.*/dist-packages')))
|
||||||
|
for path in extra_paths:
|
||||||
|
if os.path.isdir(path) and path not in sys.path:
|
||||||
|
sys.path.append(path)
|
||||||
|
|
||||||
|
try:
|
||||||
|
import lgpio
|
||||||
|
logging.info('Loaded lgpio from system dist-packages')
|
||||||
|
return lgpio
|
||||||
|
except ImportError as err:
|
||||||
|
logging.warning(
|
||||||
|
'lgpio is not installed (%s). On Bookworm install liblgpio from '
|
||||||
|
'https://github.com/joan2937/lg (wget the zip, make, sudo make install, '
|
||||||
|
'sudo ldconfig). Trixie can use: sudo apt install python3-lgpio liblgpio1',
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def _open_gpiochip():
|
def _open_gpiochip():
|
||||||
global _chip
|
global _chip
|
||||||
if _chip is not None:
|
if _chip is not None:
|
||||||
return _chip
|
return _chip
|
||||||
|
|
||||||
try:
|
lgpio = _import_lgpio()
|
||||||
import lgpio
|
if lgpio is None:
|
||||||
except ImportError:
|
|
||||||
logging.debug('lgpio is not installed')
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
last_err = None
|
last_err = None
|
||||||
@@ -228,7 +260,9 @@ class _LgpioDHT:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, chip, gpio, dev_type):
|
def __init__(self, chip, gpio, dev_type):
|
||||||
import lgpio as sbc
|
sbc = _import_lgpio()
|
||||||
|
if sbc is None:
|
||||||
|
raise ImportError('lgpio is not installed')
|
||||||
|
|
||||||
self._sbc = sbc
|
self._sbc = sbc
|
||||||
self._chip = chip
|
self._chip = chip
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
adafruit-blinka>=8.0.0
|
adafruit-blinka>=8.0.0
|
||||||
adafruit-circuitpython-dht>=4.0.0
|
adafruit-circuitpython-dht>=4.0.0
|
||||||
lgpio>=0.2.2.0; platform_machine == "aarch64" or platform_machine == "armv7l"
|
lgpio>=0.2.2.0; platform_machine == "aarch64" or platform_machine == "armv7l" or platform_machine == "armv6l"
|
||||||
mysqlclient>=2.2.0
|
mysqlclient>=2.2.0
|
||||||
|
|||||||
Reference in New Issue
Block a user