Hi Savalio, good to see you here! Thank you for coming here from Reddit, I thought that your question is an interesting topic and I know some ChipChop users would like to follow this and learn from it.
Ok, so I've made a very basic server code for you. You definitely don't need the AsyncWebServer, it's complete bullshit as the WebServer.h has everything that you need and once you understand the basic syntax it's a powerful tool and it takes a lot less space on your chip.
Here is the code that when you connect to your ESP's WiFi hotspot and then go to
http://192.168.0.1/ in the phone browser it will bring a simple form and when you submit it will print out in the Serial Monitor what you entered in the fields so you can do whatever logic you want with it.
#ifdef ESP32
#include <WiFi.h>
#include <WebServer.h>
WebServer server(80);
#else
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
ESP8266WebServer server(80);
#endif
//you can pre-set the IP address of your hotspot here
//so you can later access your web pages like: http://192.168.0.1/test << you don't use .htm just the route name (like a folder) http://xxx.xxx.xxx.xxx/route_name
IPAddress apIP(192, 168, 0, 1);
IPAddress netMsk(255, 255, 255, 0);
String softAP_ssid = "My ESP hotspot"; //wifi hotspot name
String softAP_password = ""; // password if you need one
/**
* once you connect to the hotspot this is the function that will return a html page to the web browser
* if you want to show other pages simply duplicate this and change the html string you want to send
* don't forget to also declare it as a "route" in the start_hotspot() below
*/
void showRoot(){
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1");
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html><head>
<title>ESP Input Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head><body>
<form action="/form1">
input1: <input type="text" name="input1"><br>
input2: <input type="text" name="input2"><br>
input3: <input type="text" name="input3"><br>
<input type="submit" value="Submit">
</form>
</body></html>)rawliteral";
server.send(200, "text/html", index_html);
server.client().stop(); // Stop is needed because we sent no content length so it's an easy solution to send whatever size string you want
}
/** this is where you intercept what you submit through the html form
* you can make more of thse for each page that you want to receive form input like handleForm2(), processForm3() etc...
*/
void handleForm1() {
String input1 = server.arg("input1");
String input2 = server.arg("input2");
String input3 = server.arg("input3");
//do some logic with the form field values
Serial.println(input1);
Serial.println(input2);
Serial.println(input3);
//you can show a different page when the from is submitted or simply return the same "root" page
//as the browser will go automatically to "/form1" so you need to send it something back
//this redirects it back to root
server.sendHeader("Location", "/",true);
server.send(302, "text/plain", "");
//or send a different page
//showForm1ResponsePage() or something like that
}
void start_hotspot(){
WiFi.mode(WIFI_AP_STA); //set the mode to station and softAP
WiFi.softAPConfig(apIP, apIP, netMsk);
WiFi.softAP(softAP_ssid, softAP_password);
// this is where you can define "routes" functions that will be called to display different pages
// so for example http://xxx.xxx.xxx.xxx/settings would call a function showSettings() where you can return some settings html page
server.on("/", [](){ showRoot();}); //this is the "root" page that will be called when you just type the ip address
//anything else you want, you just need to do like this and declare the functions same as showRoot() above
// server.on("/settings", [](){ showSettings(); });
//this is where you define the functions that will be called on form submit
// the "route" here "/form1" is what you put in the html like <form action="/form1">
server.on("/form1", [](){ handleForm1();});
server.begin(); // start the Web server
}
void setup(){
Serial.begin(115200);
start_hotspot();
}
void loop(){
// keep the server listening
server.handleClient();
}
I have included lots of comments to help you understand how it works so you can customise it for your needs.
There is of course a more advanced option using LittleFS to store proper HTML files in the flash with combination of Ajax instead of GET & POST and also using a dns server so it can bring the pages straight away after yo connect to the hotspot (WiFi Captive Portal). If you want to explore that at spome point just let me know and I will be happy to show you.
Let me know how it goes