ESP32 AsyncWebServer Port

439 views Asked by At

Quick question... Can the ESP32 AsyncWebServer port be set/reset AFTER the server has been declared?

The server is declared after the global variables, but before any functions, with the line: AsyncWebServer server(PortNumber);

Usually, the port number is 80 (the default HTTP port). However, I have the need to use a different port and the port will be chosen by the user from a WiFi credentials page and then stored in NVS.

So, I need to retrieve the port number from the NVS before I execute the AsyncWebServer command, or be able to change the port number on the fly.

How can I achieve this?

2

There are 2 answers

0
Nugget On BEST ANSWER

The answer to this is here: Answer

3
Tarmo On

Looking at the ESPAsyncWebserver declaration there doesn't seem to be a way to change or set the port after creating an instance of it. But isn't the solution to your problem as simple as creating the server later, after your configuration settings have been read? Something to this tune:

uint16_t port = config.read("server_port");
ESPAsyncWebserver* server = new ESPAsyncWebserver(port);