Skip to content
Permalink
1984609473
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
26 lines (21 sloc) 849 Bytes
import asyncio
from gmqtt import Client as MQTTClient
BROKER = '127.0.0.1'
PORT = 1883
CITY = 'city_name'
DISTRICT = 'district_name'
DEVICE_ID = 'device_id'
ALERTS_TOPIC = f'alerts/{CITY}/{DISTRICT}/{DEVICE_ID}'
LOCATION_TOPIC = f'location/{CITY}/{DISTRICT}/{DEVICE_ID}'
STATUS_TOPIC = f'status/{CITY}/{DISTRICT}/{DEVICE_ID}'
RESPONSE_TOPIC = f'response/{CITY}/{DISTRICT}/{DEVICE_ID}'
async def publish_messages():
client = MQTTClient(DEVICE_ID)
await client.connect(BROKER, PORT)
client.publish(ALERTS_TOPIC, 'Alert: Danger zone', qos=1)
client.publish(LOCATION_TOPIC, 'Location: 37.7749, -122.4194', qos=1)
client.publish(STATUS_TOPIC, 'Status: Active', qos=1)
client.publish(RESPONSE_TOPIC, 'Response: Acknowledged', qos=1)
await client.disconnect()
if __name__ == '__main__':
asyncio.run(publish_messages())