I just want to have any program work where there are a few buttons, a user selects one, and a command is sent to the ESP32. The GUI should stay the same (not change into a submenu).
.ino
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <SPIFFS.h>
#include <painlessMesh.h>
const char* ssid = "Wireless Controller";
const char* password = "12345678";
// User stub
void sendMessage() ; // Prototype so PlatformIO doesn't complain
Task taskSendMessage( TASK_SECOND * 1 , TASK_FOREVER, &sendMessage );
void sendMessage() {
taskSendMessage.setInterval( random( TASK_SECOND * 1, TASK_SECOND * 5 ));
}
AsyncWebServer server(80);
IPAddress IP(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
void setup()
{
Serial.begin(115200);
// Initialize SPIFFS
if (!SPIFFS.begin(true))
{
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
WiFi.softAP(ssid, password);
delay(500);
WiFi.softAPConfig(IP, gateway, subnet);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request)
{
request->send(SPIFFS, "/index.html", String(), false);
});
server.on("/post", HTTP_POST, [](AsyncWebServerRequest * request)
{
int paramsNr = request->params(); // number of params (e.g., 1)
Serial.println(paramsNr);
Serial.println();
AsyncWebParameter * j = request->getParam(0); // 1st parameter
Serial.print("Size: ");
Serial.print(j->value()); // value ^
Serial.println();
request->send(200);
});
server.onNotFound(notFound);
server.begin();
}
void loop() {}
HTML
<!DOCTYPE html>
<html>
<body>
<h1>Menu 1</h1>
<form action="/post" method="post" target="hidden-form">
<p><button class="submit" name="circle" value="6">Circle</button></p>
<p><button class="submit" name="square" value="4">Square</button></p>
</form>
</body>
</html>
The tutorials and examples I've seen (maybe I missed one), do not have a GUI (web-page). I'm not sure how to get the POST method to work. A similar program half-works with the GET method. I think to successfully accomplish the simple minimal function, GET and POST are needed.
Please try this on your own or I do not want your help. I've gotten a bunch of advice on what to do that didn't help. It's only two files and maybe three libraries. Maybe there needs to be an href
involved?
File structure
sketch (folder)
- sketch.ino
- data (folder)
- index.html