如何向运行中的HiveMQ Broker发送MQTT消息(如Hello World)?
Got it, let's break down how you can send a "Hello World" message to your running HiveMQ Broker. Since the mbed example is tied to its specific embedded environment, we'll use more accessible tools that work with your local setup:
1. Use the Mosquitto Command-Line Tools (Easiest Option)
Mosquitto provides lightweight, easy-to-use MQTT client tools that are perfect for quick testing.
- First, install the
mosquitto-clientspackage:- On Ubuntu/Debian: Run
sudo apt install mosquitto-clients - On Windows/macOS: Download the full Mosquitto package from its official distribution channel
- On Ubuntu/Debian: Run
- To send your "Hello World" message, open a terminal and run:
Let's break down the parameters:mosquitto_pub -h localhost -p 1883 -t "test/my-topic" -m "Hello World"-h localhost: Points to your local HiveMQ Broker (since it's running on your machine)-p 1883: Uses the port your Broker is listening on (matches your startup log)-t "test/my-topic": The MQTT topic to publish to (you can name this anything you want)-m "Hello World": The actual message content
- To verify the message was received, open another terminal and subscribe to the same topic:
You'll see "Hello World" pop up right away.mosquitto_sub -h localhost -p 1883 -t "test/my-topic"
2. Use Putty (Raw Mode, For Low-Level Testing)
Putty doesn't have a built-in MQTT client, but you can use its Raw mode to connect directly to the Broker's TCP port and manually send MQTT packets. Note: This requires understanding basic MQTT binary packet structure.
- Open Putty, select the Raw connection type. Enter
localhostas the Host Name and1883as the Port, then click "Open". - First, you need to send a
CONNECTpacket to establish a connection with the Broker. Paste this hex string (ensure your tool supports hex input—you might need a Putty plugin or use Netcat instead for easier sending):
This is a basic MQTT 3.1.1 connect packet with client ID "test1", keep-alive set to 60 seconds.10 17 00 04 4D 51 54 54 04 02 00 3C 00 05 74 65 73 74 31 - Once you receive a
CONNACKresponse (hex20 02 00 00means connection success), send thePUBLISHpacket for your message:
This publishes "Hello World" to the topic "test/topic" with QoS 0.30 0D 00 08 74 65 73 74 2F 74 6F 70 69 63 48 65 6C 6C 6F 20 57 6F 72 6C 64
If hex input feels too clunky, use Netcat instead (most systems have it pre-installed):
# Send connect packet echo -n -e '\x10\x17\x00\x04MQTT\x04\x02\x00\x3C\x00\x05test1' | nc localhost 1883 # Send publish packet echo -n -e '\x30\x0D\x00\x08test/topicHello World' | nc localhost 1883
3. Write a Simple Python Script (Flexible & Customizable)
If you want more control or want to integrate this into code, use the Paho MQTT Python library:
- Install the library first:
pip install paho-mqtt - Create a script (e.g.,
mqtt_publish.py) with this code:import paho.mqtt.client as mqtt # Callback that runs when we connect to the Broker def on_connect(client, userdata, flags, rc): print(f"Connected successfully (result code: {rc})") # Publish the "Hello World" message client.publish( topic="test/my-topic", payload="Hello World", qos=0, retain=False ) # Disconnect after sending the message client.disconnect() # Create an MQTT client instance client = mqtt.Client(client_id="python_test_client") # Attach the connect callback client.on_connect = on_connect # Connect to the local HiveMQ Broker client.connect(host="localhost", port=1883, keepalive=60) # Start the client loop to handle connection and message sending client.loop_forever() - Run the script with
python mqtt_publish.py—it will connect, send the message, then disconnect automatically.
内容的提问来源于stack exchange,提问作者Nephilim

