0

Hello i am new to NodeMCU (ESP8266) i am building a small program to connect with WIFI (with internet) and if internet is not available i want to process request over IP address.

I am using firebase as a database / server to fetch the status. And process command through a URL like http://192.168.1.223/on?pin=04 if internet is connected then no problem i can update firebase entry, but with same network (LAN) connection over wifi i want to process i need to have static IP. But the concern is i am not able to make this IP address static, and if i make it static then my NodeMCU is not able to connect with firebase.

Here is how my code looks like:

#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
#include <ESP8266WebServer.h>

#define FIREBASE_HOST "pushst-56f2c.firebaseio.com"
#define FIREBASE_AUTH "mhBpzrNyhhwEGwmFxkVFTIEylwrXMw0gm"
#define PATH "/clients/devicename/watermotor"

IPAddress ip(192, 168, 1, 223);  //Node static IP
IPAddress dns(192, 168, 1, 223);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);

char ssid[] = "MyNetwork";   
char password[] = "Qwert98!!"; 

int wifiStatus = WL_IDLE_STATUS;

ESP8266WebServer server(80);

void setup() { 
      Serial.begin(115200); 

      delay(100);
      Serial.println();
      Serial.println();
      Serial.print("Your are connecting to;");
      Serial.println(ssid);

      WiFi.begin(ssid, password);
      WiFi.config(ip, dns, gateway, subnet);


      while (WiFi.status() != WL_CONNECTED) {
        digitalWrite(WIFI_CONNECT_LED, HIGH);
        delay(500);
        Serial.print(".");
      }

      wifiStatus = WiFi.status();

      if(wifiStatus == WL_CONNECTED){
          digitalWrite(WIFI_CONNECT_LED, LOW);

         Serial.println("Your IP address is: ");
         Serial.println(WiFi.localIP());  

         Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
         Firebase.set("/clients/devicename/watermotor/name", "Motor");
         Firebase.set("/clients/devicename/watermotor/relay_status", "off");
         Firebase.set("/clients/devicename/watermotor/device_status", "online");
         Firebase.stream(PATH);

         server.on("/on", handleOnRequest); //Associate the handler function
         server.on("/off", handleOffRequest); //Associate the handler function

         server.begin(); //Start the server
      }
      else{
        Serial.println("");
        Serial.println("WiFi not connected");
      }

}


void loop() { 
    server.handleClient();
    if (Firebase.failed()) {
      Serial.println("streaming error");
      Serial.println(Firebase.error());
      delay(1000);
      return;
    }

    if (Firebase.available()) {
        Serial.println();
        Serial.print("Firebase available");
        Serial.println();
        FirebaseObject event = Firebase.readEvent();
    }
}

I am not sure if its there is some problem with FirebaseLibrary or its just the ESP8266!

Any suggestions will be helpful, thanks in advance! :)

  • 1
    why same IP for esp8266 and DNS? – Juraj Feb 22 '18 at 14:44
  • Its just for reference, i can keep any @Juraj – user3201500 Feb 22 '18 at 15:53
  • it should be the address of the DNS server which can resolve the name of the firebase host – Juraj Feb 22 '18 at 16:49
  • Not getting you @Juraj any example? – user3201500 Feb 22 '18 at 18:01
  • example? just fill in the IP address of the DNS server which is usually the address of the router/gateway IPAddress dns(192,168,1,1); (if this is right) – Juraj Feb 22 '18 at 18:30
0

You have wrong DNS server IP address. In your sketch it is the same as the static IP address you assign to esp. Then the name of the firebase host can't be resolved by DNS.

The DNS server is usually the router/gateway.

IPAddress dns(192,168,1,1)
0

I also placed the same problem(If I set the static IP means it's not getting internet)But Then I added the DNS "IPAddress dns(192,168,1,1)" for that also it didn't connect to the Internet. But I connected to the Internet after adding the "IPAddress dns(8,8,8,8)".

This is my Code

#include <ESP8266WiFi.h>
const char* ssid = "Varaprasad";
const char* password = "varara";
int red = D6; int green = D7; int blue = D8;
const char* host = "18.221.215.201"; 
IPAddress staticIP(192,168,1,78);
IPAddress gateway(192,168,1,254);
IPAddress dns(8, 8, 8, 8);
IPAddress subnet(255,255,255,0);
void setup()

{
 pinMode(red,OUTPUT); pinMode(green,OUTPUT); pinMode(blue,OUTPUT);
Serial.begin(9600);
Serial.println();
Serial.printf("Connecting to %s\n", ssid);
WiFi.begin(ssid, password);
WiFi.config(staticIP,dns, gateway, subnet);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
  if(WiFi.status() == WL_CONNECTED){
  WiFiClient client1;
    if (client1.connect(host,80))
      {
         client1.stop();
      analogWrite(red,0); analogWrite(green,1023); analogWrite(blue,0);
      }
      else{
      analogWrite(red,1023); analogWrite(green,0); analogWrite(blue,0);
      client1.stop();
      }
    }
  }

Not the answer you're looking for? Browse other questions tagged or ask your own question.