Skip to content
Permalink
3f773c09fb
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
222 lines (172 sloc) 8.13 KB
import paho.mqtt.client as mqtt
import json
import sqlite3
from datetime import datetime
# MQTT callback function for on_connect
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected successfully! Returned code:", rc)
client.subscribe("project/battery/percentage")
client.subscribe("health_data")
client.subscribe("medication_reminder")
client.subscribe("bracelet/speed_altitude")
else:
print("Bad connection. Returned code:", rc)
# Function to determine environment based on altitude
def determine_environment(altitude):
if altitude < 0:
return "Underwater"
elif altitude < 50:
return "On water surface"
elif altitude < 500:
return "On land"
else:
return "In the air"
# MQTT callback function for on_message
def on_message(client, userdata, msg):
try:
data = json.loads(msg.payload.decode("utf-8"))
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
if msg.topic == "project/battery/percentage":
battery_percentage = data[0]
insert_battery_data_into_database({"timestamp": timestamp, "battery_percentage": battery_percentage})
print(f"Battery percentage: {battery_percentage}%, Time: {timestamp}")
elif msg.topic == "health_data":
blood_pressure, heart_rate, body_temperature = data.get('blood_pressure', 0), data.get('heart_rate', 0), data.get('body_temperature', 0)
insert_health_data_into_database({"timestamp": timestamp, "blood_pressure": blood_pressure,
"heart_rate": heart_rate, "body_temperature": body_temperature})
print(f"Blood Pressure: {blood_pressure}, Heart Rate: {heart_rate}, Body Temperature: {body_temperature}, Time: {timestamp}")
elif msg.topic == "medication_reminder":
tid = data.get('tid', '')
current_time = data.get('current_time', '')
medication_reminder = data.get('medication_reminder', '')
insert_medication_data_into_database({"timestamp": timestamp, "tid": tid,
"current_time": current_time, "medication_reminder": medication_reminder})
print(f"TID: {tid}, Current Time: {current_time}, Medication Reminder: {medication_reminder}, Time: {timestamp}")
elif msg.topic == "bracelet/speed_altitude":
altitude, speed = data.get('altitude', 0), data.get('speed', 0)
environment = determine_environment(altitude)
insert_speed_altitude_data_into_database({"timestamp": timestamp, "altitude": altitude,
"speed": speed, "environment": environment})
print(f"Altitude: {altitude}, Speed: {speed}, Environment: {environment}, Time: {timestamp}")
except Exception as e:
print("Error processing message:", e)
# Function to create the SQLite database and table for battery percentage data
def create_battery_database_and_table():
try:
conn = sqlite3.connect('battery_data.db')
cursor = conn.cursor()
# Create table if not exists
cursor.execute('''CREATE TABLE IF NOT EXISTS battery_data
(timestamp TEXT, battery_percentage REAL)''')
conn.commit()
cursor.close()
conn.close()
except sqlite3.Error as e:
print("Error creating database and table for battery data:", e)
# Function to create the SQLite database and table for health data
def create_health_database_and_table():
try:
conn = sqlite3.connect('health_data.db')
cursor = conn.cursor()
# Create table if not exists
cursor.execute('''CREATE TABLE IF NOT EXISTS health_data
(timestamp TEXT, blood_pressure REAL, heart_rate REAL, body_temperature REAL)''')
conn.commit()
cursor.close()
conn.close()
except sqlite3.Error as e:
print("Error creating database and table for health data:", e)
# Function to create the SQLite database and table for medication reminder data
def create_medication_database_and_table():
try:
conn = sqlite3.connect('medication_data.db')
cursor = conn.cursor()
# Create table if not exists
cursor.execute('''CREATE TABLE IF NOT EXISTS medication_data
(timestamp TEXT, tid TEXT, current_time TEXT, medication_reminder TEXT)''')
conn.commit()
cursor.close()
conn.close()
except sqlite3.Error as e:
print("Error creating database and table for medication reminder data:", e)
# Function to create the SQLite database and table for speed and altitude data
def create_speed_altitude_database_and_table():
try:
conn = sqlite3.connect('speed_altitude_data.db')
cursor = conn.cursor()
# Create table if not exists
cursor.execute('''CREATE TABLE IF NOT EXISTS speed_altitude_data
(timestamp TEXT, altitude REAL, speed REAL, environment TEXT)''')
conn.commit()
cursor.close()
conn.close()
except sqlite3.Error as e:
print("Error creating database and table for speed and altitude data:", e)
# Function to insert data into the SQLite database for battery percentage
def insert_battery_data_into_database(data):
try:
conn = sqlite3.connect('battery_data.db')
cursor = conn.cursor()
# Insert data into the table
cursor.execute("INSERT INTO battery_data VALUES (?, ?)",
(data.get('timestamp', None), data.get('battery_percentage', None)))
conn.commit()
cursor.close()
conn.close()
except sqlite3.Error as e:
print("Error inserting battery data into database:", e)
# Function to insert data into the SQLite database for health data
def insert_health_data_into_database(data):
try:
conn = sqlite3.connect('health_data.db')
cursor = conn.cursor()
# Insert data into the table
cursor.execute("INSERT INTO health_data VALUES (?, ?, ?, ?)",
(data.get('timestamp', None), data.get('blood_pressure', None),
data.get('heart_rate', None), data.get('body_temperature', None)))
conn.commit()
cursor.close()
conn.close()
except sqlite3.Error as e:
print("Error inserting health data into database:", e)
# Function to insert data into the SQLite database for medication reminder data
def insert_medication_data_into_database(data):
try:
conn = sqlite3.connect('medication_data.db')
cursor = conn.cursor()
# Insert data into the table
cursor.execute("INSERT INTO medication_data VALUES (?, ?, ?, ?)",
(data.get('timestamp', None), data.get('tid', None),
data.get('current_time', None), data.get('medication_reminder', None)))
conn.commit()
cursor.close()
conn.close()
except sqlite3.Error as e:
print("Error inserting medication reminder data into database:", e)
# Function to insert data into the SQLite database for speed and altitude data
def insert_speed_altitude_data_into_database(data):
try:
conn = sqlite3.connect('speed_altitude_data.db')
cursor = conn.cursor()
# Insert data into the table
cursor.execute("INSERT INTO speed_altitude_data VALUES (?, ?, ?, ?)",
(data.get('timestamp', None), data.get('altitude', None),
data.get('speed', None), data.get('environment', None)))
conn.commit()
cursor.close()
conn.close()
except sqlite3.Error as e:
print("Error inserting speed and altitude data into database:", e)
# MQTT client setup
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("broker.hivemq.com", 1883)
# Create the SQLite databases and tables for each topic
create_battery_database_and_table()
create_health_database_and_table()
create_medication_database_and_table()
create_speed_altitude_database_and_table()
# Loop to continuously process MQTT messages
client.loop_forever()