diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..b563b8c --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +Dockerfile +.dockerignore +.git +.cache +.venv/ +*.pyc diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1d17dae --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.venv diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..dc6b3bd --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM python:3-alpine + +RUN apk add build-base + +RUN addgroup -S flask && addgroup -g 997 -S gpio && adduser -S flask -G flask -G gpio + +USER flask + +WORKDIR /usr/src/flask_klaxon + +COPY . . +RUN pip install --no-cache-dir -r requirements.txt + +CMD ["python","klaxon_flask.py"] diff --git a/klaxon_flask.py b/klaxon_flask.py new file mode 100755 index 0000000..8ee726a --- /dev/null +++ b/klaxon_flask.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +from flask import Flask, render_template, request +from paste.translogger import TransLogger +import RPi.GPIO as GPIO +from waitress import serve + + +RELAIS_1_GPIO = 17 +app = Flask(__name__) + + +def setup(): + print('Running setup') + GPIO.setmode(GPIO.BCM) + GPIO.setup(RELAIS_1_GPIO, GPIO.OUT) + GPIO.output(RELAIS_1_GPIO, GPIO.HIGH) # Defaulting to high/off + print('GPIO setup, pin set to off') + + +@app.route('/') +def home(): + return render_template('index.html') + + +@app.route('/start') +def starthorn(): + GPIO.setmode(GPIO.BCM) + GPIO.setup(RELAIS_1_GPIO, GPIO.OUT) + GPIO.output(RELAIS_1_GPIO, GPIO.LOW) # Toggle switch to low/on + return('Sending power to the relay switch, hopefully toggling a horn...') + + +@app.route('/stop') +def stophorn(): + GPIO.setmode(GPIO.BCM) + GPIO.setup(RELAIS_1_GPIO, GPIO.OUT) + GPIO.output(RELAIS_1_GPIO, GPIO.HIGH) # Toggle switch to high/off + GPIO.cleanup() + return('Ceasing power to the relay switch, hopefully silencing a horn...') + + +def destroy(): + return('Cleaning up GPIO') + GPIO.cleanup() + + +if __name__ == '__main__': # Program entrance + try: + setup() + serve(TransLogger(app, setup_console_handler=False), listen='*:5000') + finally: + destroy() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2c07fe5 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +Flask>=2.2.2 +Paste>=3.5.2 +RPi.GPIO>=0.7.0 +waitress>=2.1.2 diff --git a/static/pumpkin.png b/static/pumpkin.png new file mode 100644 index 0000000..36040a7 Binary files /dev/null and b/static/pumpkin.png differ diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..e77ed76 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,44 @@ + + + + + + Klaxon horn jump scare button + + + + + +
+
+ +
+
+ + +