-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Description
Platform
- Hardware: ESP8266
- Core Version: 2.7.4
- Development Env: Arduino IDE
- Operating System: Windows|Linux
Settings in IDE
- Module: Generic ESP8266 Module
- Flash Mode: DOUT
- Flash Size: 1MB
- lwip Variant: |v2 Lower Memory
- Reset Method: nodemcu
- Flash Frequency: 40Mhz
- CPU Frequency: 80Mhz
- Upload Using: SERIAL
- Upload Speed: 115200
Problem Description
Cannot stop ESP8266WebServer and reuse port for a new webserver.
This issue first came up with a user of FauxmoESP who was using WiFiManger to set up a captive portal to configure WiFi. The issue was that once the webserver on the portal stopped, FauxmoESP was not able to use port 80 on its own webserver. (See vintlabs/fauxmoESP#123 and tzapu/WiFiManager#1142)
Testing today, I attempted simply using an example sketch for ESP8266WebServer, and attempting to stop it and start a second one. (Sketch below)
In this test and in the other scenario, there are no errors, but the "new" webserver on port 80 simply is not receiving any data on port 80.
This appears to be related to #686
Is there a proper way to close the server that I haven't found?
Many thanks.
MCVE Sketch
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
const char* ssid = "SSID";
const char* password = "PASS";
ESP8266WebServer server(80);
ESP8266WebServer server2(80);
void handleRoot() {
server.send(200, "text/plain", "Response from server #1");
}
void handleRoot2() {
server2.send(200, "text/plain", "Response from server #2");
}
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);
WiFi.mode(WIFI_STA);
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.begin();
Serial.println("HTTP server started");
while (millis() < 10000)
{
server.handleClient();
}
server.stop();
server2.on("/", handleRoot2);
server2.begin();
Serial.println("HTTP SECOND server started");
while (true)
{
server2.handleClient();
}
}
void loop(void) {
}