65 lines
1.6 KiB
Python
65 lines
1.6 KiB
Python
from flask import Flask, render_template, jsonify
|
|
import RPi.GPIO as gpio
|
|
from time import sleep
|
|
import subprocess
|
|
import json
|
|
|
|
# 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
|
|
finally:
|
|
gpio.cleanup()
|
|
|
|
# BME280 Humidity, Temperature, Pressure sensor
|
|
def htp():
|
|
string = subprocess.check_output(["./home/pi/PWSS/HTP"]).decode(encoding='UTF-8',errors='strict')
|
|
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(" ", "")
|
|
}
|
|
# for i in string:
|
|
# if i.isdigit():
|
|
# result.append(i)
|
|
|
|
return result
|
|
# Flask server
|
|
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)
|
|
|
|
|
|
if __name__=="__main__":
|
|
app.run(host='0.0.0.0') |