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?
Women-s-Security-MQTT-System-/wearable_device.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
47 lines (39 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 | |
import time | |
from datetime import datetime | |
broker = "localhost" | |
port = 1883 | |
city = "CityName" | |
district = "DistrictName" | |
device_id = "Device123" | |
def publish(client, topic, message, qos): | |
client.publish(topic, json.dumps(message), qos=qos) | |
def main(): | |
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) | |
client.connect(broker, port, 60) | |
while True: | |
timestamp = datetime.now().isoformat() | |
gps_coordinates = {"lat": 12.9716, "long": 77.5946} # Example coordinates | |
battery_level = 85 # Example User battery level | |
alert_type = "panic" # Example alert type | |
alert_message = { | |
"timestamp": timestamp, | |
"gps": gps_coordinates, | |
"battery": battery_level, | |
"alert": alert_type | |
} | |
location_message = { | |
"timestamp": timestamp, | |
"gps": gps_coordinates | |
} | |
status_message = { | |
"timestamp": timestamp, | |
"battery": battery_level | |
} | |
publish(client, f"alerts/{city}/{district}/{device_id}", alert_message, 2) | |
publish(client, f"location/{city}/{district}/{device_id}", location_message, 1) | |
publish(client, f"status/{city}/{district}/{device_id}", status_message, 0) | |
time.sleep(10) # Send data every 10 seconds | |
if __name__ == "__main__": | |
main() |