ESP8266:将client.remoteIP()发送到客户端

时间:2017-02-20 23:00:12

标签: webserver esp8266

`ESP8266 Web服务器不会将client.remoteIP()发送到客户端浏览器。

void loop() {
  // Listenning for new clients
  WiFiClient client = server.available();

  if (client) {

    Serial.println("New client");
    // bolean to locate when the http request ends
    boolean blank_line = true;
    while (client.connected()) {


    if (client.available()) {
        char c = client.read(); 

        if (c == '\n' && blank_line) {
            getWeather();
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println("Connection: close");
            client.println();
            // your actual web page that displays temperature
            client.println("<!DOCTYPE HTML>");
            client.println("<html>");
            //client.println("<head><META HTTP-EQUIV=\"refresh\" CONTENT=\"15\"></head>");
            client.println("<body><h1>ESP8266 Weather Web Server</h1>");
            client.println("<table border=\"2\" width=\"456\" cellpadding=\"10\"><tbody><tr><td>");
            client.println();
            client.println(client.remoteIP());
            client.println();
            client.println("<h3>Temperature = ");
            client.println(temperatureFString);
            client.println("&deg;F</h3><h3>Humidity = ");
            client.println(humidityString);
            client.println("%</h3><h3>Approx. Dew Point = ");
            client.println(dpString);
            client.println("&deg;F</h3><h3>Pressure = ");
            client.println(pressureString);
            client.println("hPa (");
            client.println(pressureInchString);
            client.println("Inch)</h3></td></tr></tbody></table></body></html>");  


            int x;
            for(x = 1;x < 2; x++)
            {
                Serial.println(client.remoteIP());
            }
            break;
        }   
        if (c == '\n') {
          // when starts reading a new line
          blank_line = true;
        }
        else if (c != '\r') {
          // when finds a character on the current line
          blank_line = false;
        }
      }
    }  
    // closing the client connection
    delay(1);
    client.stop();
    Serial.println("Client disconnected.");
  }

是否有一种方法可以在浏览器表格中显示客户端的IP? 我在使用Serial.print(client.remoteIP())时没有问题。

1 个答案:

答案 0 :(得分:0)

IPAddress是一个类,不返回String。最好以这种方式使用你的情况:

client.remoteIP().toString().c_str()