Skip to content
Permalink
6cedebc0b4
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
34 lines (25 sloc) 826 Bytes
import paho.mqtt.client as mqtt
# Define the callback functions
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to broker")
else:
print(f"Connection failed with code {rc}")
def on_message(client, userdata, message):
print(f"Received message: {message.payload.decode()}")
# Create an MQTT client
client = mqtt.Client()
# Set the callback functions
client.on_connect = on_connect
client.on_message = on_message
# Set the broker and port
broker_address = "broker.hivemq.com"
port = 8883
# Connect to the broker
client.connect(broker_address, port)
# Subscribe to the "blocking" topic
client.subscribe("blocking")
# Publish a report message
client.publish("report", "Blocked user: user123")
# Start the MQTT loop
client.loop_forever()