1 | initial version |
To transmit a large two-dimensional array using RAM chunks with ESPAsyncWebServer, you can follow these steps:
Here is an example code snippet that demonstrates how to implement the above steps:
const int CHUNK_SIZE = 100; // Chunk size in bytes
const int ROW_NUM = 1000; // Number of rows in the array
const int COL_NUM = 1000; // Number of columns in the array
int data[ROW_NUM][COL_NUM]; // The two-dimensional array to transmit
void sendChunk(AsyncWebServerRequest *request, int start, int size) {
int end = start + size;
if (end > ROW_NUM * COL_NUM) {
end = ROW_NUM * COL_NUM;
}
AsyncWebServerResponse *response = request->beginResponse((end - start), "application/octet-stream");
response->addHeader("Content-Disposition", "attachment; filename=data.bin");
request->send(response);
request->client()->write((const uint8_t*) &data[start / COL_NUM][start % COL_NUM], (end - start));
}
void handleRequest(AsyncWebServerRequest *request) {
for (int i = 0; i < ROW_NUM * COL_NUM; i += CHUNK_SIZE) {
sendChunk(request, i, CHUNK_SIZE);
}
}
// Client-side code
int receivedData = 0;
int receivedChunks = 0;
int receivedArray[ROW_NUM][COL_NUM];
void receiveChunk(byte *data, size_t len) {
memcpy((void*) &receivedArray[receivedData / COL_NUM][receivedData % COL_NUM], data, len);
receivedData += len;
receivedChunks++;
if (receivedChunks == (ROW_NUM * COL_NUM + CHUNK_SIZE - 1) / CHUNK_SIZE) { // Check if all chunks have been received
// Do something with the received array
}
}
In this example, we first define the chunk size and the size of the array. We then create the sendChunk() method that takes the request, the start index of the chunk, and the size of the chunk as input parameters. This method creates a response that specifies the length of the data and its content type. It then sends the chunk of data using the write() method of the request client.
Next, we create the handleRequest() method that iterates over the array in chunks and sends each chunk using the sendChunk() method.
On the client-side, we define the receivedData and receivedChunks variables, and the receiveChunk() method that is called for each received chunk of data. This method copies the received data into the receivedArray and updates the receivedData and receivedChunks variables. Once all the chunks have been received, we can perform any actions we want with the receivedArray.