How to Parse out HTML Data in c++ for ESP32

amaalouf
Posts: 1
Joined: Fri Mar 06, 2020 12:40 am

How to Parse out HTML Data in c++ for ESP32

Postby amaalouf » 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:
  1.            
  2.  
  3.              if (header.indexOf("/get?input1=") >= 0) {
  4.              Serial.println(PARAM_INPUT_1);
  5.             }
  6.             if (header.indexOf("&input2=") >= 0) {
  7.               Serial.println(PARAM_INPUT_2);
  8.             }
  1.  
  2.  
  3. #include <Arduino.h>
  4. #include <WiFi.h>
  5. #include <DNSServer.h>
  6. // Replace with your network credentials
  7. const char* ssid     = "dictatorTippens";
  8. const char* password = "teamomega";
  9. int max_connection = 4;
  10.  
  11. const byte DNS_PORT = 53;
  12.  
  13. const char* PARAM_INPUT_1 = "input1";                           //  copied over
  14. const char* PARAM_INPUT_2 = "input2";                           //  copied over
  15. const char* PARAM_INPUT_3 = "input3";                           //  copied over
  16.  
  17. DNSServer dnsServer;
  18. // Set web server port number to 80
  19. WiFiServer server(80);
  20.  
  21. // Variable to store the HTTP request
  22. String header;
  23. String saveHeader;
  24.  
  25. void setup() {
  26.   Serial.begin(9600);
  27.  
  28.   // Connect to Wi-Fi network with SSID and password
  29.   Serial.print("Setting AP (Access Point)…");
  30.  
  31.   // Remove the password parameter, if you want the AP (Access Point) to be open
  32.   WiFi.softAP(ssid, password, max_connection);
  33.  
  34.   IPAddress IP = WiFi.softAPIP();
  35.   Serial.print("AP IP address: ");
  36.   Serial.println(IP);
  37.  
  38.   // if DNSServer is started with "*" for domain name, it will reply with
  39.   // provided IP to all DNS request
  40.   dnsServer.start(DNS_PORT, "*", IP);
  41.  
  42.   server.begin();
  43. }
  44.  
  45. void loop(){
  46.   dnsServer.processNextRequest();
  47.   WiFiClient client = server.available();   // Listen for incoming clients
  48.  
  49.   if (client) {                             // If a new client connects,
  50.     Serial.println("New Client.");          // print a message out in the serial port
  51.     String currentLine = "";                // make a String to hold incoming data from the client
  52.     while (client.connected()) {            // loop while the client's connected
  53.       if (client.available()) {             // if there's bytes to read from the client,
  54.         char c = client.read();             // read a byte, then
  55.         Serial.write(c);                    // print it out the serial monitor
  56.        
  57.         header += c;
  58.         if (c == '\n') {                    // if the byte is a newline character
  59.           saveHeader = header;
  60.           // if the current line is blank, you got two newline characters in a row.
  61.           // that's the end of the client HTTP request, so send a response:
  62.           if (currentLine.length() == 0) {
  63.             // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  64.             // and a content-type so the client knows what's coming, then a blank line:
  65.             client.println("HTTP/1.1 200 OK");
  66.             client.println("Content-type:text/html");
  67.             client.println("Connection: close");
  68.             client.println();
  69.            
  70.             // turns the GPIOs on and off
  71.             if (header.indexOf("/get?input1=") >= 0) {
  72.               Serial.println(PARAM_INPUT_1);
  73.             }
  74.             if (header.indexOf("&input2=") >= 0) {
  75.               Serial.println(PARAM_INPUT_2);
  76.             }
  77.                          
  78.  
  79.            // HTML web page to handle 2 input fields (input1, input2)
  80.             const char index_html[] PROGMEM = R"rawliteral(
  81.           <!DOCTYPE HTML><html><head>
  82.              <title>ESP Input Form</title>
  83.              <meta name="viewport" content="width=device-width, initial-scale=1">
  84.              </head><body>
  85.              <form action="/get">
  86.                SSID: <input type="text" name="input1"><br>
  87.                Password: <input type="text" name="input2">
  88.                <input type="submit" value="Submit">
  89.              </form><br>
  90.            </body></html>)rawliteral";
  91.  
  92.             client.println(index_html);
  93.    
  94.             // Break out of the while loop
  95.             break;
  96.           } else { // if you got a newline, then clear currentLine
  97.             currentLine = "";
  98.           }
  99.         } else if (c != '\r') {  // if you got anything else but a carriage return character,
  100.           currentLine += c;      // add it to the end of the currentLine
  101.         }
  102.       }
  103.     }
  104.  
  105.    
  106.     // Clear the header variable
  107.     header = "";
  108.     // Close the connection
  109.     client.stop();
  110.     Serial.println("Client disconnected.");
  111.     Serial.println("");
  112.     //saveHeader for testing purposes to find serial output
  113.     Serial.println("this is saveHeader" + saveHeader + "end of saveHeader");
  114.   }
  115. }
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
Attachments
imgur.PNG
imgur.PNG (17.02 KiB) Viewed 5209 times

Who is online

Users browsing this forum: No registered users and 110 guests