Arduino web server upload file to sd card

Hi! I have this code that you upload a file to the Arduino over a web server and then my Arduino prints it in the serial monitor. So if I upload a txt file it file print out what is says in the serial monitor. I want to make it so I can upload a picture for example, to an sd card. I think I know how I would get it on the sd card. I want it to put the file stright on to the sd card with the same file name. The thing is it always gives me over information other than the file. I don’t know much about storage on Arduino but I believe that if I want to put an image on the sd card or something I can’t download it to the Arduino and then edit out the other information. There would be enough SPRAM.
So my question is, how would I get the correct information without storing the whole thing on the arduino?

This is my code:

#include <SPI.h>
#include <WiFiNINA.h>

#include "arduino_secrets.h" 
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // 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:
  }
  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("<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("POST /update")) {
          digitalWrite(LED_BUILTIN, HIGH);               // GET /H turns the LED on
         while (client.available()) {
char a = client.read();
Serial.write(a);
}
        }
      }
    }
    // 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);
}

And if I upload a txt file I get this in the serial monitor:

POST /upload HTTP/1.1
Host: 192.168.4.46
Connection: keep-alive
Content-Length: 321
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
Origin: http://192.168.4.46
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryhBnbMDWX6OhYSCjT
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Referer: http://192.168.4.46/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9

--WebKitFormBoundaryhBnbMDWX6OhYSCjT—

Example text.

--WebKitFormBoundaryhBnbMDWX6OhYSCjT—

It would also say the file name but I don’t any way to show that right now.
Thanks for any help!

An Arduino Uno has 2 kB of SRAM.
Almost any picture will have more kB.
What is the final purpose of modifying some data in the picture?

For picture-data modification I would use a raspberry Pi as the mimimum.
Maybe a Teensy 4.0 would work too.

But I'm asking again: what is the final purpose of modifying the picture-data?
Mowing a the green of a football-arena with a small scissor is just nonsense.
You would use a 7-bow-spindle-mower with GPS-steering.
best regards Stefan

1 Like

I don’t actually want to modify anything. I want to send any type of file to a wevserver that my Arduino makes and then save it onto an sd card with the same file name and ending.

You should specify with more precision how the bytes of the files will be send to the arduino.
Do you want to run the arduino a webserver with a website where the website has a
"choose file-dialog" selecting a file and then the file shall be uploaded?

I don't know it exactly but my estimation is that the tiny small 2kB of overall RAM that an arduino-Uno has is too little to manage someting like this.

You can do this with an ESP32 which has 160 kB of RAM available.

160 kB RAM is 80 times more than an Uno has.

You did not post what the final purpose of having the picture stored on SD-Card is.
Having stored pictures or any kind of file on an SD-Card is no self-purpose.

best regards Stefan

The purposes is for the Arduino to run a web server which any cômputo can acces and upload any type of file. This file will then be put onto an sd card So it receives one bit from the file which is being uploaded and then sends it to the sd card. My understanding is that if the Arduino only deals with one bit at a time it will not take up all the ram.

This project might not seem to have a lot of point, but I want to do it.
Tell me if anything is still unclear.

OK just try it

Yes but I do not know how to do that.

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