【问题标题】:MQTT code stops working after a couple of hoursMQTT 代码在几个小时后停止工作
【发布时间】:2018-03-18 15:36:44
【问题描述】:

在 Arduino IDE 中为 NodeMCU 编写了一些代码,以使用 MQTT 按下按钮。该代码在一段时间内工作得很好,但几个小时后它就不再响应了。

代码很科学,因为我是超级菜鸟,如下:

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

const char* ssid = "ap_name"; //change
const char* password =  "ap_pw"; //change
const char* mqttServer = "server_ip"; //change
const int mqttPort = 1883; 
const char* mqttUser = "server_name"; //change
const char* mqttPassword = "server_pw"; //change

WiFiClient espClient;
PubSubClient client(espClient);

Servo servo;

void setup() {
  Serial.begin(115200); 
  WiFi.begin(ssid, password);

  servo.attach(D4);
  servo.write(70);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");

  client.setServer(mqttServer, mqttPort);
  client.setCallback(callback);

  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");

    if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {

      Serial.println("connected");  

    } else {

      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);

    }
  }

  client.publish("esp/test", "Hello from ESP8266");
  client.subscribe("esp/test");

}

void callback(char* topic, byte* payload, unsigned int length) {

  Serial.print("Message arrived in topic: ");
  Serial.println(topic);

  Serial.print("Message:");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }

  if(*payload == 49){
    rotServo();
    Serial.println();
    Serial.print("Roterar servo");
    delay(3000);
    client.publish("esp/test", "0");
  }

  Serial.println();
  Serial.println("-----------------------");

}

void rotServo(){
  servo.attach(D4);
  servo.write(70);
  delay(1000);
  servo.write(175);
  delay(2000);
  servo.write(70);
  delay(3000);
  servo.detach();
}

void loop() {
  client.loop();
}

有人知道是什么原因导致它停止工作吗?

【问题讨论】:

    标签: arduino mqtt esp8266 nodemcu servo


    【解决方案1】:

    这可能是因为,如果您的客户端断开连接,您将永远不会重新连接。

    请参阅示例here 了解更多详细信息,但这里是示例中的重新连接函数以及它在循环中的调用方式。您需要对其进行定制以适应您的应用程序。

    void reconnect() {
      // Loop until we're reconnected
      while (!client.connected()) {
        Serial.print("Attempting MQTT connection...");
        // Create a random client ID
        String clientId = "ESP8266Client-";
        clientId += String(random(0xffff), HEX);
        // Attempt to connect
        if (client.connect(clientId.c_str())) {
          Serial.println("connected");
          // Once connected, publish an announcement...
          client.publish("outTopic", "hello world");
          // ... and resubscribe
          client.subscribe("inTopic");
        } else {
          Serial.print("failed, rc=");
          Serial.print(client.state());
          Serial.println(" try again in 5 seconds");
          // Wait 5 seconds before retrying
          delay(5000);
        }
      }
    }
    
    
    void loop() {
    
      if (!client.connected()) {
        reconnect();
      }
      client.loop();
      ...
    }
    

    另一方面,虽然我认为这不会导致您的具体问题,但在您的回调中,您只需将有效负载的内容与数字 49 进行比较。

    if(*payload == 49){
    ...
    }
    

    在开始查看有效负载之前,您应该检查主题是否是您感兴趣的实际主题并且长度也是您所期望的。

    【讨论】:

    • 非常感谢您注意到这一点!现在将修改代码并进行测试。我会尽快回来报告。
    猜你喜欢
    • 2014-09-23
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 2021-07-30
    相关资源
    最近更新 更多