working api, frontend, sensors and camera

This commit is contained in:
yeet 2022-01-03 20:50:24 +02:00
parent 9da65e8dc1
commit ca14043788
7 changed files with 138 additions and 11 deletions

0
HTP Normal file → Executable file
View File

30
api/all.json Normal file
View File

@ -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"
}
}

1
api/daily.json Normal file
View File

@ -0,0 +1 @@
{}

38
main.py
View File

@ -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"<p>{HTP()}</p>"
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')

24
static/style.css Normal file
View File

@ -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;
}

35
stats_saver.py Normal file
View File

@ -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()

21
templates/index.html Normal file
View File

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<title>PWSS</title>
</head>
<body>
<img id="camera" src="http://openwebrx.local:9000/stream.mjpg" width="640" height="480" autoplay="">
<div id="weather-data">
<ul>
<li>{{stinker}}</li>
{% for i in HTP.keys() %}
<li>{{HTP[i]}}</li>
{% endfor %}
</ul>
</div>
</body>
</html>