iT邦幫忙

0

esp8266在使用node-red的mqtt連線失敗

  • 分享至 

  • xImage

如題我在使用node-red做東西,設定都用好了卻不知道為什麼連不上,附上我的程式碼`#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include "Timer.h"

#ifdef SERIAL_DEBUG
#define DEBUG_PRINT(x) Serial.print(x)
#define DEBUG_PRINTLN(x) Serial.println(x)
#define DEBUG_PRINTF(x, y) Serial.printf(x, y)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
#define DEBUG_PRINTF(x, y)
#endif

Adafruit_MPU6050 mpu;
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);

const char* WIFI_SSID = "";
const char* WIFI_PSK = "";
const char* MQTT_BROKER = "";
const char* TOPIC = "room/sensor";
const char* MQTT_USERNAME = ";
const char* MQTT_PASSWORD = "";
const int MQTT_PORT = 1884;

const uint32_t BAUD_RATE = 115200;
u16 LostCnt = 0;
const u16 ResCmp = 120;

char data;

Timer t1;
void setup() {
// put your setup code here, to run once:
Serial.begin(BAUD_RATE);
WiFi.mode(WIFI_STA);
pinMode(LED_BUILTIN, OUTPUT);

InitMPU6050();

InitWiFi();
Serial.printf("\nConnecting to %s\n", WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PSK);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.printf("WiFi connected\n");
Serial.printf("IP address: ");
Serial.println(WiFi.localIP());

connectMQTTServer();
Serial.println("========A=======");

t1.every(1000, LEDCONTROL);
}

void loop() {
// put your main code here, to run repeatedly:
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
Serial.print(" \tObject Temp: "); Serial.print(temp.temperature); Serial.println(" degC");
Serial.println();

if (WiFi.status() == WL_CONNECTED) {
if (mqttClient.connected()) { // 如果开发板成功连接服务器
mqttClient.publish(TOPIC, readTempStr(temp.temperature));
mqttClient.loop(); // 保持客户端心跳
} else { // 如果开发板未能成功连接服务器
connectMQTTServer(); // 则尝试连接服务器
}
t1.update();
}

delay(3000);

}

void InitWiFi() {
DEBUG_PRINTF("\nConnecting to %s", ssid);
WiFi.setSleepMode(WIFI_NONE_SLEEP);
WiFi.begin(WIFI_SSID, WIFI_PSK);
WiFi.mode(WIFI_STA);

LostCnt = 0;
while (WiFi.status() != WL_CONNECTED) {
LostCnt += 1;
DEBUG_PRINT(".");
delay(1000);
if ((LostCnt % 20 == 0) && (LostCnt < ResCmp) && (LostCnt != 0)) {
DEBUG_PRINT("Failed");
DEBUG_PRINTF("\nReconnecting to %s", ssid);
WiFi.begin(WIFI_SSID, WIFI_PSK);
} else if (LostCnt > ResCmp) {
// TODO : add error signal parameter
// LEDSignal();
DEBUG_PRINTLN("\nRestart ESP8266!!!");
ESP.restart();
}
}
}
void LEDCONTROL () {
if(Serial.available()){
data = Serial.read();
if (data == '1'){
Serial.printf("LED ON");
digitalWrite(LED_BUILTIN, LOW);
}
else{
Serial.printf("LED OFF");
digitalWrite(LED_BUILTIN, HIGH);
}
}
}

void InitMPU6050() {
if (!mpu.begin()) {

Serial.println("Sensor init failed");

while (1)

yield();

}

Serial.println("Found a MPU-6050 sensor");
}

void connectMQTTServer(){
mqttClient.setServer(MQTT_BROKER, MQTT_PORT); // 设置MQTT服务器和端口号
String mqttClient_id = "esp8266-" + WiFi.macAddress(); // 根据ESP8266的MAC地址生成客户端ID(避免与其它ESP8266的客户端ID重名)

while (!mqttClient.connected()) {
Serial.printf("The mqttClient %s connects to the public mqtt broker\n", mqttClient_id.c_str());
if (mqttClient.connect(mqttClient_id.c_str(), MQTT_USERNAME, MQTT_PASSWORD)) {
Serial.println("");
Serial.println("Public emqx mqtt broker connected");
} else {
Serial.print("failed with state ");
Serial.print(mqttClient.state());
delay(2000);
}
}
}

static char tempStr[10];//使用字符数组来代替String对象,char tempStr[10]修改为全局变量或是静态变量static
char* readTempStr(float temperature) {
sprintf(tempStr, "%.2f", temperature);//sprintf()函数将浮点数转换为字符串,并存储到tempStr数组中,返回tempStr数组的指针
return tempStr;//tempStr是一个全局静态变量,因此在readTempStr()函数外部访问它,从而将温度值传递给mqttClient.publish()函数。
}我把位置跟密碼刪了,輸出是以下Connecting to
.....WiFi connected
IP address:
The mqttClient esp8266-connects to the public mqtt broker
failed with state -2The mqttClient esp8266- connects to the public mqtt broker
failed with state -2The mqttClient esp8266- connects to the public mqtt broker
failed with state -2The mqttClient esp8266- connects to the public mqtt broker`希望可以幫解惑,參考以下文章https://blog.csdn.net/m0_54706625/article/details/129862762

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答