This commit is contained in:
jeanGaston 2024-05-02 03:37:48 +02:00
parent a8cd68d114
commit 06d905ae57
5 changed files with 63 additions and 19 deletions

View File

@ -49,9 +49,9 @@ def dashboard():
data = fetch_all_data()[:5] data = fetch_all_data()[:5]
# Convert figure to JSON for rendering in template # Convert figure to JSON for rendering in template
graph_json = history_graph('Home') temp_graph_json = history_graph_temp()
HR_graph_json = history_graph_HR()
return render_template('index.html', data=data, temperature=None, graph_json=graph_json) return render_template('index.html', data=data, temperature=None, temp_graph_json=temp_graph_json, HR_graph_json=HR_graph_json)
#Route to display the sensor history #Route to display the sensor history
@app.route('/history') @app.route('/history')

View File

@ -27,7 +27,7 @@ def create_database(db_name):
Sensor TEXT, Sensor TEXT,
Timestamp TEXT, Timestamp TEXT,
Temp INT, Temp INT,
HR INTEGER, HR INT,
Bat INT, Bat INT,
FOREIGN KEY (Sensor) REFERENCES Sensors(Name))''') FOREIGN KEY (Sensor) REFERENCES Sensors(Name))''')
@ -145,21 +145,62 @@ def print_email_settings():
# Close the connection # Close the connection
conn.close() conn.close()
def history_graph(sensor): def history_graph_temp():
# Fetch sensor data # Fetch sensor data
data = fetch_data_by_sensor(sensor) sensorList = fetch_all_sensor()
df = pd.DataFrame(data, columns=['ID', 'Sensor', 'Timestamp', 'Temp', 'HR', 'Bat']) Trace = ['','' ,'' ]
# Create traces for temperature and HR i =0
trace_temp = go.Scatter(x=df['Timestamp'], y=df['Temp'], mode='lines', name='Temperature') for mac, name in sensorList:
trace_hr = go.Scatter(x=df['Timestamp'], y=df['HR'], mode='lines', name='Humidity Rate')
data = fetch_data_by_sensor(name)
#print(data)
df = pd.DataFrame(data, columns=['ID', 'Sensor', 'Timestamp', 'Temp', 'HR', 'Bat'])
# Create traces for temperature
Trace[i] = go.Scatter(x=df['Timestamp'], y=df['Temp'], mode='lines', name=name)
#print(Trace[i])
i+=1
#print(trace_temp)
# Create layout # Create layout
layout = go.Layout(title='Last Hour of History', layout = go.Layout(title='Graph Temp history',
xaxis=dict(title='Time'), xaxis=dict(title='Time'),
yaxis=dict(title='Value')) yaxis=dict(title='Temperature in °C'))
# Create figure # Create figure
fig = go.Figure(data=[trace_temp, trace_hr], layout=layout) fig = go.Figure(data=[Trace[0], Trace[1], Trace[2] ], layout=layout )
# Convert figure to JSON for rendering in template # Convert figure to JSON for rendering in template
graph_json = fig.to_json() graph_json = fig.to_json()
#print(graph_json)
return graph_json
def history_graph_HR():
# Fetch sensor data
sensorList = fetch_all_sensor()
Trace = ['','','']
i = 0
for mac, name in sensorList:
data = fetch_data_by_sensor(name)
df = pd.DataFrame(data, columns=['ID', 'Sensor', 'Timestamp', 'Temp', 'HR', 'Bat'])
# Create traces for temperature
Trace[i] = go.Scatter(x=df['Timestamp'], y=df['HR'], mode='lines', name=name)
#print(Trace[i])
i+=1
#print(trace_temp)
# Create layout
layout = go.Layout(title='Graph HR history',
xaxis=dict(title='Time'),
yaxis=dict(title='Humidity Rate in % '))
# Create figure
fig = go.Figure(data=[Trace[0], Trace[1], Trace[2] ], layout=layout )
# Convert figure to JSON for rendering in template
graph_json = fig.to_json()
#print(graph_json)
return graph_json

View File

@ -3,6 +3,7 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<title>Sensor Data Dashboard</title> <title>Sensor Data Dashboard</title>
<!-- <link rel="stylesheet" href="/css"> --> <!-- <link rel="stylesheet" href="/css"> -->
<style>body { <style>body {
@ -85,14 +86,16 @@
<p>Unable to fetch weather data.</p> <p>Unable to fetch weather data.</p>
{% endif %} {% endif %}
</div> </div>
<h1>Last Hour of History</h1> <h1>History in graphs</h1>
<div id="graph"></div> <div id="graph_temp"></div>
<div id="graph_hr"></div>
<script> <script>
// Parse JSON data // Parse JSON data
var graphData = {{ graph_json | safe }}; var temp_graphData = {{ temp_graph_json | safe }};
var HR_graphData = {{ HR_graph_json | safe }};
// Render graph // Render graph
Plotly.newPlot('graph', graphData.data, graphData.layout); Plotly.newPlot('graph_temp', temp_graphData.data, temp_graphData.layout);
Plotly.newPlot('graph_hr', HR_graphData.data, HR_graphData.layout);
</script> </script>
<script> <script>
// Refresh the page every 10 seconds // Refresh the page every 10 seconds