0

源自以下代码

我将该代码上传到我的 ESP8266,当我的笔记本电脑通过 LAN 电缆连接到我的网络时,当我与笔记本电脑通信时,一切都很好。

问题是:当我尝试用我的笔记本电脑或手机通过 Wi-Fi 与 ESP 通信时,我发现ERR_CONNECTION_REFUSED它们很少工作和通信。我尝试了另一部手机另一个路由器,并对我的路由器进行了出厂重置,一切都一样。

我知道路由器中有一个名为 AP Isolation 的选项,它已被检查并被禁用。

ERR_CONNECTION_REFUSED我的问题是:当我使用该代码与 ESP8266 通信时,此错误的原因可能是什么?

如果有人可以帮助我,我会很高兴,因为我陷入了这种情况。

ESP 代码(与链接相同):

#include <ESP8266WiFi.h>

const char* ssid = "*****";
const char* password = "*******";

WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.printf("Connecting to %s ", ssid);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" connected");

  server.begin();
  Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str());
}

// prepare a web page to be send to a client (web browser)
// the connection will be closed after completion of the response
// the page will be refreshed automatically every 5 sec
String prepareHtmlPage() {
  String htmlPage = String("HTTP/1.1 200 OK\r\n") +
                    "Content-Type: text/html\r\n" +
                    "Connection: close\r\n" +
                    "Refresh: 5\r\n" + "\r\n" +
                    "<!DOCTYPE HTML>" + "<html>" +
                    "Analog input:  " + String(analogRead(A0)) +
                    "</html>" + "\r\n";
  return htmlPage;
}


void loop() {
  WiFiClient client = server.available();

  // wait for a client (web browser) to connect 
  if (client) {
    Serial.println("\n[Client connected]");
    while (client.connected()) {
      // read line by line what the client (web browser) is requesting
      if (client.available()) {
        String line = client.readStringUntil('\r');
        Serial.print(line);
        // wait for end of client's request, that is marked with an empty line
        if (line.length() == 1 && line[0] == '\n') {
          client.println(prepareHtmlPage());
          break;
        }
      }
    }
    delay(1); // give the web browser time to receive the data

    // close the connection:
    client.stop();
    Serial.println("[Client disconnected]");
  }
}
4

0 回答 0