Skip to content
Permalink
4314b47c03
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
47 lines (39 sloc) 1.37 KB
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()