AsyncWebServer with other Port constantly generates restarts receiving the IP #110
Replies: 4 comments
-
I am closing the issue for these reasons:
Please read carefully when opening an issue ticket. For support issues, please go to discussion tab. |
Beta Was this translation helpful? Give feedback.
-
I don't understand some of the accusations. The code is from your own example. Only that I had to make small changes for a different port. Because of your initialization question, Line 164 // Start server
// server.begin(); // original
server = new AsyncWebServer(7777);
server -> begin(); I formulate the question differently. Why can't you change the port with the library WebSocket example? If you change from static AsyncWebServer server(80);
static AsyncWebSocket ws(“/ws”); makes static AsyncWebServer server(81);
static AsyncWebSocket ws(“/ws”); nothing works anymore. I call the IP with the addition : 81 in the browser. There is no reaction on the ESP when I switch the switch in the browser. If you search for a solution, you get the one shown above. However, the ESP then constantly resets itself. // works with port 80, but no other port
// Import required libraries
#include <credentials.h> // ssid, password
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
bool ledState = true;
const int ledPin = LED_BUILTIN;
// Create AsyncWebServer object on port 80
AsyncWebServer server(81);
AsyncWebSocket ws("/ws");
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>ESP32 WebSocket Server</title>
<style>
html{font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}
body{margin-top: 50px;}
h1{color: #444444;margin: 50px auto;}
p{font-size: 19px;color: #888;}
#state{font-weight: bold;color: #444;}
.switch{margin:25px auto;width:80px}
.toggle{display:none}
.toggle+label{display:block;position:relative;cursor:pointer;outline:0;user-select:none;padding:2px;width:80px;height:40px;background-color:#ddd;border-radius:40px}
.toggle+label:before,.toggle+label:after{display:block;position:absolute;top:1px;left:1px;bottom:1px;content:""}
.toggle+label:before{right:1px;background-color:#f1f1f1;border-radius:40px;transition:background .4s}
.toggle+label:after{width:40px;background-color:#fff;border-radius:20px;box-shadow:0 2px 5px rgba(0,0,0,.3);transition:margin .4s}
.toggle:checked+label:before{background-color:#4285f4}
.toggle:checked+label:after{margin-left:42px}
</style>
</head>
<body>
<h1>ESP32 WebSocket Server</h1>
<div class="switch">
<input id="toggle-btn" class="toggle" type="checkbox" %CHECK%>
<label for="toggle-btn"></label>
</div>
<p>On-board LED: <span id="state">%STATE%</span></p>
<script>
window.addEventListener('load', function() {
var websocket = new WebSocket(`ws://${window.location.hostname}/ws`);
websocket.onopen = function(event) {
console.log('Connection established');
}
websocket.onclose = function(event) {
console.log('Connection died');
}
websocket.onerror = function(error) {
console.log('error');
};
websocket.onmessage = function(event) {
if (event.data == "1") {
document.getElementById('state').innerHTML = "OFF";
document.getElementById('toggle-btn').checked = false;
}
else {
document.getElementById('state').innerHTML = "ON";
document.getElementById('toggle-btn').checked = true;
}
};
document.getElementById('toggle-btn').addEventListener('change', function() { websocket.send('toggle'); });
});
</script>
</body>
</html>
)rawliteral";
void handleWebSocketMessage(void *arg, uint8_t *data, size_t len) {
AwsFrameInfo *info = (AwsFrameInfo*)arg;
if (info->final && info->index == 0 && info->len == len && info->opcode == WS_TEXT) {
data[len] = 0;
if (strcmp((char*)data, "toggle") == 0) {
ledState = !ledState;
ws.textAll(String(ledState));
}
}
}
void eventHandler(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
switch (type) {
case WS_EVT_CONNECT:
Serial.printf("WebSocket client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str());
break;
case WS_EVT_DISCONNECT:
Serial.printf("WebSocket client #%u disconnected\n", client->id());
break;
case WS_EVT_DATA:
handleWebSocketMessage(arg, data, len);
digitalWrite(ledPin, ledState);
break;
case WS_EVT_PONG:
case WS_EVT_ERROR:
break;
}
}
String processor(const String& var){
if(var == "STATE"){
return ledState ? "OFF" : "ON";
}
if(var == "CHECK"){
return ledState ? "checked" : "";
}
return String();
}
void setup(){
// Serial port for debugging purposes
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
Serial.print("Connecting to ");
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
// Print ESP Local IP Address
Serial.println(WiFi.localIP());
ws.onEvent(eventHandler);
server.addHandler(&ws);
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/html", index_html, processor);
});
// Start server
server.begin();
}
void loop() {
ws.cleanupClients();
} |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Okay. Thanks for the hint for the JS var websocket modification. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Platform
ESP32 (XIAO ESP32S3)
IDE / Tooling
Arduino (IDE/CLI), Arduino IDE 2.3.4, Windows 11, Espressif Core 3.1.1 & C++20 (23)
What happened?
If you change the port for the Async WebServer, the ESP32 restarts immediately as soon as it has received the IP.
Stack Trace
Minimal Reproductible Example (MRE)
I confirm that:
Beta Was this translation helpful? Give feedback.
All reactions