Ask Your Question
0

How can an ESPAsyncWebServer transmit a large two-dimensional array using RAM chunks?

asked 2023-05-11 20:56:33 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-11 21:11:02 +0000

nofretete gravatar image

To transmit a large two-dimensional array using RAM chunks with ESPAsyncWebServer, you can follow these steps:

  1. Divide the two-dimensional array into smaller chunks of a fixed size.
  2. Create a method that sends a chunk of data over the network using the ESPAsyncWebServer's send() method. This method should take the starting index of the chunk and the size of the chunk as input parameters.
  3. Create a loop that iterates over the chunks and sends each chunk separately using the method created in step two.
  4. On the client-side, create a loop that receives each chunk of data and concatenates them into a single array.

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-11 20:56:33 +0000

Seen: 8 times

Last updated: May 11 '23