How to Parse out HTML Data in c++ for ESP32
Posted: Fri Mar 06, 2020 12:55 am
I have code in order to create an Access Point for my ESP32 in order to submit data to it and overwrite the configuration file. Similar to how when you connect a new printer, the user has to have a way to access and give it configuration file SSID and Password. Basically the AP will load a Captive Portal and ask the user to submit an SSID and Password, and it works. My problem is figuring out how to get the actual input to parse out from the text in front and behind it, and save it into it's own variable.
Where I believe the problem can be solved:
The output is attached below, with the prefix of input1 and input2 (boxed in image). I want to parse out client inputs for SSID & Password (underlined in image).
Thank you
Where I believe the problem can be solved:
- if (header.indexOf("/get?input1=") >= 0) {
- Serial.println(PARAM_INPUT_1);
- }
- if (header.indexOf("&input2=") >= 0) {
- Serial.println(PARAM_INPUT_2);
- }
- #include <Arduino.h>
- #include <WiFi.h>
- #include <DNSServer.h>
- // Replace with your network credentials
- const char* ssid = "dictatorTippens";
- const char* password = "teamomega";
- int max_connection = 4;
- const byte DNS_PORT = 53;
- const char* PARAM_INPUT_1 = "input1"; // copied over
- const char* PARAM_INPUT_2 = "input2"; // copied over
- const char* PARAM_INPUT_3 = "input3"; // copied over
- DNSServer dnsServer;
- // Set web server port number to 80
- WiFiServer server(80);
- // Variable to store the HTTP request
- String header;
- String saveHeader;
- void setup() {
- Serial.begin(9600);
- // Connect to Wi-Fi network with SSID and password
- Serial.print("Setting AP (Access Point)…");
- // Remove the password parameter, if you want the AP (Access Point) to be open
- WiFi.softAP(ssid, password, max_connection);
- IPAddress IP = WiFi.softAPIP();
- Serial.print("AP IP address: ");
- Serial.println(IP);
- // if DNSServer is started with "*" for domain name, it will reply with
- // provided IP to all DNS request
- dnsServer.start(DNS_PORT, "*", IP);
- server.begin();
- }
- void loop(){
- dnsServer.processNextRequest();
- WiFiClient client = server.available(); // Listen for incoming clients
- if (client) { // If a new client connects,
- Serial.println("New Client."); // print a message out in the serial port
- String currentLine = ""; // make a String to hold incoming data from the client
- while (client.connected()) { // loop while the client's connected
- if (client.available()) { // if there's bytes to read from the client,
- char c = client.read(); // read a byte, then
- Serial.write(c); // print it out the serial monitor
- header += c;
- if (c == '\n') { // if the byte is a newline character
- saveHeader = header;
- // if the current line is blank, you got two newline characters in a row.
- // that's the end of the client HTTP request, so send a response:
- if (currentLine.length() == 0) {
- // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
- // and a content-type so the client knows what's coming, then a blank line:
- client.println("HTTP/1.1 200 OK");
- client.println("Content-type:text/html");
- client.println("Connection: close");
- client.println();
- // turns the GPIOs on and off
- if (header.indexOf("/get?input1=") >= 0) {
- Serial.println(PARAM_INPUT_1);
- }
- if (header.indexOf("&input2=") >= 0) {
- Serial.println(PARAM_INPUT_2);
- }
- // HTML web page to handle 2 input fields (input1, input2)
- const char index_html[] PROGMEM = R"rawliteral(
- <!DOCTYPE HTML><html><head>
- <title>ESP Input Form</title>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- </head><body>
- <form action="/get">
- SSID: <input type="text" name="input1"><br>
- Password: <input type="text" name="input2">
- <input type="submit" value="Submit">
- </form><br>
- </body></html>)rawliteral";
- client.println(index_html);
- // Break out of the while loop
- break;
- } else { // if you got a newline, then clear currentLine
- currentLine = "";
- }
- } else if (c != '\r') { // if you got anything else but a carriage return character,
- currentLine += c; // add it to the end of the currentLine
- }
- }
- }
- // Clear the header variable
- header = "";
- // Close the connection
- client.stop();
- Serial.println("Client disconnected.");
- Serial.println("");
- //saveHeader for testing purposes to find serial output
- Serial.println("this is saveHeader" + saveHeader + "end of saveHeader");
- }
- }
Thank you