From 19f24a8f5d4792af5338182201626f7fe78ceab8 Mon Sep 17 00:00:00 2001 From: "Jaskaran Kumar (kumarj16)" Date: Thu, 4 Apr 2024 17:12:36 +0100 Subject: [PATCH] new code added --- story.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 story.py diff --git a/story.py b/story.py new file mode 100644 index 0000000..b0e2586 --- /dev/null +++ b/story.py @@ -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!")