Skip to content

new code added #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions story.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import paho.mqtt.client as mqtt
import time

# Define your MQTT broker details
broker_address = "broker.hivemq.com"
port = 8883
topic = "story/status" # Change this to your desired topic
# Callback when the client connects to the broker
def on_connect(client, userdata, flags, rc):
print(f"Connected with result code {rc}")
client.subscribe(topic)

# Callback when a message is published
def on_publish(client, userdata, mid):
print(f"Message published (mid={mid})")

# Create an MQTT client
client = mqtt.Client()
# Set TLS options for secure connection
client.tls_set()

# Set authentication (if required)
# client.username_pw_set(username="your_username", password="your_password")

# Connect to the broker
client.on_connect = on_connect
client.on_publish = on_publish
client.connect(broker_address, port)

# Start the MQTT loop
client.loop_start()
# Publish a status message
status_message = "Feeling adventurous today!"
client.publish(topic, status_message)

# Wait for a while to allow publishing
time.sleep(2)

# Disconnect from the broker
client.disconnect()
client.loop_stop()

print("Status message published successfully!")