From bd9b72997ac5e93aaa0574bf97c90043291b6f56 Mon Sep 17 00:00:00 2001 From: Pavel Sklenar Date: Sat, 2 Mar 2024 20:21:58 +0100 Subject: [PATCH] Update Dockerfile and GPIO library usage --- Dockerfile | 10 +++++----- mqtt-switch2.py | 17 ++++++++--------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/Dockerfile b/Dockerfile index 21c5694..dcba7b6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,10 +2,10 @@ FROM python:3.12 # Install necessary packages for GPIO access and other dependencies -RUN apt-get update && apt-get install -y \ - python3-dev \ - python3-rpi.gpio \ - && rm -rf /var/lib/apt/lists/* +# RUN apt-get update && apt-get install -y \ +# python3-dev \ +# python3-rpi.gpio \ +# && rm -rf /var/lib/apt/lists/* # Set the working directory in the container WORKDIR /usr/src/app @@ -14,7 +14,7 @@ WORKDIR /usr/src/app COPY mqtt-switch2.py . # Install required Python libraries -RUN pip install --no-cache-dir paho-mqtt RPi.GPIO timeloop Flask +RUN pip install --no-cache-dir paho-mqtt gpiozero timeloop Flask # Environment variables can be defined in the Dockerfile, but it's better to pass them at runtime # for flexibility and security diff --git a/mqtt-switch2.py b/mqtt-switch2.py index 61b6eaa..858e850 100644 --- a/mqtt-switch2.py +++ b/mqtt-switch2.py @@ -5,7 +5,7 @@ import signal from datetime import datetime, timedelta -import RPi.GPIO as GPIO +from gpiozero import LED import paho.mqtt.client as mqtt from timeloop import Timeloop from flask import Flask, jsonify @@ -31,8 +31,7 @@ logger = logging.getLogger(__name__) # GPIO setup -GPIO.setmode(GPIO.BCM) -GPIO.setup(GPIO_ID, GPIO.OUT) +switch = LED(GPIO_ID) # Timeloop for scheduled tasks tl = Timeloop() @@ -70,12 +69,12 @@ def on_message(self, client, userdata, msg): self.handle_message(payload) def handle_message(self, payload): - state = GPIO.input(GPIO_ID) + state = switch.value if payload == 'ON' and state != 1: - GPIO.output(GPIO_ID, True) + switch.on() logger.info("Turning on the device.") elif payload == 'OFF' and state == 1: - GPIO.output(GPIO_ID, False) + switch.off() logger.info("Turning off the device.") self.publish_state() @@ -86,7 +85,7 @@ def disconnect_callback(self, client, userdata, flags, reason_code, properties): self.publish_availability('offline') def publish_state(self): - state = 'ON' if GPIO.input(GPIO_ID) == 1 else 'OFF' + state = 'ON' if switch.is_lit else 'OFF' logger.info(f"Publishing state {state} to {MQTT_TOPIC}") self.mqttc.publish(MQTT_TOPIC, state, qos=0, retain=True) @@ -109,7 +108,7 @@ def scheduled_turn_off(): global last_call if last_call and (datetime.now() - last_call).seconds / 60 > AUTOMATIC_SHUTDOWN_DELAY: logger.info("No recent activity, turning off the device.") - GPIO.output(GPIO_ID, False) + switch.off() mqtt_controller.publish_state() @tl.job(interval=timedelta(seconds=20)) @@ -132,7 +131,7 @@ def signal_handler(sig, frame): logger.info("Gracefully shutting down...") tl.stop() mqtt_controller.stop() - GPIO.cleanup() + os._exit(0) if __name__ == "__main__": signal.signal(signal.SIGINT, signal_handler)