问题: 我无法使用来自 WAN 的端口转发访问 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();
}
请帮帮我。