2

I made this minimal example in order to figure out how to get requests (or whatever they are called) to display in the serial monitor. However, none of the query parameters (in the URL) can be seen in the serial monitor at 115200 baud.

The files are below and here: https://github.com/adamelli/MinimalExamples/tree/main/22-1-25_query_parameters_submitButton

File structure

sketch (folder)

  • sketch.ino
  • data (folder)
    • index.html
    • selection.html

sketch.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);

void notFound(AsyncWebServerRequest *request) { request->send(404, "text/plain", "Not found"); } IPAddress IP(192, 168, 1, 1); IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 255, 0);

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);

// Route for root / web page server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) { request->send(SPIFFS, "/index.html", String(), false ); });

// Index.HTML NAVIGATION BUTTIONS ************************************************

server.on("/index.html", HTTP_GET, [](AsyncWebServerRequest * request) { request->send(SPIFFS, "/index.html", String(), false); });

server.on("/selection.html", HTTP_GET, [](AsyncWebServerRequest * request) { request->send(SPIFFS, "/selection.html", String(), false ); });

server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {

int paramsNr = request-&gt;params();
Serial.println(paramsNr);

for (int i = 0; i &lt; paramsNr; i++)
{
  AsyncWebParameter* p = request-&gt;getParam(i);
  Serial.print(&quot;Param name: &quot;);
  Serial.println(p-&gt;name());

  Serial.print(&quot;Param value: &quot;);
  Serial.println(p-&gt;value());

  Serial.println(&quot;------&quot;);
}

request-&gt;send(200, &quot;text/plain&quot;, &quot;message received&quot;);

});

server.begin(); }

void loop() {}

index.html

<!DOCTYPE html>
<html>
<body>

<h1>Main Menu</h1>

<p><a href="/selection.html"><button class="button">Selection</button></a></p>

<p><a href="/notMinimal.html"><button class="button">Oblivion</button></a></p>

</body> </html>

selection.html

<!DOCTYPE html>
<html>
<body> 
<!-- -->
<form action="/" method="get">
&lt;label for=&quot;size&quot; style=&quot;font-size: 22px&quot;&gt;Shape Size:&lt;/label&gt;

&lt;select name=&quot;size&quot; id=&quot;size&quot; style=&quot;font-size: 22px;&quot;&gt;
  &lt;option value=&quot;1&quot;&gt; 1   &lt;/option&gt;
  &lt;option value=&quot;2&quot;&gt; 1/2 &lt;/option&gt; 
    &lt;/select&gt;


&lt;p&gt;&lt;label for=&quot;speed&quot; style=&quot;font-size: 22px&quot;&gt;Speed:&lt;/label&gt;

&lt;select name=&quot;speed&quot; id=&quot;speed&quot; style=&quot;font-size: 24px&quot;&gt;
  &lt;option value=&quot;1&quot;&gt;          Slowest &lt;/option&gt;
  &lt;option value=&quot;2&quot; selected&gt; Regular &lt;/option&gt;
&lt;/select&gt;&lt;/p&gt;




<p><a href="/selection"><button class="submit" name="circle" value="3">Circle</button></a></p>

<p><a href="/selection"><button class="submit" name="square" value="4">Square</button></a></p>

</form>

</body> </html>

The data files must be uploaded via "Tools" → "ESP32 Sketch Data Upload".

How is the data (size and speed) viewable in the serial monitor? What needs to change?

Adam
  • 47
  • 2
  • 9

1 Answers1

2

You have two handlers for "/":

  server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
    request->send(SPIFFS, "/index.html", String(), false );
  });

and

server.on("/", HTTP_GET, [](AsyncWebServerRequest * request)
  {
int paramsNr = request-&gt;params();
Serial.println(paramsNr);

for (int i = 0; i &lt; paramsNr; i++)
{

...

Only one will be called when you load the top level page. You'll need to combine them to both serve index.html and process the parameters being passed to it.

romkey
  • 1,573
  • 10
  • 10