PWSS/main.py

65 lines
1.6 KiB
Python
Raw Normal View History

from flask import Flask, render_template, jsonify
2022-01-02 02:29:31 +02:00
import RPi.GPIO as gpio
from time import sleep
import subprocess
import json
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:
pass
2022-01-02 02:29:31 +02:00
finally:
gpio.cleanup()
# BME280 Humidity, Temperature, Pressure sensor
def htp():
string = subprocess.check_output(["./home/pi/PWSS/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 = {
"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("/")
def home():
hpt = htp()
stank = stinker()
return render_template("index.html", HTP=hpt, stinker=stank)
@app.route("/api/all")
def all_data():
with open("api/all.json","r") as f:
data = json.load(f)
return jsonify(data)
@app.route("/api/daily")
def daily_data():
with open("api/daily.json","r") as f:
data = json.load(f)
return jsonify(data)
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')