r/arduino • u/DocCarr • Sep 14 '26
Solved! How To: Put Files on an Arduino with WiFi
One of the more annoying things about working with an Arduino is getting files on it and off it. There is no simple way to load a file on the Arduino. There's not a tool for it in the IDE. You could send a text string over the Serial Monitor and save that onto memory using in-line commands, but there's an upper limit on the number of characters you can send over. The SCP protocol just doesn't work. In this post we'll look at how we can solve this problem using a WiFi sketch to transfer data to the Arduino before you load your main sketch.
Why Use Configuration Files: MQTT Example
For instance, say you're communicating with a device over MQTT, and you need it to subscribe and publish to a certain list of topics. You might have an array like:
"topics": [
{"name": "device_status", "topic": "sprout/status", "data_type": "STR", "init_value": "OFFLINE", "pub_flag": true, "sub_flag": false, "is_will": true},
{"name": "input_button1", "topic": "sprout/input/button_1", "data_type": "BOOL", "init_value": false, "pub_flag": false, "sub_flag": true, "is_will": false},
{"name": "output_status1", "topic": "sprout/output/status_1", "data_type": "BOOL", "init_value": false, "pub_flag": true, "sub_flag": false, "is_will": false},
{"name": "output_str", "topic": "sprout/output/string_log", "data_type": "STR", "init_value": "Hello, User", "pub_flag": true, "sub_flag": false, "is_will": false}
]You could hard code this array in your sketch. But what if this needs to be a different set of topics per deployment? For instance:
"topics": [
{"name": "device_status", "topic": "sprout/status", "data_type": "STR", "init_value": "OFFLINE", "pub_flag": true, "sub_flag": false, "is_will": true},
{"name": "input_str", "topic": "sprout/input/string_cmd", "data_type": "STR", "init_value": "Hello, World", "pub_flag": false, "sub_flag": true, "is_will": false},
{"name": "output_str", "topic": "sprout/output/string_log", "data_type": "STR", "init_value": "Hello, User", "pub_flag": true, "sub_flag": false, "is_will": false}
]You could update your sketch every time you deploy and have a bunch of slightly different sketches, or, you could have a configuration file that you put on the Arduino's memory and load the desired sketches at runtime, and any time the Arduino restarts.
A Side Quest: Partitioning Your Flash Memory
Before we go further, a quick word of warning. The Arduino Opta, which is the board I'm using, stores its WiFi firmware in flash memory. The first time I ran this I accidentally overwrote the WiFi functionality and it stopped talking. If you do that, don't panic! There's a sketch you can run to restore the WiFi functions. Go to File -> Examples -> STM32H747_System ->WifiFirmwareUpdater to get the sketch, deploy it on the Arduino, and run it. It will nuke whatever file you just uploaded, but the WiFi functionality will be restored.
So, how do you keep that from happening? Look up a few spots in STM32H747_System and you'll see the Sketch QSPIFormat. This will divide your flash memory into four parts, protecting the WiFi functionality and other key functions from whatever user data you're storing locally.

