Hi
I have a test web page running on an ESP32. On this page I have a button to turn an led on and off, report the status of the GPIO the led is is connected t, a form consisting of 1 input field (input1) and a submit button. These are working as expected and the Serial Monitor reflects what was submitted to the input1 field.
Below this I have four drop down boxes (day, hour, min, zone) to select an alarm time. When I click the 'Select Alarm Time' button I have a jsquery that runs that updates the webpage with the chosen values.
What I would like to do also when the 'Select Alarm Time' button is clicked is have these values return to my running program appear in the Serial monitor and be used for other routines I will be writing. processing.
I am at a loss as to how to get these values to be passed back be stored into four String variables on the ESP32 called;
dayselected;
hourselected;
minutesselected;
zoneselected;
If anyone can help showing me how to extend the jsquery that runs on the web page and what I need to put into my sketch to get these values returned. I would be most grateful. I have many things but nothing seems to work. The webpage is on SPIFFS as is the jsquery.min.js file. they are attached. My sketch is below.
Thank you for taking the time
Chazza
// include Spiffs
#include <SPIFFS.h>
// Wifi
#include <WiFi.h> // for WiFi shield
#include <WiFiUdp.h>
// web server
#include <ESPAsyncWebServer.h>
// Create AsyncWebServer
int webport = 80; //the port the webserver is listening on
AsyncWebServer server(webport);
const int led = 2; // onboard led
// variables to store data in from web page
// input box using simple form
const char* PARAM_INPUT_1 = "input1";
// four variables that need to be updated when 'Select Alarm Time Button' is clicked on webpage
String dayselected;
String hourselected;
String minutesselected;
String zoneselected;
// Stores LED state
String ledState;
// Replaces placeholder with state value
String processor(const String& var) {
// Serial.print(var);
if (var == "STATE") {
if (digitalRead(led)) {
ledState = " ON";
}
else {
ledState = " OFF";
}
return ledState;
}
return String();
}
void setup() {
Serial.begin(115200);
pinMode(led, OUTPUT);
// Initialize SPIFFS
if (!SPIFFS.begin(true)) {
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
WiFi.begin("ssid", "password");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.println(WiFi.localIP());
// load web routes
webroutes();
// Start web server
server.begin();
Serial.println("Setup Finished");
}
void webroutes() {
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/index.html", String(), false, processor);
});
// Route to set GPIO to HIGH
server.on("/on", HTTP_GET, [](AsyncWebServerRequest * request) {
digitalWrite(led, HIGH);
request->send(SPIFFS, "/index.html", String(), false, processor);
});
// Route to set GPIO to LOW
server.on("/off", HTTP_GET, [](AsyncWebServerRequest * request) {
digitalWrite(led, LOW);
request->send(SPIFFS, "/index.html", String(), false, processor);
});
// Send a GET request to <ESP_IP>/get?input1=<inputMessage>
server.on("/get", HTTP_GET, [](AsyncWebServerRequest * request) {
String inputMessage;
String inputParam;
// GET input1 value on <ESP_IP>/get?input1=<inputMessage>
if (request->hasParam(PARAM_INPUT_1)) {
inputMessage = request->getParam(PARAM_INPUT_1)->value();
inputParam = PARAM_INPUT_1;
inputMessage = request->getParam(PARAM_INPUT_1)->value();
} else {
inputMessage = "No message sent";
inputParam = "none";
}
Serial.print("Value received input1 field : "); Serial.println(inputMessage);
});
server.on("/jquery.min.js", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/jquery.min.js", "text/javascript");
});
} // end of void webroutes()
void loop() {
}