ESP32通过MQTT连接树莓派Node-RED失败,求解决方案
ESP32无法连接Node-RED MQTT代理的问题排查与解决建议
我正在开发基于ESP32与DHT11传感器的项目,目标是通过本地WiFi将ESP32采集的温湿度数据经Node-RED发送至树莓派,其中Node-RED作为MQTT服务器代理,但ESP32无法成功连接Node-RED。以下是ESP32上运行的Arduino代码及Node-RED相关配置截图,恳请各位提供解决建议。
ESP32 Arduino代码
#include <WiFi.h> #include <PubSubClient.h> #include <Adafruit_Sensor.h> #include <DHT.h> // Wi-Fi credentials const char* ssid = "hkst2023"; const char* password = "BEftsp2023#"; // MQTT broker configuration const char* mqtt_server = "192.168.1.100"; // Replace with your Raspberry Pi's IP address const int mqtt_port = 1883; const char *topic = "testing"; const char* mqtt_username = "mqtt123"; // MQTT username, if required const char* mqtt_password = "00000000"; // MQTT password, if required // DHT sensor configuration #define DHTPIN 5 // DHT11 data pin (GPIO2 on ESP32) #define DHTTYPE DHT11 // DHT sensor type DHT dht(DHTPIN, DHTTYPE); // Initialize MQTT client WiFiClient espClient; PubSubClient client(espClient); void setup() { // Start DHT sensor dht.begin(); // Connect to Wi-Fi Serial.begin(115200); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); } Serial.println(); Serial.print("Connected to WiFi. IP address: "); Serial.println(WiFi.localIP()); // Connect to MQTT broker client.setServer(mqtt_server, mqtt_port); // If your MQTT broker requires authentication: // client.setCredentials(mqtt_username, mqtt_password); } void loop() { if (!client.connected()) { reconnect(); } client.loop(); // Read temperature and humidity from DHT sensor float humidity = dht.readHumidity(); float temperature = dht.readTemperature(); // Check if the reading was successful if (isnan(humidity) || isnan(temperature)) { Serial.println("Failed to read from DHT sensor!"); delay(2000); return; } // Publish data to MQTT topics char tempStr[6]; char humStr[6]; dtostrf(temperature, 2, 2, tempStr); dtostrf(humidity, 2, 2, humStr); // MQTT topics for temperature and humidity const char* tempTopic = "esp32/temperature"; const char* humTopic = "esp32/humidity"; client.publish(tempTopic, tempStr); client.publish(humTopic, humStr); delay(5000); // Delay for 5 seconds before the next reading } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect("ESP32Client")) { Serial.println("connected"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } }
Node-RED配置截图



解决建议
- 启用MQTT认证:从截图看Node-RED的MQTT代理已配置用户名和密码,但代码中
client.setCredentials被注释,必须取消注释这一行,否则连接会被拒绝。修改后代码片段:// Connect to MQTT broker client.setServer(mqtt_server, mqtt_port); // If your MQTT broker requires authentication: client.setCredentials(mqtt_username, mqtt_password); - 确认IP地址正确性:核实树莓派的本地IP是否为
192.168.1.100,可在树莓派终端执行hostname -I查看当前IP。 - 排查网络连通性:确保ESP32和树莓派在同一WiFi局域网,可通过ESP32串口输出确认WiFi连接成功,再在树莓派执行
ping [ESP32的IP]测试连通性。 - 检查端口监听状态:在树莓派终端执行
sudo lsof -i :1883,确认Node-RED的MQTT节点正在监听1883端口。 - 确保客户端ID唯一:
client.connect("ESP32Client")中的客户端ID需在MQTT代理中唯一,避免与其他设备冲突导致连接失败。 - 查看Node-RED日志:通过树莓派终端
node-red-start启动Node-RED查看日志,或在Node-RED界面右侧菜单栏点击"日志",获取连接失败的具体报错信息。
内容的提问来源于stack exchange,提问作者yong jie
相关产品推荐
相关产品推荐