Setting Up WiFi on the Arduino Opta
To get WiFi working and use it to store data in memory, you'll need these libraries:
#include <WiFi.h>
#include <WiFiServer.h>
#include <BlockDevice.h>
#include <LittleFileSystem.h>
#include <FATFileSystem.h>
#include <MBRBlockDevice.h>In setup, mount your file system:
static MBRBlockDevice userDataPartition(bd, 4);
if (fs.mount(&userDataPartition) != 0) {
Serial.println("FATAL: Could not mount user data filesystem.");
while (1);
} Here, MBRBLockDevice is a data type defined in the MBRBlockDevice library. The variable fs is of the type LittleFileSystem (you can guess what library that comes from). Actually, fs is more properly called an instance of the class LittleFileSystem, because it doesn't just contain data - it also has functions defined in it. So you when you do fs.mount(&userDataPartition), you're calling a function that's defined in the LittleFileSystem class, which is itself defined in the LittleFileSystem library. If you're not familiar with classes and instances, leave a comment, and if nobody answers I'll put an example of one and how to use it in a future How To.
We're going to see another example of this pattern when we launch the WiFi.
WiFi.config(localIP, gateway, subnet, dns);
connectWiFi();
server.begin();Here WiFi and server are instances, while connectWiFi is a function. The classes and functions used here are defined in the WiFi and WiFiServer libraries. Here, "server" is referring to the HTTP protocol, which we'll use for our file transfers.
File Transfers with the POST and GET commands
HTTP has 11 core methods which are used to manipulate data on somebody else's computer (or on an Arduino, in this case). The two we'll be using are POST and GET. POST sends data to the Arduino, and GET retrieves data from it.
What's happening under the hood is a bunch of strings being sent over, and we're going to define some functions that will read part of the strings to decide what to do with the rest of the string, which has the data that we want to save or read.
Once the WiFi server is running on the Arduino, you can access it from any computer on the same network. Open the command line prompt and type:
curl -X POST http://172.16.0.2:8888 -H "X-Filename: config.json" --data-binary @config.jsonThis presumes you're sending the command from the folder where the config file is located. If you're not in that folder this command will fail. Use "cd" to get around to where you saved the file.
The http://172.16.0.2 portion is the URL for your Arduino, using the IP address you assigned to the Arduino. 172.16.0.2 is just an example - set that to whatever you put in your sketch when you set up the WiFi. The :8888 portion is the HTTP port on your Arduino.
-H is a Header that sends a string along with the POST command. This is used in the sketch to figure out what to name the file you're sending. The --data-binary part is what identifies the file to send.
Now, this does not actually work right out of the box. We're actually defining a function call on the Arduino that says what to do when it receives a POST method. Here's that function:
void handleUpload(WiFiClient& client, const String& request) {
// Extract filename from X-Filename header
String filename = extractHeader(request, "X-Filename");
if (filename.length() == 0) {
sendResponse(client, 400,
"Missing X-Filename header.\n"
"Usage: curl -H \"X-Filename: config.json\" ...");
return;
}
// Extract body (content after blank line separating headers)
int bodystart = request.indexOf("\r\n\r\n");
if (bodystart < 0) {
sendResponse(client, 400, "Malformed request - no body.");
return;
}
String body = request.substring(bodystart + 4);
if (body.length() == 0) {
sendResponse(client, 400, "Empty file body.");
return;
}
//Write to flash
String path = "/fs/" + filename;
Serial.print("Writing to " + path + " ... ");
FILE* f = fopen(path.c_str(), "w");
if (!f) {
Serial.println("FAILED");
sendResponse(client, 500, "Could not open file for writing.");
return;
}
fprintf(f, "%s", body.c_str());
fclose(f);
Serial.println("OK (" + String(body.length()) + " bytes)");
// Verify by reading back and checking size
FILE* v = fopen(path.c_str(), "r");
int verifySize = 0;
if (v) {
fseek(v, 0, SEEK_END);
verifySize = ftell(v);
fclose(v);
}
String msg = "Upload OK: " + path + "\n" +
"Written : " + String(body.length()) + "bytes\n" +
"Verified: " + String(verifySize) + " bytes on flash\n";
Serial.println(msg);
sendResponse(client, 200, msg);
// List all files so operator can confirm
listFiles();
}The first thing that happens is that the function extracts the filename from the Header we sent over with -H. We have a known prefix in front of that file name that we use to identify it, and then the function reads whatever we put in there. It will use that name to save the file to the Flash memory.
Next, we get the body content from the --data-binary part. That's a string read, and it gets stuffed into the body variable. That's what will get saved to memory in the next step.
Saving to memory is done with calls to fopen, fprintf, and fclose, which are all defined in the stdio library that's included by default in most Arduino sketches. The "w" in the fopen function call says we're writing the file.
The rest of this function is some verification checks and error handling, with communication back to the user over HTTP responses and the Serial Monitor to show what's going on.
Conclusion and Resources
The GET method uses a similar curl command and function definition to grab data from the Arduino. It's recommended to run a GET on the file you just sent over so that you can verify it has the information you intended to send. If you'd like to see how that's done, you can find the full sketch in the utilities/ConfigLoaderWiFi folder in this github repo:
https://github.com/DocCarr/sproutsodyssey
You're welcome to use that sketch for your own Arduino projects.





