1
votes

When I upload my code , after 3 iterations of the loop() , I get an soft wdt reset and the NodeMCU restarts. 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 ?

2
I don't have the hardware setting as yours, so can't verify what exactly going wrong. But you should keep things related to setting, configuration and initialisation in setup(), and only include the execution in the loop(). For example, WiFi only need to setup once, not every loop. BTW, what is the while..loop that is comment-out in the setup()? it shouldn't be there at all.hcheung

2 Answers

0
votes

You should not call WiFi.begin OR Firebase.begin more than once. 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.

I think you should un-comment the code you have in your setup function and then in loop, just call your 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);
}
-1
votes

It resets because you're doing something wrong with the way you're handling the Wi-Fi connection. It happened to me once when I tried to make a http request before connecting to the server. 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.