socket / HTTP Server: How to answer 1 request with multiple packets (to save RAM) #13393
-
I have build a specialized ESP32 Datalogger with an http server to get the data out of the logger.
for the server and start it with
This works so far. I log the data into a ringbuffer for which i use a bytearray. This is how i want to solve it: I know, there would be also other solutions possible, like answer the request only with a small page, with some data and wait for a new request to get the next page of data. but this is not what my question is about! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I had a similar out of memory situation when reading large files as HTTP replies. Here's how I solved it... First, create a function that returns the file contents in small chunks rather than all at once.
Then, call that function repeatedly, draining between each write.
The HTTP client connection stays open until the entire file is sent. By having the "chunk" in a separate function, the memory allocated to the chunk variable gets deleted each time and flagged for garbage collection. This keeps RAM usage low. You can probably adapt this code to send out your buffer contents instead of a file. This is part of a larger project. For the whole thing, see: https://github.com/DavesCodeMusings/thimble/blob/main/thimble.py |
Beta Was this translation helpful? Give feedback.
-
Many thanks, this helps a lot! Before i could use only a buffer of about 3.000 measurement data sets, now a buffer of 12.000 sets is possible. I have done it like this
the response part:
With tests i found out, only the first packet should contain the HTTP startline and HTTP headers. the following packets are pure data. like defined in RFC 2616 a chunked answer to a request will be finished (and the started download ends) if a zero length chunk is received. unfortunately i don't know how to send a zero length chunk. I have checked implementation of writer.drain(). it will only send something if len(buf) > 0 ... |
Beta Was this translation helpful? Give feedback.
Many thanks, this helps a lot!
Before i could use only a buffer of about 3.000 measurement data sets, now a buffer of 12.000 sets is possible.
I have done it like this
the response part: