Permalink
Cannot retrieve contributors at this time
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?
MQTT-SAPTS-4005/Medical_Reminder_main.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
45 lines (35 sloc)
1.21 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import paho.mqtt.client as mqtt | |
import json | |
from datetime import datetime | |
import time | |
# Function to generate medication reminders based on current time | |
def generate_medication_reminder(): | |
current_time = datetime.now().strftime("%H:%M") | |
medication_schedule = { | |
"03:11": "Take Medication A", | |
"00:24": "Take Medication B", | |
"02:55": "Take Medication C" | |
} | |
if current_time in medication_schedule: | |
return medication_schedule[current_time] | |
else: | |
return "No medication required right now" | |
# MQTT client setup | |
client = mqtt.Client() | |
client.subscribe("owntracks/HS/#") | |
client.connect("broker.hivemq.com", 1883) | |
# Loop to publish medication reminders | |
while True: | |
tid = "HS" # Example TID | |
current_time = datetime.now().strftime("%H:%M:%S") | |
medication_reminder = generate_medication_reminder() | |
data_to_publish = { | |
'tid': tid, | |
'current_time': current_time, | |
'medication_reminder': medication_reminder | |
} | |
data_encoded = json.dumps(data_to_publish) | |
client.publish("medication_reminder", data_encoded) | |
print(f"Just published: {data_to_publish}") | |
# Sleep for 60 seconds before publishing the next data | |
time.sleep(3) |