Hey,
I've got this asynchronous webserver started, combined with Wifi manager. I'm using an eample html-page which serves a form with 2 fields. At the moment, when you press the submit button, a popup window is opened and display the input of the form field.
I'd like to use the input of the form field in the loop. So I can have a led switched on or of, or do some caluculations. But how do I do that?
I think I'll need some javascript to let the submit button post the input. And then I think I need some code to grab that input som I can use it in the loop.
Been looking around but haven't fond much at the moment.... I hoping someone can help me out!
Here you will find the sketch-code and the html code.
sketch:
#if defined(ESP8266)
#include "ESP8266WiFi.h" //https://github.com/esp8266/Arduino
#else
#include "WiFi.h"
#endif
#include "ESPAsyncTCP.h"
#include "ESPAsyncWebServer.h"
#include "FS.h"
#include "ESPAsyncWiFiManager.h" //https://github.com/tzapu/WiFiManager
#include "NTPtimeESP.h"
#define DEBUG_ON
AsyncWebServer server(80);
DNSServer dns;
void setup(){
// Start serial
Serial.begin(115200);
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
AsyncWiFiManager wifiManager(&server,&dns);
//reset saved settings
//wifiManager.resetSettings();
//set custom ip for portal
//wifiManager.setAPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
//fetches ssid and pass from eeprom and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
wifiManager.autoConnect("AutoConnectAP");
//or use this for auto generated name ESP + ChipID
//wifiManager.autoConnect();
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
// Connecting to a WiFi network
// Serial.println();
//Serial.println();
// Serial.print("Connecting to ");
SPIFFS.begin();
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/index.html", String());
});
server.on("/rand", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/plain", String(random(1000)));
});
server.begin();
}
void loop() {
// put your main code here, to run repeatedly:
}
HTML-page:
<script language = "JavaScript">
function formToJson(form){
var pass = form.pass.value;
var ssid = form.ssid.value;
var jsonFormInfo = JSON.stringify({
pass:pass,
ssid: ssid
});
window.alert(jsonFormInfo);
}
</script>
<form onSubmit = "event.preventDefault(); formToJson(this);">
<label class="label">Network Name</label>
<input type = "text" name = "ssid"/>
<label>Password</label>
<input type = "text" name = "pass"/>
<input type="submit" value="Submit">
</form>
Kind regards,
Christophe