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?
BTSASS_SE/Jaskaran singhSTORY.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
42 lines (33 sloc)
1 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 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() |