0
0
Drone-programmingHow-ToBeginner · 4 min read

How to Use HiveMQ Cloud MQTT Broker: Quick Setup Guide

To use the HiveMQ Cloud MQTT broker, first create a free account on HiveMQ Cloud and get your broker URL and credentials. Then connect your MQTT client using the broker URL, port, and authentication details to publish or subscribe to topics securely.
📐

Syntax

The basic syntax to connect to HiveMQ Cloud MQTT broker involves specifying the broker URL, port, client ID, and authentication credentials. You then use MQTT commands to connect, subscribe, and publish messages.

  • broker_url: The HiveMQ Cloud endpoint (e.g., broker.hivemq.com or your custom URL)
  • port: Usually 8883 for secure MQTT (TLS)
  • client_id: Unique identifier for your client
  • username and password: Credentials from HiveMQ Cloud dashboard
plaintext
mqtt_client.connect(broker_url, port, client_id, username, password)
mqtt_client.subscribe(topic)
mqtt_client.publish(topic, message)
💻

Example

This example shows how to connect to HiveMQ Cloud MQTT broker using Python with the paho-mqtt library. It connects securely, subscribes to a topic, and publishes a message.

python
import paho.mqtt.client as mqtt

# HiveMQ Cloud connection details
broker = "YOUR_BROKER_URL"
port = 8883
client_id = "my_mqtt_client"
username = "YOUR_USERNAME"
password = "YOUR_PASSWORD"
topic = "test/topic"

# Callback when a message is received
def on_message(client, userdata, msg):
    print(f"Received message: {msg.payload.decode()} on topic {msg.topic}")

# Create MQTT client and set callbacks
client = mqtt.Client(client_id)
client.username_pw_set(username, password)
client.tls_set()  # Enable TLS for secure connection
client.on_message = on_message

# Connect and subscribe
client.connect(broker, port)
client.subscribe(topic)
client.loop_start()

# Publish a test message
client.publish(topic, "Hello from HiveMQ Cloud!")

# Keep script running to receive messages
import time
time.sleep(5)
client.loop_stop()
client.disconnect()
Output
Received message: Hello from HiveMQ Cloud! on topic test/topic
⚠️

Common Pitfalls

  • Wrong broker URL or port: Use the exact URL and port from your HiveMQ Cloud dashboard; default ports are 8883 for TLS.
  • Missing TLS configuration: HiveMQ Cloud requires secure connections; forgetting tls_set() causes connection failure.
  • Incorrect credentials: Use the username and password generated in HiveMQ Cloud; wrong credentials block connection.
  • Not handling client loop: MQTT clients need a loop to process network events; forgetting loop_start() or loop_forever() stops message flow.
python
## Wrong way (no TLS, no loop):
client.connect(broker, port)
client.subscribe(topic)
client.publish(topic, "Test")

## Right way (with TLS and loop):
client.tls_set()
client.connect(broker, port)
client.subscribe(topic)
client.loop_start()
client.publish(topic, "Test")
📊

Quick Reference

Remember these key points when using HiveMQ Cloud MQTT broker:

  • Use port 8883 with TLS for secure connections.
  • Always call tls_set() before connecting.
  • Use your unique client ID and credentials from HiveMQ Cloud.
  • Start the client loop with loop_start() or loop_forever() to handle messages.
  • Subscribe to topics before publishing if you want to receive messages.

Key Takeaways

Create a HiveMQ Cloud account to get your broker URL and credentials before connecting.
Use TLS (port 8883) and call tls_set() for secure MQTT connections.
Always start the MQTT client loop to send and receive messages properly.
Use correct client ID, username, and password from HiveMQ Cloud dashboard.
Subscribe to topics to receive messages and publish to send messages.