This commit is contained in:
jeanGaston 2024-05-02 03:18:50 +02:00
parent a56ccaf14c
commit a8cd68d114
8 changed files with 60 additions and 10 deletions

2
.gitignore vendored
View File

@ -12,3 +12,5 @@ data.db
Program/__pycache__/database.cpython-311.pyc
Program/__pycache__/database.cpython-311.pyc
Program/__pycache__/database.cpython-311.pyc
Program/__pycache__/database.cpython-311.pyc
Program/__pycache__/mail.cpython-311.pyc

View File

@ -16,6 +16,21 @@ db_file=${db_file:-data.db}
echo -e "DB_FILE = '$db_file'" > Program/env.py
# Create env.py file
echo -n "Enter database file name (default: data.db): "
read db_file
db_file=${db_file:-data.db}
echo -e "DB_FILE = '$db_file'" > Program/env.py
# Create env.py file
echo -n "Enter database file name (default: data.db): "
read db_file
db_file=${db_file:-data.db}
echo -e "DB_FILE = '$db_file'" > Program/env.py
# Execute main.py
cd Program
python3 main.py

View File

@ -47,8 +47,11 @@ def fetch_outdoor_temperature():
@app.route('/')
def dashboard():
data = fetch_all_data()[:5]
return render_template('index.html', data=data, temperature=None)
# Convert figure to JSON for rendering in template
graph_json = history_graph('Home')
return render_template('index.html', data=data, temperature=None, graph_json=graph_json)
#Route to display the sensor history
@app.route('/history')
@ -63,6 +66,7 @@ def history():
""" S1 = fetch_data_by_sensor("DEMO1")
S2 = fetch_data_by_sensor("DEMO2")
S3 = fetch_data_by_sensor("DEMO3") """
return render_template('history.html', S1=disp_data[0], S2=disp_data[1], S3=disp_data[2])

View File

@ -1,6 +1,10 @@
from datetime import datetime
import sqlite3
from os import path
import plotly.graph_objs as go
import pandas as pd
import time
from env import *
@ -141,3 +145,21 @@ def print_email_settings():
# Close the connection
conn.close()
def history_graph(sensor):
# Fetch sensor data
data = fetch_data_by_sensor(sensor)
df = pd.DataFrame(data, columns=['ID', 'Sensor', 'Timestamp', 'Temp', 'HR', 'Bat'])
# Create traces for temperature and HR
trace_temp = go.Scatter(x=df['Timestamp'], y=df['Temp'], mode='lines', name='Temperature')
trace_hr = go.Scatter(x=df['Timestamp'], y=df['HR'], mode='lines', name='Humidity Rate')
# Create layout
layout = go.Layout(title='Last Hour of History',
xaxis=dict(title='Time'),
yaxis=dict(title='Value'))
# Create figure
fig = go.Figure(data=[trace_temp, trace_hr], layout=layout)
# Convert figure to JSON for rendering in template
graph_json = fig.to_json()

View File

@ -32,10 +32,6 @@ def email(recipient_email, message, ReachedVal, Sensor, TimeStamp):
- The sensor on wich the values are maxed out
- the time stamp
"""
""" port = SMTP_PORT # For SSL
smtp_server = SMTP
sender_email = SMTP_ID # Enter your address
password = SMTP_PWD """
print(fetch_email_settings())
sender_email, password, smtp_server, port, recipient_email = fetch_email_settings()
@ -59,12 +55,14 @@ def email(recipient_email, message, ReachedVal, Sensor, TimeStamp):
# Send the email
server.sendmail(sender_email, recipient_email, msg.as_string())
print("Email sent successfully!")
except Exception as e:
print(f"Failed to send email. Error: {e}")
finally:
print(f" [{datetime.now()}] Email sent successfully!")
# Close the connection
server.quit()
except Exception as e:
print(f"[{datetime.now()}] Failed to send email. Error: {e}")
finally:
None
def check_and_send_email():
# Connect to the database

View File

@ -85,6 +85,15 @@
<p>Unable to fetch weather data.</p>
{% endif %}
</div>
<h1>Last Hour of History</h1>
<div id="graph"></div>
<script>
// Parse JSON data
var graphData = {{ graph_json | safe }};
// Render graph
Plotly.newPlot('graph', graphData.data, graphData.layout);
</script>
<script>
// Refresh the page every 10 seconds
setTimeout(function(){