Parse string for wifi POST

Hi! I've been trying to use wifinina to create a website that files can be sent to arduino. Basically I want to only print the information from the file into the serial moniter.

this is the code:

#include <WiFiNINA.h>

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = "xxxxxxx";  // your network SSID (name)
char pass[] = "xxxxxxx";  // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;             // your network key index number (needed only for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
  Serial.begin(9600);  // initialize serial communication
  pinMode(9, OUTPUT);  // set the LED pin mode

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true)
      ;
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to WiFi network:
 while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);  // print the network name (SSID);*/

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    //delay(1000);
  }
  server.begin();     // start the web server on port 80
  printWifiStatus();  // you're connected now, so print out the status
}


void loop() {
  WiFiClient client = server.available();  // listen for incoming clients

  if (client) {                    // 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 (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 to the serial monitor
        if (c == '\n') {           // if the byte is a newline character

          // 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();

            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/H\">here</a> turn the LED on pin 9 on<br>");
            client.print("Click <a href=\"/L\">here</a> turn the LED on pin 9 off<br>");
            client.print("<form method='POST' action='/update' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>");

            // The HTTP response ends with another blank line:
            client.println();
            // 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
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          digitalWrite(LED_BUILTIN, HIGH);  // GET /H turns the LED on
        }
        if (currentLine.endsWith("GET /L")) {
          digitalWrite(LED_BUILTIN, LOW);  // GET /L turns the LED off
        }
        if (currentLine.endsWith("POST /update")) {
          while (client.available()) {
            char n = client.read();
            Serial.write(n);
            }
          }
        }
      }
    }
    // close the connection:
    client.stop();
    //Serial.println("client disconnected");
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}

That will print this when I upload a txt file with the word "test" in it:

IP Address: 192.168.1.161
signal strength (RSSI):-68 dBm
To see this page in action, open a browser to http://192.168.1.161

HTTP/1.1
Host: 192.168.1.161
Origin: http://192.168.1.161
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarycmZUsToBlfO5TBiP
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15
Referer: http://192.168.1.161/
Content-Length: 188
Accept-Language: en-GB,en;q=0.9

------WebKitFormBoundarycmZUsToBlfO5TBiP

Content-Disposition: form-data; name="update"; filename="test.txt"

Content-Type: text/plain

test

------WebKitFormBoundarycmZUsToBlfO5TBiP--

I want the arduino to only print what is in the file, in this case the word test. But I do NOT want the arduino to store the hole string, since if I uploaded a video file that would be too big, and not a string, it should just read a byte from the file and print it.

Thanks for any help!
(Tell me if something is unclear)

when you arrive there,

this has been received (all the headers)

so the next byte you read is the first byte of what has been sent to you

Yes, that much I get, but the thing is it end with


------WebKitFormBoundarycmZUsToBlfO5TBiP--

And I don’t get how to not include that bit.
So if I read everything after the header it would be

test
------WebKitFormBoundarycmZUsToBlfO5TBiP--

you have a multipart message so that data will arrive in chunks
this keyword mark the boundary of a message chunk

so you need to detect in the header what the boundary string is going to be

Content-Type: multipart/form-data; boundary=----WebKitFormBoundarycmZUsToBlfO5TBiP

and then when you see it, just ignore it

probably you should read the header line by line

Ok I thinkI get it, so I would read line by line and not byte by byte, so when I see this

----WebKitFormBoundarycmZUsToBlfO5TBiP

I ignore it.
How would I read line by line?
Could you give an example in code (if possible)
Thanks!

I would suggest to study Serial Input Basics to handle this
Instead of reading from serial you read from the client and your end marker is ‘\n’

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.