NodeMCU(ESP8266)定义静态IP,停止Firebase处理

时间:2018-02-22 13:27:35

标签: firebase firebase-realtime-database esp8266 nodemcu arduino-esp8266

您好我是NodeMCU的新手(ESP8266)我正在建立一个小程序来连接WIFI(与互联网),如果互联网不可用,我想通过IP地址处理请求。

我使用firebase作为数据库/服务器来获取状态。如果互联网已连接,则通过像http://192.168.1.223/on?pin=04这样的URL处理命令然后没问题我可以更新firebase条目,但是通过wifi使用相同的网络(LAN)连接我想要处理我需要有静态IP。但问题是我无法使这个IP地址静态,如果我使它静态,那么我的NodeMCU无法连接firebase。

以下是我的代码的样子:

#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();
    }
}

我不确定FirebaseLibrary或ESP8266是否存在问题!

任何建议都会有所帮助,在此先感谢! :)

1 个答案:

答案 0 :(得分:0)

您的DNS服务器IP地址错误。在草图中,它与您分配给esp的静态IP地址相同。然后,无法通过DNS解析firebase主机的名称。

DNS服务器通常是路由器/网关。

IPAddress dns(192,168,1,1)