diff --git a/.gitignore b/.gitignore index 84cecd8..a3ff884 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/Install.sh b/Install.sh index c083082..79b92d9 100644 --- a/Install.sh +++ b/Install.sh @@ -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 diff --git a/Program/Webserver.py b/Program/Webserver.py index 9dbf94b..57d9b57 100644 --- a/Program/Webserver.py +++ b/Program/Webserver.py @@ -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]) diff --git a/Program/__pycache__/database.cpython-311.pyc b/Program/__pycache__/database.cpython-311.pyc index e9c9ad8..6213e20 100644 Binary files a/Program/__pycache__/database.cpython-311.pyc and b/Program/__pycache__/database.cpython-311.pyc differ diff --git a/Program/__pycache__/mail.cpython-311.pyc b/Program/__pycache__/mail.cpython-311.pyc index ba56e53..6187601 100644 Binary files a/Program/__pycache__/mail.cpython-311.pyc and b/Program/__pycache__/mail.cpython-311.pyc differ diff --git a/Program/database.py b/Program/database.py index 46ead2b..13e5cc2 100644 --- a/Program/database.py +++ b/Program/database.py @@ -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() \ No newline at end of file diff --git a/Program/mail.py b/Program/mail.py index 6ada2bc..523aae3 100644 --- a/Program/mail.py +++ b/Program/mail.py @@ -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 diff --git a/Program/templates/index.html b/Program/templates/index.html index 19d4e3d..1e5bac1 100644 --- a/Program/templates/index.html +++ b/Program/templates/index.html @@ -85,6 +85,15 @@

Unable to fetch weather data.

{% endif %} +

Last Hour of History

+
+