Migrating from ESP8266 to ESP32 web server issues?

I'm trying to resurrect some code and want to migrate from an ESP8266 to an ESP32C3.

In the following routine:

void startWebServer()
{
    // 1. map default requests
    httpServer.onNotFound([]()  
    {   
        if (!handleFileRead(httpServer.uri())) // send file corresponding to URI if it exists in LittleFS
        httpServer.send(404, "text/plain", "404: Not Found"); // otherwise, respond with a 404 (Not Found) error
    });
    
    // 2. map specific requests (with remaping if needed)
    
    // Icon that appears in the tabs of the internet browser
    httpServer.on("/favicon.ico", [](){ handleFileRead("/carre_vert.png"); });

    //Simple mouse shortcut // http Server.on("/sou1", [](){ handle FileRead("/mouse 1.html"); });

    // List the LittleFS content    
    httpServer.on("/list", HTTP_GET, replyFileList2);
    //httpServer.on("/list", HTTP_GET, handleFileList); //try with action in html

    // Upload request
    // if the client posts to the upload page
    // Send status 200 (OK) to tell the client we are ready to receive
    // Receive and save the file
    httpServer.on("/upload.html", HTTP_POST, []() { httpServer.send(200, "text/plain", ""); }, handleFileUpload);
    
    // Delete request (not working) --> workaround: add .del to the file    
    // Edit request (on va pas faire) -->workaround: display the file, save it then load it
    httpServer.on("/edit.html", HTTP_DELETE, handleFileDelete);

    // Get heap status, analog input value and all GPIO statuses in one json call
    httpServer.on("/stat", HTTP_GET, []() 
    {WiFiServer server(80);
        String json = "{";
        json += "\"vers\":" + String(CENTRAL_NAME);
        json += "\"heap\":" + String(ESP.getFreeHeap());
        json += ", \"analog\":" + String(analogRead(A0));
        json += ", \"gpio\":" + String((uint32_t)(((0 | 0) & 0xFFFF) | ((1 & 0x01) << 16)));
        json += ", \"au\":" + String(au);
        json += "}";
        httpServer.send(200, "text/json", json);
        json = String();
    });
    
    // ex with a string in the program (just for testing)
    httpServer.on("/ii", HTTP_GET, []()    // internal index
    {
        httpServer.send(200, "text/html", index_html);  //send the string defined in this file
    });
    
    // Direct AU
    httpServer.on("/au0", HTTP_GET, [](){ au = 0; httpServer.send(200, "text/html", ok_au0);});  //we return not too long so as not to disturb the dcc and not to block the browser
    httpServer.on("/au1", HTTP_GET, [](){ au = 1; httpServer.send(200, "text/html", ok_au1);});

    httpServer.begin(); // start the server
    #if USB_MODE == 1
    DBG.println("HTTP server started.");
    #endif
}

I get compiler errors stating

/Users/etimberl/Documents/Hobbies/Micro Controllers/Arduino/trains/WILDMaterials/WILD_Arduino_Load/D17_32_TinyPico_nospiffs/D17_32_TinyPico_nospiffs.ino: In function 'void no_html_in_pwr()':
D17_32_TinyPico_nospiffs:3495:16: error: 'class WiFiServer' has no member named 'send'; did you mean 'end'?
     httpServer.send(200, "text/html", ask_go_au);
                ^~~~
                end
                                                        ^~~~
                                                              end
/Users/etimberl/Documents/Hobbies/Micro Controllers/Arduino/trains/WILDMaterials/WILD_Arduino_Load/D17_32_TinyPico_nospiffs/D17_32_TinyPico_nospiffs.ino: In function 'void startWebServer()':
D17_32_TinyPico_nospiffs:3763:16: error: 'class WiFiServer' has no member named 'on'
     httpServer.on("/au1", HTTP_GET, [](){ au = 1; httpServer.send(200, "text/html", ok_au1);});
                ^~
/Users/etimberl/Documents/Hobbies/Micro Controllers/Arduino/trains/WILDMaterials/WILD_Arduino_Load/D17_32_TinyPico_nospiffs/D17_32_TinyPico_nospiffs.ino: In lambda function:
D17_32_TinyPico_nospiffs:3763:62: error: 'class WiFiServer' has no member named 'send'; did you mean 'end'?
     httpServer.on("/au1", HTTP_GET, [](){ au = 1; httpServer.send(200, "text/html", ok_au1);});
                                                              ^~~~
                                                              end
/Users/etimberl/Documents/Hobbies/Micro Controllers/Arduino/trains/WILDMaterials/WILD_Arduino_Load/D17_32_TinyPico_nospiffs/D17_32_TinyPico_nospiffs.ino: In function 'void http_loop()':
D17_32_TinyPico_nospiffs:3784:16: error: 'class WiFiServer' has no member named 'handleClient'; did you mean 'hasClient'?
     httpServer.handleClient();
                ^~~~~~~~~~~~
                hasClient
Multiple libraries were found for "WiFi.h"
 Used: /Users/etimberl/Library/Arduino15/packages/esp32/hardware/esp32/2.0.14/libraries/WiFi
 Not used: /Applications/Arduino.app/Contents/Java/libraries/WiFi
 Not used: /Users/etimberl/Documents/Hobbies/Micro Controllers/Arduino/libraries/WiFiEspAT
exit status 1
'class WiFiServer' has no member named 'send'; did you mean 'end'?


This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I replaced the 8266 web library as follows:

//WebServer httpServer(80); // Create a webserver object that listens for HTTP request on port 80
WiFiServer httpServer(80); // Create a webserver object that listens for HTTP request on port 80
WebSocketsServer webSocketServer = WebSocketsServer(81); // Create a webSocketServer object that listens for request on port 81

Should I have found another ESP32 specific library, perhaps?

You don't show all you code (i.e the #include statements) so a bit difficult to tell which library you have actually used, but I am guessing that on the ESP8266 your code probably used:

#include <ESP8266WebServer.h>
ESP8266WebServer server(80);

WebServer actually makes use of and is a wrapper for calls to WiFiServer and the equivalent on the ESP32 is just changing the above to:

#include <WebServer.h>
WebServer server(80);

ESP32 also has an async version which has more functionality and makes use of multi-tasking and multi-processing:

#include "ESPAsyncWebServer.h"`
AsyncWebServer server(80);

As an aside, if you were thinking about going HTTPS, then it gets a bit more complicated because there is no equivalent of ESP8266SecureWebServer. There is an alternative library, but it is not code compatible.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.