You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何向运行中的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-clients package:
    • On Ubuntu/Debian: Run sudo apt install mosquitto-clients
    • On Windows/macOS: Download the full Mosquitto package from its official distribution channel
  • To send your "Hello World" message, open a terminal and run:
    mosquitto_pub -h localhost -p 1883 -t "test/my-topic" -m "Hello World"
    
    Let's break down the parameters:
    • -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:
    mosquitto_sub -h localhost -p 1883 -t "test/my-topic"
    
    You'll see "Hello World" pop up right away.

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 localhost as the Host Name and 1883 as the Port, then click "Open".
  • First, you need to send a CONNECT packet 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):
    10 17 00 04 4D 51 54 54 04 02 00 3C 00 05 74 65 73 74 31
    
    This is a basic MQTT 3.1.1 connect packet with client ID "test1", keep-alive set to 60 seconds.
  • Once you receive a CONNACK response (hex 20 02 00 00 means connection success), send the PUBLISH packet for your message:
    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
    
    This publishes "Hello World" to the topic "test/topic" with QoS 0.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:24:21