简体   繁体   English

软WDT复位-ESP8266 / NodeMCU

[英]Soft wdt Reset - ESP8266/NodeMCU

When I upload my code , after 3 iterations of the loop() , I get an soft wdt reset and the NodeMCU restarts. 当我上传代码时,在loop()的3次迭代之后,我得到了一个软的wdt复位,并且NodeMCU重新启动。 It happens everytime. 每次都会发生。 What would be the error ? 这是什么错误?

#include <dummy.h>
#include <elapsedMillis.h>
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>

// Set these to run example.
#define FIREBASE_HOST "plugmatebeta.firebaseio.com"
#define FIREBASE_AUTH "zQtW9gVXzNuz1tD8OzaTCoFpIx7MbFjwyncsWnGC"                       
#define WIFI_SSID "6LowPAN"
#define WIFI_PASSWORD "rashmin0703"
#define WifiAlertLED D3
#define pushButton D6
#define outsideButton D7
#define relay D8
#define connectedLED D2

void setup() {
  Serial.begin(9600);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting: ");
  delay(5000);

//  while (WiFi.status()!=WL_CONNECTED){
//    Serial.print(".");
//    delay(100);
//  }
//  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
//  delay(1000);

  pinMode(WifiAlertLED,OUTPUT);
  pinMode(connectedLED,OUTPUT);
  pinMode(pushButton,INPUT);
  pinMode(outsideButton,INPUT);
  pinMode(relay,OUTPUT);


}

void loop() {
  if (WiFi.status() != WL_CONNECTED){
        digitalWrite(connectedLED, LOW);
        WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
        Serial.print("connecting");
        BlinkLED();
        controlOne();
        delay(1000);
    }
  else{
    Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
    delay(500);
    Serial.println("inside connected");
    controlTwo();
  }
}


void BlinkLED(){
  digitalWrite(WifiAlertLED, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);              // wait for a second
  digitalWrite(WifiAlertLED, LOW);    // turn the LED off by making the voltage LOW
  delay(100);   
}

void controlOne(){
  if ((digitalRead(pushButton) == HIGH) && (digitalRead(outsideButton)==HIGH) ){
         digitalWrite(relay, HIGH);
         Serial.println("on");
  }else{
         digitalWrite(relay, LOW);
         Serial.println("off");
  }
}

void controlTwo(){
  String firebaseResult = firebaseAction();
  if ((digitalRead(pushButton) == HIGH) && ( (firebaseResult=="1") || (digitalRead(outsideButton)==HIGH) ) ){
    digitalWrite(relay, HIGH);
    Serial.println("onnnnnnnnn");
    publishtoFirebase("ON");
  }else{
    digitalWrite(relay, LOW);
    Serial.println("onnnnnnnnn");
    publishtoFirebase("OFF");
  } 
}


String firebaseAction(){
    String x =Firebase.getString("/plgm8-1/command");
    yield();
    delay(200);
    Serial.println(x);
    return x;
    delay(100);
}

void publishtoFirebase(String x){
  Firebase.setString("/plgm8-1/status", x);
  delay(200);
  yield();
}

I tried searching for errors regarding , soft wdt reset more , but it seems like the resources are very low. 我尝试搜索有关错误的信息,但似乎资源很低。 Is it a problem with the timer of the NodeMCU ? NodeMCU的计时器有问题吗?

You should not call WiFi.begin OR Firebase.begin more than once. 您不应多次致电WiFi.begin或Firebase.begin。 Once the ESP connects in setup, it will re-connect on it's own if the connection drops, you don't need to handle this inside your loop. 一旦ESP进行了设置连接,如果连接断开,它将自行自行重新连接,您无需在循环中处理此问题。

I think you should un-comment the code you have in your setup function and then in loop, just call your controlTwo(); 我认为您应该取消注释设置函数中的代码,然后循环执行,只需调用controlTwo();

There is one thing to watch out for. 需要注意的一件事。 There is a watchdog timer that will fire if you spend too much time doing nothing, particularly in the setup function, so try to avoid too many un-needed calls to delay or an infinite loop in setup. 如果您花太多时间不进行任何操作(尤其是在设置功能中),则会有一个看门狗定时器触发,因此请避免避免不必要的太多调用来延迟设置或无限循环。

Here is a hacked up version of your code that I think should work. 这是您认为可以使用的经过修改的代码版本。

void setup() {
  Serial.begin(115200);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting: ");
  delay(5000);

   while (WiFi.status()!=WL_CONNECTED){
    Serial.print(".");
    BlinkLED();
    controlOne();
    delay(100);
   }
   Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
   delay(1000);

  pinMode(WifiAlertLED, OUTPUT);
  pinMode(connectedLED, OUTPUT);
  pinMode(pushButton, INPUT);
  pinMode(outsideButton, INPUT);
  pinMode(relay, OUTPUT);
}

void loop() {
  Serial.println("inside connected");
  controlTwo();
  delay(500);
}

It resets because you're doing something wrong with the way you're handling the Wi-Fi connection. 重置是因为您处理Wi-Fi连接的方式有问题。 It happened to me once when I tried to make a http request before connecting to the server. 当我尝试在连接服务器之前发出一个http请求时,它发生了一次。 The compiler does not see this, so you need to know what you're doing. 编译器看不到这一点,因此您需要知道自己在做什么。

Try to run the WiFi.begin() function only once in setup() and close the connection to the firebase server when you're done with it, or just run it once if you want it connected to server 24/7. 尝试在setup()仅运行一次WiFi.begin()函数,并在完成后关闭与Firebase服务器的连接,或者如果希望将其连接到服务器24/7,则仅运行一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM