diff --git a/HTP b/HTP old mode 100644 new mode 100755 diff --git a/api/all.json b/api/all.json new file mode 100644 index 0000000..302234b --- /dev/null +++ b/api/all.json @@ -0,0 +1,30 @@ +{ + "04:06:52": { + "temperature": "25.12C", + "pressure": "960.19hPa", + "humidity": "33.21%", + "air_quality": false, + "time": "04:06:52" + }, + "04:06:58": { + "temperature": "25.12C", + "pressure": "960.22hPa", + "humidity": "33.19%", + "air_quality": false, + "time": "04:06:58" + }, + "04:07:50": { + "temperature": "25.06C", + "pressure": "960.19hPa", + "humidity": "33.26%", + "air_quality": false, + "time": "01/02/22" + }, + "04:08:14": { + "temperature": "25.06C", + "pressure": "960.21hPa", + "humidity": "33.23%", + "air_pollution": false, + "time": "01/02/22" + } +} \ No newline at end of file diff --git a/api/daily.json b/api/daily.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/api/daily.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/main.py b/main.py index 70be027..1f59012 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,8 @@ -from flask import Flask +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(): @@ -15,24 +16,23 @@ def stinker(): return True sleep(2) except KeyboardInterrupt: - print("\n") - + pass finally: - print("clean") gpio.cleanup() # BME280 Humidity, Temperature, Pressure sensor -def HTP(): - string = subprocess.check_output(["./HTP"]).decode(encoding='UTF-8',errors='strict') +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, - "pressure":pressure, - "humidity":humidity} + "temperature":temperature.replace(" ", ""), + "pressure":pressure.replace(" ", ""), + "humidity":humidity.replace(" ", "") + } # for i in string: # if i.isdigit(): # result.append(i) @@ -42,8 +42,24 @@ def HTP(): app = Flask(__name__) @app.route("/") -def hello_world(): - return f"

{HTP()}

" +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') \ No newline at end of file diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..8f8e147 --- /dev/null +++ b/static/style.css @@ -0,0 +1,24 @@ +@import url("https://fonts.googleapis.com/css?family=Ubuntu"); + +body{ + height:100%; + display:flex; + align-items: center; + justify-content: space-around; + flex-direction: column; + font-family:"Ubuntu"; +} + +#weather-data li{ + list-style: none; +} + +#weather-data{ + font-size: xx-large; + background-color: lightgray; + padding-right: 25px; +} + +#camera{ + padding: 20px; +} \ No newline at end of file diff --git a/stats_saver.py b/stats_saver.py new file mode 100644 index 0000000..897d2c2 --- /dev/null +++ b/stats_saver.py @@ -0,0 +1,35 @@ +from main import stinker, htp +from datetime import datetime +import pytz, json + +tz = pytz.timezone('Europe/Sofia') +date = datetime.now(tz).strftime("%x") +specific_time = datetime.now(tz).strftime("%H:%M:%S") + +def get_data(): + data = htp() + data["air_pollution"] = stinker() + data["time"] = specific_time + return data + +def daily_data(): + data = get_data() + with open("api/daily.json", "r+") as f: + f_data = json.load(f) + f_data[date] = data + f.seek(0) + json.dump(f_data, f, indent=4) + f.close() + + +def manual_data(): + data = get_data() + data["time"] = date + with open("api/all.json", "r+") as f: + f_data = json.load(f) + f_data[specific_time] = data + f.seek(0) + json.dump(f_data, f, indent=4) + f.close() + +manual_data() \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..983b0ba --- /dev/null +++ b/templates/index.html @@ -0,0 +1,21 @@ + + + + + + + + PWSS + + + +
+ +
+ + \ No newline at end of file