Layer: L7 Application ยท Build flags: PC_ENABLE_VFS
What this example teaches
The pc_vfs_* API is one file interface with swappable backends: a RAM pool in host tests, a real filesystem on the device. The point is that features target one API and the application chooses the medium. Here it is mounted on LittleFS so writes persist across reboots; mounting the RAM backend instead changes nothing in the endpoints.
Mount a backend once, then use the same calls everywhere:
LittleFS.begin(true);
pc_vfs_mount(pc_vfs_fs(&LittleFS));
The file operations are backend-agnostic:
pc_vfs_write_file(path, data, strlen(data));
long n = pc_vfs_read_file(path, buf, cap);
long sz = pc_vfs_size(path);
pc_vfs_remove(path);
The four routes (/save, /load, /size, /rm) are thin wrappers over those calls, with a small helper that turns a ?name= query into a leading-slash path.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPC_ENABLE_VFS=1" \
--lib="." examples/L7-Application/Vfs/Vfs.ino
curl "http://<ip>/save?name=greeting&data=hello" # store /greeting
curl "http://<ip>/load?name=greeting" # hello
curl "http://<ip>/size?name=greeting" # 5
curl "http://<ip>/rm?name=greeting" # delete
Annotated source
The complete sketch (Vfs.ino), reproduced verbatim with added explanatory comments:
#define PC_ENABLE_VFS 1
#include <LittleFS.h>
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
static const char *path_of(
HttpReq *req,
char *out,
size_t cap)
{
if (!name || !*name)
return nullptr;
snprintf(out, cap, "/%s", name);
return out;
}
void setup()
{
Serial.begin(115200);
delay(250);
Serial.print("IP: ");
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
LittleFS.begin(true);
pc_vfs_mount(pc_vfs_fs(&LittleFS));
char path[PC_VFS_NAME_MAX];
if (!path_of(req, path, sizeof(path)) || !data)
{
server.
send(
id, 400,
"application/json",
"{\"error\":\"name+data\"}");
return;
}
bool ok = pc_vfs_write_file(path, data, strlen(data));
server.
send(
id, ok ? 200 : 500,
"application/json", ok ?
"{\"ok\":true}" :
"{\"ok\":false}");
});
char path[PC_VFS_NAME_MAX];
if (!path_of(req, path, sizeof(path)))
{
server.
send(
id, 400,
"text/plain",
"name?");
return;
}
char buf[512];
long n = pc_vfs_read_file(path, buf, sizeof(buf) - 1);
if (n < 0)
{
server.
send(
id, 404,
"text/plain",
"not found");
return;
}
buf[n] = '\0';
server.
send(
id, 200,
"text/plain", buf);
});
char path[PC_VFS_NAME_MAX];
long n = path_of(req, path, sizeof(path)) ? pc_vfs_size(path) : -1;
char b[24];
snprintf(b, sizeof(b), "%ld", n);
server.
send(
id, 200,
"text/plain", b);
});
char path[PC_VFS_NAME_MAX];
bool ok = path_of(req, path, sizeof(path)) && pc_vfs_remove(path);
server.
send(
id, ok ? 200 : 404,
"application/json", ok ?
"{\"ok\":true}" :
"{\"ok\":false}");
});
}
void loop()
{
}
Single-port HTTP server with deterministic, zero-allocation execution.
void send(uint8_t slot_id, int code, const char *content_type, const char *payload)
Send an HTTP response with a body and close the connection.
int32_t begin(const WebServerConfig *cfg=nullptr)
Initialize all connection slots and open all registered listeners.
void on(const char *path, HttpMethod method, Handler callback)
Register a route handler.
void handle()
Drive the server - call every Arduino loop() iteration.
const char * http_get_query(const HttpReq *req, const char *key)
Look up a query parameter value by name (case-sensitive).
bool init_wifi_physical(const char *, const char *)
Connect to a WiFi access point.
uint32_t pc_net_egress_ip(void)
IPv4 (network byte order) of the current egress interface, or 0 if none.
bool wifi_ready()
True if the WiFi station link is up (associated + an IP is assigned).
Layer 1 (Physical) - link bring-up and live egress-interface reporting.
Layer 7 (Application) - public HTTP routing API.
@ HTTP_GET
Safe, idempotent read.
Fully-parsed HTTP/1.1 request.
Unified virtual filesystem wrapper (PC_ENABLE_VFS).