Skip to content
Permalink
main
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
import paho.mqtt.client as mqtt
import time
# MQTT broker details
broker_address = "broker.hivemq.com"
port = 8883
# Callback functions
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribe to the topic for receiving status updates
client.subscribe("social_media/status")
def on_message(client, userdata, msg):
print("Received status: "+str(msg.payload))
def publish_story(client, story):
# Publish the story to the designated topic
client.publish("social_media/story", story)
print("Story published: "+ story)
# Initialize MQTT client
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
# Connect to the broker
client.connect(broker_address, port)
# Start the MQTT loop
client.loop_start()
# Publish a sample story
publish_story(client, "Just posted a new status!")
# Keep the program running
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
client.disconnect()
client.loop_stop()