#include <Preferences.h> // this library is used to get access to Non-volatile storage (NVS) of ESP32// see https://github.com/espressif/arduino-esp32/blob/master/libraries/Preferences/examples/StartCounter/StartCounter.ino

// Call this function regularly to look for client requests// template see https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/examples/SimpleWiFiServer/SimpleWiFiServer.ino// returns empty string if no request from any client// returns GET Parameter: everything after the "/?" if ADDRESS/?xxxx was entered by the user in the webbrowser// returns "-" if ADDRESS but no GET Parameter was entered by the user in the webbrowser// remark: client connection stays open after returnString Webserver_GetRequestGETParameter(){ String GETParameter = "";

myclient = server.available(); // listen for incoming clients

//Serial.print(".");

if (myclient) { // if you get a client, Serial.println("New Client."); // print a message out the serial port String currentLine = ""; // make a String to hold incoming data from the client

while (myclient.connected()) { // loop while the client's connected

if (myclient.available()) { // if there's bytes to read from the client,

// Initialize Soft Access Point with ESP32// ESP32 establishes its own WiFi network, one can choose the SSIDint WiFi_AccessPointStart(char* AccessPointNetworkSSID){ WiFi.softAP(AccessPointNetworkSSID);

// printout the ESP32 IP-Address in own network, per default it is "192.168.4.1". Serial.println(WiFi.softAPIP());

return 1;}

// Put ESP32 in both modes in parallel: Soft Access Point and station in router network (LAN)void WiFi_SetBothModesNetworkStationAndAccessPoint(){ WiFi.mode(WIFI_AP_STA);}

// Decodes a GET parameter (expression after ? in URI (URI = expression entered in address field of webbrowser)), like "Country=Germany&City=Aachen"// and set the ConfigValuesint DecodeGETParameterAndSetConfigValues(String GETParameter){

int posFirstCharToSearch = 1; int count = 0;

// while a "&" is in the expression, after a start position to search while (GETParameter.indexOf('&', posFirstCharToSearch) > -1) { int posOfSeparatorChar = GETParameter.indexOf('&', posFirstCharToSearch); // position of & after start position int posOfValueChar = GETParameter.indexOf('=', posFirstCharToSearch); // position of = after start position

ConfigValue[count] = GETParameter.substring(posOfValueChar + 1, posOfSeparatorChar); // extract everything between = and & and enter it in the ConfigValue

// start the webserver to listen for request of clients (in LAN or own ESP32 network) Webserver_Start();

}

// check the ConfigValues and set ConfigStatus// process the first ConfigValue to switch somethingvoid ProcessAndValidateConfigValues(int countValues){ if (countValues > 8) {countValues = 8;};

// if we have more than 1 value, store the second and third value in non-volatile storage if (countValues > 2) { preferences.putString("SSID", ConfigValue[1]); preferences.putString("Password", ConfigValue[2]); ///////////////ser print////////////// Serial.println(ConfigValue[1]); Serial.println(ConfigValue[2]); Serial1.println(ConfigValue[1]); Serial1.println(ConfigValue[2]); Serial2.println(ConfigValue[1]); Serial2.println(ConfigValue[2]);

} else // the user entered the ESP32 address in router network in his browser, with a GET parameter to switch the LED { ProcessAndValidateConfigValues(1); // then only the LED must be switched

} }

String HTMLPageWithConfigForm;

if (WebRequestHostAddress == "192.168.4.1") // the user entered this address in the browser, to get the configuration webpage { HTMLPageWithConfigForm = EncodeFormHTMLFromConfigValues("ESP32 Webserver CONFIG", 3) + "<br>IP Address: " + WiFi_GetOwnIPAddressInRouterNetwork(); // build a new Configuration webpage with form and new ConfigValues entered in textboxes } else // the user entered the ESP32 address in router network in the browser, to get normal webpage { HTMLPageWithConfigForm = EncodeFormHTMLFromConfigValues("ESP32 Webserver Demo", 1); // build a new webpage to control the LED }

Webserver_SendHTMLPage(HTMLPageWithConfigForm); // send out the webpage to client = webbrowser and close client connection