diff --git a/.gitignore b/.gitignore index fa8bef0..84cecd8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,14 @@ Program/__pycache__/env.cpython-311.pyc Program/data.db Program/env.py +Program/__pycache__/Webserver.cpython-311.pyc +Program/__pycache__/datascraper.cpython-311.pyc +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__/database.cpython-311.pyc +data.db +Program/__pycache__/database.cpython-311.pyc +Program/__pycache__/database.cpython-311.pyc +Program/__pycache__/database.cpython-311.pyc diff --git a/Install.sh b/Install.sh index e69de29..c083082 100644 --- a/Install.sh +++ b/Install.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# Install Python3 and pip +sudo apt update +sudo apt install -y python3 python3-pip python3-flask python3-schedule + +# Install required Python modules +pip3 install schedule +pip3 install bluepy + +# 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 new file mode 100644 index 0000000..ee3e4bb --- /dev/null +++ b/Program/Webserver.py @@ -0,0 +1,49 @@ +import threading +from env import * +from flask import Flask, render_template +import sqlite3 + +app = Flask(__name__) +# Function to fetch data from the database +def fetch_all_data(): + conn = sqlite3.connect(DBFILE) + c = conn.cursor() + c.execute("SELECT * FROM SensorData ") + data = c.fetchall() + conn.close() + data.reverse() + return data +# Function to fetch data from the database with the sensor name as a parameter +def fetch_data_by_sensor(sensor): + conn = sqlite3.connect(DBFILE) + c = conn.cursor() + c.execute("SELECT * FROM SensorData WHERE Sensor LIKE ? ", ('%' + sensor + '%',)) + data = c.fetchall() + conn.close() + data.reverse() + return data +# Route to display the database contents +@app.route('/') +def dashboard(): + data = fetch_all_data()[:5] + + return render_template('index.html', data=data) + +#Route to display the sensor history +@app.route('/history') +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=S1, S2=S2, S3=S3) + + +def run_flask(): + app.run() + +def RunInThread_WebServer(): + threading.Thread(target=run_flask, daemon=True).start() +if __name__ == '__main__': + app.run(debug=True) + diff --git a/Program/__pycache__/database.cpython-311.pyc b/Program/__pycache__/database.cpython-311.pyc index 11a8aa7..1f07046 100644 Binary files a/Program/__pycache__/database.cpython-311.pyc and b/Program/__pycache__/database.cpython-311.pyc differ diff --git a/Program/database.py b/Program/database.py index 167150d..ba36946 100644 --- a/Program/database.py +++ b/Program/database.py @@ -9,7 +9,7 @@ def create_database(db_name): # Create SensorData table c.execute('''CREATE TABLE IF NOT EXISTS SensorData (Id INTEGER PRIMARY KEY AUTOINCREMENT, - Sensor TEXT, + FOREIGN KEY (Sensor) REFERENCES Sensors(Name), Timestamp TEXT, Temp INT, HR INTEGER, @@ -17,9 +17,8 @@ def create_database(db_name): # Create Sensors table c.execute('''CREATE TABLE IF NOT EXISTS Sensors - (Id INTEGER PRIMARY KEY AUTOINCREMENT, - Mac TEXT, - Name TEXT)''') + (Mac TEXT RIMARY KEY, + Name TEXT )''') conn.commit() conn.close() diff --git a/Program/datascraper.py b/Program/datascraper.py index 54e0343..d31c95a 100644 --- a/Program/datascraper.py +++ b/Program/datascraper.py @@ -1,22 +1,34 @@ +import schedule +import threading +import time from env import * from database import add_sensor_data from bluepy.btle import Scanner - def BltDataScrap(): scanner = Scanner() - print("Begin device scan") + #print("Begin device scan") devices = scanner.scan(timeout=3.0) for device in devices: if device.addr in SENSORS : - print( - f"Device found {device.addr} ({device.addrType}), " - f"RSSI={device.rssi} dB" - ) - for adtype, description, value in device.getScanData(): - if adtype == 22: - temp = int(value[24:28], 16) / 100 - HR = int(value[28:32], 16) / 100 - Bat = int(value[20:22], 16) - print(f"Temp : {temp} °c \n HR : {HR} % , \n Batterie : {Bat} %") - add_sensor_data() \ No newline at end of file + #print( + #f"Device found {device.addr} ({device.addrType}), " + #f"RSSI={device.rssi} dB" + #) + for adtype, description, value in device.getScanData(): + if adtype == 22: + temp = int(value[24:28], 16) / 100 + HR = int(value[28:32], 16) / 100 + Bat = int(value[20:22], 16) + #print(f"Temp : {temp} °c \n HR : {HR} % , \n Batterie : {Bat} %") + add_sensor_data(DBFILE, SENSORS[device.addr], time.strftime("%Y-%m-%d %H:%M:%S"), temp, HR, Bat) + return 0 +def RunInThread_DataScrap(): + print("######################################################################\nOpen Thread datascrap") + threading.Thread(target=BltDataScrap, daemon=True).start() + +def ScheduleDataScrap(): + RunInThread_DataScrap() + schedule.every(10).seconds.do(RunInThread_DataScrap) #run every 10 seconds + + diff --git a/Program/main.py b/Program/main.py index d574b11..f467779 100644 --- a/Program/main.py +++ b/Program/main.py @@ -1,4 +1,15 @@ from database import * +from datascraper import * +from Webserver import RunInThread_WebServer from env import * +import schedule check_database(DBFILE) +RunInThread_WebServer() +ScheduleDataScrap() + + +while True: + schedule.run_pending() + time.sleep(1) + diff --git a/Program/templates/history.html b/Program/templates/history.html new file mode 100644 index 0000000..3356952 --- /dev/null +++ b/Program/templates/history.html @@ -0,0 +1,129 @@ + + + + + + Database Contents + + + + + +

Database Contents

+

Sensor 1

+ + + + + + + + + + {% for row in S1 %} + + + + + + + + + {% endfor %} +
IdSensorTimestampTempHRBat
{{ row[0] }}{{ row[1] }}{{ row[2] }}{{ row[3] }}{{ row[4] }}{{ row[5] }}
+

Sensor 2

+ + + + + + + + + + {% for row in S2 %} + + + + + + + + + {% endfor %} +
IdSensorTimestampTempHRBat
{{ row[0] }}{{ row[1] }}{{ row[2] }}{{ row[3] }}{{ row[4] }}{{ row[5] }}
+

Sensor 3

+ + + + + + + + + + {% for row in S3 %} + + + + + + + + + {% endfor %} +
IdSensorTimestampTempHRBat
{{ row[0] }}{{ row[1] }}{{ row[2] }}{{ row[3] }}{{ row[4] }}{{ row[5] }}
+ + + + diff --git a/Program/templates/index.html b/Program/templates/index.html new file mode 100644 index 0000000..2690e90 --- /dev/null +++ b/Program/templates/index.html @@ -0,0 +1,88 @@ + + + + + + Sensor Data Dashboard + + + + +
+

Sensor Data Dashboard

+ + + + + + + + + + {% for row in data %} + + + + + + + + + {% endfor %} +
IdSensorTimestampTempHRBat
{{ row[0] }}{{ row[1] }}{{ row[2] }}{{ row[3] }}{{ row[4] }}{{ row[5] }}
+
+ View History + Admin +
+
+ + +