使用ESP8266连接到MQTT Server的问题

问题描述

我尝试使用ESP连接到带有MQTT服务器的静态IP服务器。以下代码已部署到测试中。

使用两个服务器进行比较。连接的运行状况服务器可以成功发布到esp。但是,服务器有问题,不能接受esp和MQTT服务器之间的连接。 esp的串行输出如下所示,表明该过程已重新存储在以下代码的reconnect()函数中。

有人知道如何解决这个问题吗?

我尝试重新启动计算机并重新安装mosquitto。 pub和sub通过静态IP将引发拒绝连接。

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const char* mqtt_server = "YOUR_RPi_IP_Address";

// Initializes the espClient
WiFiClient espClient;
PubSubClient client(espClient);

// Connect an LED to each GPIO of your ESP8266
const int ledGPIO5 = 5;
const int ledGPIO4 = 4;

void setup_wifi() {
    delay(10);
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid,password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("");
    Serial.print("WiFi connected - ESP IP address: ");
    Serial.println(WiFi.localIP());
}

void callback(String topic,byte* message,unsigned int length) {
    Serial.print("Message arrived on topic: ");
    Serial.print(topic);
    Serial.print(". Message: ");
    String messageTemp;
    for (int i = 0; i < length; i++) {
        Serial.print((char)message\[i\]);
        messageTemp += (char)message\[i\];
    }
    Serial.println();
    // Feel free to add more if statements to control more GPIOs with MQTT
    // If a message is received on the topic home/office/esp1/gpio2,you check if the message is either 1 or 0. Turns the ESP GPIO according to the message
    if(topic=="esp8266/4"){
        Serial.print("Changing GPIO 4 to ");
        if(messageTemp == "1"){
            digitalWrite(ledGPIO4,HIGH);
            Serial.print("On");
        }
        else if(messageTemp == "0"){
            digitalWrite(ledGPIO4,LOW);
            Serial.print("Off");
        }
    }
    if(topic=="esp8266/5"){
        Serial.print("Changing GPIO 5 to ");
        if(messageTemp == "1"){
            digitalWrite(ledGPIO5,HIGH);
            Serial.print("On");
        }
        else if(messageTemp == "0"){
            digitalWrite(ledGPIO5,LOW);
            Serial.print("Off");
        }
    }
    Serial.println();
}

// This functions reconnects your ESP8266 to your MQTT broker
// Change the function below if you want to subscribe to more topics with your ESP8266
void reconnect() {
    // Loop until we're reconnected
    while (!client.connected()) {
        Serial.print("Attempting MQTT connection...");
        if (client.connect("ESP8266Client")) {
            Serial.println("connected");
            // Subscribe or resubscribe to a topic
            // You can subscribe to more topics (to control more LEDs in this example)
            client.subscribe("esp8266/4");
            client.subscribe("esp8266/5");
        } else {
            Serial.print("failed,rc=");
            Serial.print(client.state());
            Serial.println(" try again in 5 seconds");
            // Wait 5 seconds before retrying
            delay(5000);
        }
    }
}

// The setup function sets your ESP GPIOs to Outputs,starts the serial communication at a baud rate of 115200
// Sets your mqtt broker and sets the callback function
// The callback function is what receives messages and actually controls the LEDs
void setup() {
    pinMode(ledGPIO4,OUTPUT);
    pinMode(ledGPIO5,OUTPUT);
    Serial.begin(115200);
    setup_wifi();
    client.setServer(mqtt_server,1883);
    client.setCallback(callback);
}

// For this project,you don't need to change anything in the loop function.
// Basically it ensures that you ESP is connected to your broker
void loop() {
    if (!client.connected()) {
        reconnect();
    }
    if(!client.loop())
        client.connect("ESP8266Client");
}

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...