Hello, I have a problem with Nodemcu and Firebase. When I uploaded the code to the nodemcu, it runs without any errors. When internet connection is lost (wifi is still connected), it is not connected to the firebase. But I truned on my mobile data, it was still “Trying to reconnect” and can’t connect to the firebase again.
Here is my code:
include
include
define FIREBASE_HOST “YOUR_FIREBASE_PROJECT.firebaseio.com” //Without http:// or https:// schemes
define WIFI_PASSWORD “aaaaaaaa” // your WiFi PASSWORD //Password of your wifi network
define Relay1 12 //D6
int val1;
define Relay2 14 //D2
int val2;
define Relay3 4 //D1
int val3;
define Relay4 5 //D5
int val4;
void setup()
{
Serial.begin(115200); // Select the same baud rate if you want to see the datas on Serial Monitor
pinMode(Relay1,OUTPUT);
pinMode(Relay2,OUTPUT);
pinMode(Relay3,OUTPUT);
pinMode(Relay4,OUTPUT);
WiFi.begin(WIFI_SSID,WIFI_PASSWORD);
Serial.print(“connecting”);
while (WiFi.status()!=WL_CONNECTED){
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print(“connected:”);
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST,FIREBASE_AUTH);
Firebase.setInt(“S1”,0); //Here the varialbe"S1",“S2”,“S3” and “S4” needs to be the one which is used in our Firebase and MIT App Inventor
Firebase.setInt(“S2”,0);
Firebase.setInt(“S3”,0);
Firebase.setInt(“S4”,0);
}
void firebasereconnect()
{
Serial.println(“Trying to reconnect”);
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}
void loop()
{
if (Firebase.failed())
{
Serial.print(“setting number failed:”);
Serial.println(Firebase.error());
firebasereconnect();
return;
}
val1=Firebase.getString(“S1”).toInt(); //Reading the value of the varialble Status from the firebase
if(val1==1) // If, the Status is 1, turn on the Relay1
{
digitalWrite(Relay1,LOW);
Serial.println(“light 1 ON”);
}
else if(val1==0) // If, the Status is 0, turn Off the Relay1
{
digitalWrite(Relay1,HIGH);
Serial.println(“light 1 OFF”);
}
val2=Firebase.getString(“S2”).toInt(); //Reading the value of the varialble Status from the firebase
if(val2==1) // If, the Status is 1, turn on the Relay2
{
digitalWrite(Relay2,LOW);
Serial.println(“light 2 ON”);
}
else if(val2==0) // If, the Status is 0, turn Off the Relay2
{
digitalWrite(Relay2,HIGH);
Serial.println(“light 2 OFF”);
}
val3=Firebase.getString(“S3”).toInt(); //Reading the value of the varialble Status from the firebase
if(val3==1) // If, the Status is 1, turn on the Relay3
{
digitalWrite(Relay3,LOW);
Serial.println(“light 3 ON”);
}
else if(val3==0) // If, the Status is 0, turn Off the Relay3
{
digitalWrite(Relay3,HIGH);
Serial.println(“light 3 OFF”);
}
val4=Firebase.getString(“S4”).toInt(); //Reading the value of the varialble Status from the firebase
if(val4==1) // If, the Status is 1, turn on the Relay4
{
digitalWrite(Relay4,LOW);
Serial.println(“light 4 ON”);
}
else if(val4==0) // If, the Status is 0, turn Off the Relay4
{
digitalWrite(Relay4,HIGH);
Serial.println(“light 4 OFF”);
}
}
crd code: roboshala
Please help me!
What should I do for “Auto Reconnecting to the FireBase”?
Hello, I have a problem with Nodemcu and Firebase. When I uploaded the code to the nodemcu, it runs without any errors. When internet connection is lost (wifi is still connected), it is not connected to the firebase. But I truned on my mobile data, it was still “Trying to reconnect” and can’t connect to the firebase again. Here is my code:
include
include
define FIREBASE_HOST “YOUR_FIREBASE_PROJECT.firebaseio.com” //Without http:// or https:// schemes
define FIREBASE_AUTH “YOUR_FIREBASE_DATABASE_SECRET”
define WIFI_SSID “OnePlus 6T”
define WIFI_PASSWORD “aaaaaaaa” // your WiFi PASSWORD //Password of your wifi network
define Relay1 12 //D6
int val1;
define Relay2 14 //D2
int val2;
define Relay3 4 //D1
int val3;
define Relay4 5 //D5
int val4;
void setup() { Serial.begin(115200); // Select the same baud rate if you want to see the datas on Serial Monitor pinMode(Relay1,OUTPUT); pinMode(Relay2,OUTPUT); pinMode(Relay3,OUTPUT); pinMode(Relay4,OUTPUT);
digitalWrite(Relay1,HIGH); digitalWrite(Relay2,HIGH); digitalWrite(Relay3,HIGH); digitalWrite(Relay4,HIGH);
WiFi.begin(WIFI_SSID,WIFI_PASSWORD); Serial.print(“connecting”); while (WiFi.status()!=WL_CONNECTED){ Serial.print("."); delay(500); } Serial.println(); Serial.print(“connected:”); Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST,FIREBASE_AUTH); Firebase.setInt(“S1”,0); //Here the varialbe"S1",“S2”,“S3” and “S4” needs to be the one which is used in our Firebase and MIT App Inventor Firebase.setInt(“S2”,0); Firebase.setInt(“S3”,0); Firebase.setInt(“S4”,0);
} void firebasereconnect() { Serial.println(“Trying to reconnect”); Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); }
void loop() { if (Firebase.failed()) { Serial.print(“setting number failed:”); Serial.println(Firebase.error()); firebasereconnect(); return; }
val1=Firebase.getString(“S1”).toInt(); //Reading the value of the varialble Status from the firebase
if(val1==1) // If, the Status is 1, turn on the Relay1 { digitalWrite(Relay1,LOW); Serial.println(“light 1 ON”); } else if(val1==0) // If, the Status is 0, turn Off the Relay1 { digitalWrite(Relay1,HIGH); Serial.println(“light 1 OFF”); }
val2=Firebase.getString(“S2”).toInt(); //Reading the value of the varialble Status from the firebase
if(val2==1) // If, the Status is 1, turn on the Relay2 { digitalWrite(Relay2,LOW); Serial.println(“light 2 ON”); } else if(val2==0) // If, the Status is 0, turn Off the Relay2 { digitalWrite(Relay2,HIGH); Serial.println(“light 2 OFF”); }
val3=Firebase.getString(“S3”).toInt(); //Reading the value of the varialble Status from the firebase
if(val3==1) // If, the Status is 1, turn on the Relay3 { digitalWrite(Relay3,LOW); Serial.println(“light 3 ON”); } else if(val3==0) // If, the Status is 0, turn Off the Relay3 { digitalWrite(Relay3,HIGH); Serial.println(“light 3 OFF”); }
val4=Firebase.getString(“S4”).toInt(); //Reading the value of the varialble Status from the firebase
if(val4==1) // If, the Status is 1, turn on the Relay4 { digitalWrite(Relay4,LOW); Serial.println(“light 4 ON”); } else if(val4==0) // If, the Status is 0, turn Off the Relay4 { digitalWrite(Relay4,HIGH); Serial.println(“light 4 OFF”); } } crd code: roboshala
Please help me! What should I do for “Auto Reconnecting to the FireBase”?