Skip to content

Create mqtt_test.py #16

Closed
wants to merge 1 commit into from
Closed
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
29 changes: 29 additions & 0 deletions mqtt_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import paho.mqtt.client as mqtt

BROKER = "broker.hivemq.com"
PORT = 1883

# Callback when client connects
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("✅ Successfully connected to HiveMQ broker!")
else:
print("❌ Failed to connect. Return code:", rc)

# Create client
client = mqtt.Client()

# Attach callback
client.on_connect = on_connect

try:
print("Attempting to connect...")

# Connect to broker
client.connect(BROKER, PORT, 60)

# Start network loop
client.loop_start()

except Exception as e:
print("❌ Connection error:", e)