为什么代码在Arduino esp8266上运行不稳定?

Why code runs unstably on Arduino esp8266?

这是我用 Arduino IDE 闪存到我的 esp8266 (esp-01) 的代码。我尝试了多次闪烁,但有时第一次 运行 成功,第二次 运行 或断电时重置,有时根本 运行 不成功。

#include <ArduinoJson.h>
#include <Firebase.h>
#include <FirebaseArduino.h>
#include <FirebaseCloudMessaging.h>
#include <FirebaseError.h>
#include <FirebaseHttpClient.h>
#include <FirebaseObject.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <AutoConnect.h>
#include <SoftwareSerial.h>
//getting time and date
#include <NTPClient.h>
#include <WiFiUdp.h>
#define FIREBASE_HOST "arduino-9ac24.firebaseio.com"
#define FIREBASE_AUTH "NtHBXaYlV5PGn4cWBTFG5dmjfsbICFpJzc1hle1o"
ESP8266WebServer Server;
AutoConnectConfig Config; 
AutoConnect Portal(Server);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP,5);
String data;
void setup() {
  Serial.println("Module Started"); 
  //esp.begin(115200);
  Serial.begin(115200);
  Portal.begin();
  Serial.println("Server:"+WiFi.localIP().toString());
  //Portal.config(Config);
  Config.autoReconnect = true;
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); 
  timeClient.begin();
} 

void loop() {
  Portal.handleClient();
  timeClient.update();
  if (Firebase.failed()) {
    Serial.println(Firebase.error());
    Serial.println("Trying to reconnect....");
    Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  }
  else{
    if (Serial.available() > 0) {
      while (Serial.available() > 0) {
          data = Serial.read();          //data is the incoming data
     }
      Firebase.setString("/sensor_1/"+timeClient.getFormattedDate(), data);
      Serial.println(data);
    }
  }
  delay(5000);
}

这是输出

14:43:31.005 -> load 0x4010f000, len 1384, room 16  
14:43:31.005 -> ecting..........................(IP unset), DNS1=(IP unset), DNS2=(IP unset))
14:43:31.005 -> [AC] DHCP cli⸮⸮QIQ⸮
14:43:31.005 -> [AC] WiFi.begin() 
14:43:31.005 -> [AC] Connecting..................................timeout IP:(IP unset) 
14:43:33.594 -> [AC] SoftAP configure 172.217.28.1, 172.217.28.1, 255.255.255.0  
14:43:42.952 -> 
14:43:42.952 ->  ets Jan  8 2013,rst cause:4, boot mode:(1,7) 
14:43:42.952 -> 
14:43:42.952 -> wdt reset

看门狗似乎正在重置您的 ESP,因为您的循环耗时太长。这样做是为了避免陷入无限循环,当您执行阻塞循环几秒钟的冗长任务时,可能会发生这种情况。我认为 Firebase 开始连接的时间太长,尤其是因为您在调用它之前没有检查您是否已经连接到 wifi。

通常,您可以通过确保您的循环在较长时间内未被阻塞来解决此问题。我建议您将代码更改为如下内容:

void connect(){
    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

    Serial.print("connecting");

    while (WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        delay(500); // delay will give the esp framework some time to manage wifi stuff and reset the watchdog, so this is fine
    }

    Serial.println();
    Serial.print("connected: ");
    Serial.println(WiFi.localIP());

    // now we are sure that we are connected, so we can
    // call firebase begin and can be sure that it will not block
    // until the wifi eventually connects.
    Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}

void setup(){

    // setup everything that does NOT need a network connection here

    connect();

    // setup everything that DOES need a network connection here

}

void loop(){

    // The same logic applies here. As long as we are not connected,
    // we do not wan't to execute some network code and risk blocking
    // the loop for so long that the watchdog kicks in, therefore we
    // call the connect function and return afterwards to start
    // the loop over (we do this to give the esp framework time to
    // do network stuff and reset the watchdog)
    if( WiFi.status() != WL_CONNECTED ){
        connect();
        return;
    }

    // your regular code

}

如果您想了解有关 ESP 看门狗的更多信息,我建议您阅读以下内容:https://www.sigmdel.ca/michel/program/esp8266/arduino/watchdogs_en.html#ESP8266_HW_WDT