Skip to content

Commit

Permalink
Update Dockerfile and GPIO library usage
Browse files Browse the repository at this point in the history
  • Loading branch information
pajikos committed Mar 2, 2024
1 parent 5368f96 commit bd9b729
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
17 changes: 8 additions & 9 deletions mqtt-switch2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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()

Expand All @@ -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)

Expand All @@ -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))
Expand All @@ -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)
Expand Down

0 comments on commit bd9b729

Please sign in to comment.