Updates
This commit is contained in:
parent
9b284589b4
commit
8cc32d9279
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
Program/__pycache__/env.cpython-311.pyc
|
||||
Program/data.db
|
||||
Program/env.py
|
||||
BIN
Program/__pycache__/database.cpython-311.pyc
Normal file
BIN
Program/__pycache__/database.cpython-311.pyc
Normal file
Binary file not shown.
46
Program/database.py
Normal file
46
Program/database.py
Normal file
@ -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()
|
||||
22
Program/datascraper.py
Normal file
22
Program/datascraper.py
Normal file
@ -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()
|
||||
4
Program/main.py
Normal file
4
Program/main.py
Normal file
@ -0,0 +1,4 @@
|
||||
from database import *
|
||||
from env import *
|
||||
|
||||
check_database(DBFILE)
|
||||
Loading…
Reference in New Issue
Block a user