diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fa8bef0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +Program/__pycache__/env.cpython-311.pyc +Program/data.db +Program/env.py diff --git a/DataScraper/datascraper.py b/DataScraper/datascraper.py deleted file mode 100644 index e69de29..0000000 diff --git a/Program/__pycache__/database.cpython-311.pyc b/Program/__pycache__/database.cpython-311.pyc new file mode 100644 index 0000000..11a8aa7 Binary files /dev/null and b/Program/__pycache__/database.cpython-311.pyc differ diff --git a/Program/database.py b/Program/database.py new file mode 100644 index 0000000..167150d --- /dev/null +++ b/Program/database.py @@ -0,0 +1,46 @@ +import sqlite3 +from os import path + +# Function to create the database and tables if they don't exist +def create_database(db_name): + conn = sqlite3.connect(db_name) + c = conn.cursor() + + # Create SensorData table + c.execute('''CREATE TABLE IF NOT EXISTS SensorData + (Id INTEGER PRIMARY KEY AUTOINCREMENT, + Sensor TEXT, + Timestamp TEXT, + Temp INT, + HR INTEGER, + Bat INT)''') + + # Create Sensors table + c.execute('''CREATE TABLE IF NOT EXISTS Sensors + (Id INTEGER PRIMARY KEY AUTOINCREMENT, + Mac TEXT, + Name TEXT)''') + + conn.commit() + conn.close() + +# Check if the database file exists +def check_database(db_name): + if not path.exists(db_name): + print(f"Database '{db_name}' not found. Creating...") + create_database(db_name) + print("Database and tables created successfully.") + else: + print(f"Database '{db_name}' found.") + + +# Function to add data to SensorData table +def add_sensor_data(db_name, sensor, timestamp, temp, hr, bat): + conn = sqlite3.connect(db_name) + c = conn.cursor() + + c.execute('''INSERT INTO SensorData (Sensor, Timestamp, Temp, HR, Bat) + VALUES (?, ?, ?, ?, ?)''', (sensor, timestamp, temp, hr, bat)) + + conn.commit() + conn.close() \ No newline at end of file diff --git a/Program/datascraper.py b/Program/datascraper.py new file mode 100644 index 0000000..54e0343 --- /dev/null +++ b/Program/datascraper.py @@ -0,0 +1,22 @@ +from env import * +from database import add_sensor_data +from bluepy.btle import Scanner + + +def BltDataScrap(): + scanner = Scanner() + 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 diff --git a/Program/main.py b/Program/main.py new file mode 100644 index 0000000..d574b11 --- /dev/null +++ b/Program/main.py @@ -0,0 +1,4 @@ +from database import * +from env import * + +check_database(DBFILE)