2022-01-04 00:05:29 +02:00
|
|
|
from flask import Flask, render_template, jsonify, redirect, abort, request
|
2022-01-02 02:29:31 +02:00
|
|
|
import RPi.GPIO as gpio
|
|
|
|
from time import sleep
|
|
|
|
import subprocess
|
2022-01-03 20:50:24 +02:00
|
|
|
import json
|
2022-01-04 00:05:29 +02:00
|
|
|
import saver
|
|
|
|
|
|
|
|
# Change this if you change the dir
|
|
|
|
HOME_DIR = "/home/pi/PWSS/"
|
2022-01-02 00:40:42 +02:00
|
|
|
|
2022-01-02 02:29:31 +02:00
|
|
|
# MQ-135 gas sensor
|
|
|
|
def stinker():
|
|
|
|
gpio.setmode(gpio.BCM)
|
|
|
|
gpio.setup(4, gpio.IN)
|
|
|
|
|
|
|
|
try:
|
|
|
|
if gpio.input(4):
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
sleep(2)
|
|
|
|
except KeyboardInterrupt:
|
2022-01-03 20:50:24 +02:00
|
|
|
pass
|
2022-01-02 02:29:31 +02:00
|
|
|
finally:
|
|
|
|
gpio.cleanup()
|
|
|
|
|
|
|
|
# BME280 Humidity, Temperature, Pressure sensor
|
2022-01-03 20:50:24 +02:00
|
|
|
def htp():
|
2022-01-04 00:05:29 +02:00
|
|
|
string = subprocess.check_output([f"{HOME_DIR}/HTP"]).decode(encoding='UTF-8',errors='strict')
|
2022-01-02 02:29:31 +02:00
|
|
|
string = string.split("temperature:")[1]
|
|
|
|
temperature = string.split(" pressure:")[0].replace("*C", "C")
|
|
|
|
string = string.split(" pressure:")[1]
|
|
|
|
pressure = string.split(" humidity:")[0]
|
|
|
|
humidity = string.split(" humidity:")[1].replace("\r\n", "")
|
|
|
|
result = {
|
2022-01-03 20:50:24 +02:00
|
|
|
"temperature":temperature.replace(" ", ""),
|
|
|
|
"pressure":pressure.replace(" ", ""),
|
|
|
|
"humidity":humidity.replace(" ", "")
|
|
|
|
}
|
2022-01-02 02:29:31 +02:00
|
|
|
# for i in string:
|
|
|
|
# if i.isdigit():
|
|
|
|
# result.append(i)
|
|
|
|
|
|
|
|
return result
|
|
|
|
# Flask server
|
2022-01-02 00:40:42 +02:00
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
@app.route("/")
|
2022-01-03 20:50:24 +02:00
|
|
|
def home():
|
|
|
|
hpt = htp()
|
|
|
|
stank = stinker()
|
|
|
|
return render_template("index.html", HTP=hpt, stinker=stank)
|
|
|
|
|
|
|
|
@app.route("/api/all")
|
|
|
|
def all_data():
|
2022-01-04 00:05:29 +02:00
|
|
|
with open(f"{HOME_DIR}/api/all.json","r") as f:
|
2022-01-03 20:50:24 +02:00
|
|
|
data = json.load(f)
|
|
|
|
return jsonify(data)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/api/daily")
|
|
|
|
def daily_data():
|
2022-01-04 00:05:29 +02:00
|
|
|
with open(f"{HOME_DIR}/api/daily.json","r") as f:
|
2022-01-03 20:50:24 +02:00
|
|
|
data = json.load(f)
|
|
|
|
return jsonify(data)
|
|
|
|
|
2022-01-02 00:40:42 +02:00
|
|
|
|
2022-01-04 00:05:29 +02:00
|
|
|
@app.route("/api/save", methods = ['GET', 'POST'])
|
|
|
|
def save_data():
|
|
|
|
if request.method == "POST":
|
|
|
|
print("will it save?")
|
|
|
|
saver.manual()
|
|
|
|
print("saved main")
|
|
|
|
return redirect("/")
|
|
|
|
else:
|
|
|
|
return redirect("/api/all")
|
|
|
|
|
|
|
|
|
2022-01-02 00:40:42 +02:00
|
|
|
if __name__=="__main__":
|
2022-01-02 02:29:31 +02:00
|
|
|
app.run(host='0.0.0.0')
|