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

树莓派3与Node-RED MQTT连接失败求助:无法连接mqtt://localhost:1883

Troubleshooting Node-RED MQTT Connection Failures on Raspberry Pi 3

Hey there, let's work through this MQTT connection issue you're facing. Those repeated failures to connect to mqtt://localhost:1883 typically stem from a handful of common issues—here's a step-by-step guide to diagnose and fix them:

1. Verify the MQTT Broker is Installed and Running

Most Raspberry Pi setups use Mosquitto as the default MQTT broker. First, check if it's active:

sudo systemctl status mosquitto
  • If the output shows it's not running, start it with:
    sudo systemctl start mosquitto
    
  • To make sure it launches on boot, enable the service:
    sudo systemctl enable mosquitto
    
  • If Mosquitto isn't installed at all, install it first:
    sudo apt update && sudo apt install mosquitto mosquitto-clients
    

2. Check if Port 1883 is Being Listened On

Confirm the broker is actually listening on the default MQTT port. Run either of these commands:

netstat -tulpn | grep 1883
# Or use ss if netstat isn't installed
ss -tulpn | grep 1883

You should see an entry for mosquitto listening on 127.0.0.1:1883 (or 0.0.0.0:1883 for all interfaces). If no entry appears, check the broker's configuration file (/etc/mosquitto/mosquitto.conf) to ensure it's not set to listen on a different port or disabled.

3. Test Local MQTT Connectivity Directly

Rule out Node-RED as the issue by testing the broker with native MQTT clients:

  1. Open one terminal window and subscribe to a test topic:
    mosquitto_sub -h localhost -t test/topic
    
  2. Open a second terminal and publish a message to the same topic:
    mosquitto_pub -h localhost -t test/topic -m "Hello from Mosquitto"
    

If the first terminal receives the message, your broker is working correctly—focus your troubleshooting on Node-RED's configuration.

4. Double-Check Node-RED MQTT Node Configuration

  • Open your Node-RED editor, navigate to the failing MQTT broker node (165e8c2d.4cb0d4).
  • Confirm the Server field is set to localhost or 127.0.0.1 (avoid using hostnames that might resolve incorrectly).
  • If your broker requires authentication, ensure the Username and Password fields match what's configured in Mosquitto. If you haven't set up auth yet, temporarily enable anonymous access in /etc/mosquitto/mosquitto.conf (comment out allow_anonymous false and any password_file lines) and restart the broker to test.

5. Check for Firewall or Network Restrictions

While local connections usually bypass firewalls, if you have ufw enabled on your Pi, verify it's not blocking port 1883:

sudo ufw status

If needed, allow local MQTT traffic or temporarily disable the firewall to test:

sudo ufw disable

If connecting works after disabling, re-enable the firewall and add a rule for port 1883:

sudo ufw enable
sudo ufw allow 1883/tcp

6. View Detailed Node-RED Logs

Get more context from Node-RED's real-time logs to pinpoint the exact failure reason:

journalctl -u nodered.service -f

This will show live logs as Node-RED runs—look for additional error details like authentication failures or connection timeouts that might not be in the truncated logs you shared.

Start with the first couple of steps, as a stopped or missing broker is the most frequent cause of this issue. Let me know if any of these steps help you resolve the problem!

内容的提问来源于stack exchange,提问作者DOE

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:29:57