Page 1 of 1
ESPAsyncWebServer : how to send a file ?
Posted: Thu Apr 04, 2024 7:38 pm
by christianw
I have an ESP32 with an ESPAsyncWebServer which records data in a table (3600 rows maximum)
How do I get the web server to send the data as a CSV file to the browser?
Thanks in advance
Re: ESPAsyncWebServer : how to send a file ?
Posted: Thu Apr 04, 2024 9:42 pm
by lbernstone
If you are just sending a file from the flash, you can use the serveStatic method.
https://github.com/me-no-dev/ESPAsyncWe ... le-by-name
The mime type would be "text/csv".
I would recommend you add a max-age header at the frequency that you are collecting the data.
Re: ESPAsyncWebServer : how to send a file ?
Posted: Fri Apr 05, 2024 4:18 am
by christianw
lbernstone wrote: ↑Thu Apr 04, 2024 9:42 pm
If you are just sending a file from the flash
Thanks but... datas are in RAM : I have a array to store them :
Code: Select all
struct SDATA {
float temp;
long tmillis;
};
SDATA TabMesures[3600];
Re: ESPAsyncWebServer : how to send a file ?
Posted: Fri Apr 05, 2024 5:33 am
by lbernstone
I assume this is data that you actually want to log anyhow. Write it to a file, and then staticServe that file.
If you want a lightweight data logger that's a bit more sophisticated, take a look at
rrdTool
Re: ESPAsyncWebServer : how to send a file ?
Posted: Fri Apr 05, 2024 6:47 am
by christianw
I prefer not to save the data permanently but to transmit it either for downloading or for display (using chart.js for example).
Re: ESPAsyncWebServer : how to send a file ?
Posted: Fri Apr 05, 2024 6:48 am
by boarchuz
CSV is very basic, you could construct it on-the-fly from memory (assuming ESPAsyncWebServer exposes the right stuff to make it possible).
It could be as simple as this (pseudocode obviously):
Code: Select all
http_server.on(HTTP_GET, "/data.csv") {
response.header.set("Content-Type", "text/csv");
// TODO: set Content-Length or use chunked encoding
response.begin();
for(int row = 0; row < ARRAY_SIZE(TabMesures); ++row) {
char buffer[20];
sprintf(buffer, "%.2f", TabMesures[row].temp);
response.send(buffer);
response.send(',');
sprintf(buffer, "%ld", TabMesures[row].tmillis);
response.send(buffer);
response.send('\n');
}
response.end();
}
Re: ESPAsyncWebServer : how to send a file ?
Posted: Fri Apr 05, 2024 7:13 am
by christianw
I'll try this later, thanks.
But
won't work with an ESP32. I'll change to
but that's a detail.
On the other hand, I'm more worried about the RAM taken up: about 50 kB for the whole CSV
Re: ESPAsyncWebServer : how to send a file ?
Posted: Fri Apr 05, 2024 8:44 am
by lbernstone
You can do a
printf directly into the response method in a for loop. This will stream it, and likely is breaking it into mtu-sized chunks to deliver on the line.