0

问题:我无法使用来自 WAN 的端口转发访问我的 ESP32 的 http 服务器。

细节

  1. 我在我的 Mac 上设置了另一个 http 服务器(Python - m http.server),
    我可以从 LAN 和 WAN 访问它。所以,我认为端口转发设置没有问题。
  2. 我可以从 LAN 访问我的 ESP32 的 http 服务器。

我在下面向您展示 ESP32 中的代码。

#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESP32Servo.h>

const char* ssid = XXX
const char* password = XXXX
const IPAddress ip(X); 
const IPAddress gateway(X);  
const IPAddress subnet(X);
const IPAddress primaryDNS(X); //optional
const IPAddress secondaryDNS(X); //optional
WebServer server(8888);

//Servo
Servo servo;
int servoPin = 2;
int pos = 0;      // position in degrees

void handleRoot() {

  server.send(200, "text/plain", "hello from esp8266!");
   servo.write(90);
   delay(2000);             // waits 20ms for the servo to reach the position
   Serial.println("90");
}

void handleNotFound() {
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);

}

void setup(void) {

  Serial.begin(115200);
  if (!WiFi.config(ip,gateway,subnet, primaryDNS, secondaryDNS)){
       Serial.println("Failed to configure!");
  }
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  server.on("/", handleRoot);

  server.on("/inline", []() {
    server.send(200, "text/plain", "this works as well");
      if (servo.read() == 0){
        servo.write(90);
        delay(20000);
        servo.write(0);
        delay(2000);
        Serial.println("END");
      }
      else{
        servo.write(0);
        delay(2000);
        Serial.println("RETURN");
      }
  });

  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("HTTP server started");
  //Servo
  servo.setPeriodHertz(50);      // Standard 50hz servo
  servo.attach(servoPin);
}

void loop(void) {
  server.handleClient();
}

请帮我。

4

0 回答 0