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/Health_Data_Publisher.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
46 lines (36 sloc)
1.37 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 | |
from random import randint, uniform | |
import time # Import the time module | |
# Function to generate random blood pressure | |
def generate_random_blood_pressure(): | |
return randint(90, 140) | |
# Function to generate random heart rate | |
def generate_random_heart_rate(): | |
return randint(60, 100) | |
# Function to generate random body temperature | |
def generate_random_body_temperature(): | |
return round(uniform(97.0, 100.0), 1) | |
# MQTT client setup | |
client = mqtt.Client() | |
client.connect("broker.hivemq.com", 1883) | |
# Loop to publish health data | |
while True: | |
tid = "YT" # Example TID | |
blood_pressure = generate_random_blood_pressure() # Generate random blood pressure | |
heart_rate = generate_random_heart_rate() # Generate random heart rate | |
body_temperature = generate_random_body_temperature() # Generate random body temperature | |
current_time = datetime.now().strftime("%H:%M:%S") | |
data_to_publish = { | |
'tid': tid, | |
'blood_pressure': blood_pressure, | |
'heart_rate': heart_rate, | |
'body_temperature': body_temperature, | |
'time': current_time | |
} | |
data_encoded = json.dumps(data_to_publish) | |
client.publish("health_data", data_encoded) | |
print(f"Just published: {data_to_publish}") | |
# Sleep for 3 seconds before publishing the next data | |
time.sleep(3) |