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?
INDIVIDUAL_MQTT/mqtt_client.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
91 lines (66 sloc)
2.4 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 asyncio | |
from gmqtt import Client as MQTTClient | |
STOP = asyncio.Event() | |
def on_connect(client, flags, rc, properties): | |
print('Connected') | |
client.subscribe('test/topic') # Subscribing to a topic | |
def on_message(client, topic, payload, qos, properties): | |
print('RECV MSG:', payload) | |
def on_disconnect(client, packet, exc=None): | |
print('Disconnected') | |
def on_subscribe(client, mid, qos, properties): | |
print('SUBSCRIBED') | |
async def main(): | |
client = MQTTClient("client-id") | |
client.on_connect = on_connect | |
client.on_message = on_message | |
client.on_disconnect = on_disconnect | |
client.on_subscribe = on_subscribe | |
await client.connect('127.0.0.1', port=1883) | |
client.publish('test/topic', 'Hello MQTT') | |
await STOP.wait() | |
await client.disconnect() | |
if __name__ == '__main__': | |
asyncio.run(main()) | |
import asyncio | |
from gmqtt import Client as MQTTClient | |
# Define the MQTT settings | |
BROKER = '127.0.0.1' | |
PORT = 1883 # Default MQTT port | |
CITY = 'city_name' | |
DISTRICT = 'district_name' | |
DEVICE_ID = 'device_id' | |
# Define topics | |
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}' | |
STOP = asyncio.Event() | |
def on_connect(client, flags, rc, properties): | |
print('Connected') | |
# Subscribing to the defined topics | |
client.subscribe(ALERTS_TOPIC) | |
client.subscribe(LOCATION_TOPIC) | |
client.subscribe(STATUS_TOPIC) | |
client.subscribe(RESPONSE_TOPIC) | |
def on_message(client, topic, payload, qos, properties): | |
print(f'Received message from {topic}: {payload.decode()}') | |
def on_disconnect(client, packet, exc=None): | |
print('Disconnected') | |
def on_subscribe(client, mid, qos, properties): | |
print('Subscribed') | |
async def main(): | |
client = MQTTClient(DEVICE_ID) | |
client.on_connect = on_connect | |
client.on_message = on_message | |
client.on_disconnect = on_disconnect | |
client.on_subscribe = on_subscribe | |
await client.connect(BROKER, PORT) | |
client.publish(ALERTS_TOPIC, 'Alert message', qos=1) | |
client.publish(LOCATION_TOPIC, 'Location message', qos=1) | |
client.publish(STATUS_TOPIC, 'Status message', qos=1) | |
client.publish(RESPONSE_TOPIC, 'Response message', qos=1) | |
await STOP.wait() | |
await client.disconnect() | |
if __name__ == '__main__': | |
asyncio.run(main()) |