WifiClient write sends gibberish characters.

zydizz
Posts: 8
Joined: Mon Dec 12, 2022 2:36 pm

WifiClient write sends gibberish characters.

Postby zydizz » Thu Dec 29, 2022 3:05 pm

Hello everyone,
This topic arised from the continuation of the post here viewtopic.php?f=19&t=31074.

I need a buffer to read from a sd card file which is about 20kb's in size. The size of this buffer is set by a define macro which currently is set to 1024 bytes. However, while reading from the file in 1024 byte chunks I also have to delete "\r" and "\n" characters and replace them with " * " (asterisk). That part of the code is confirmed working but here is the problem:

After reading these characters into the buffer array they get sent to the "client". But instead of 1024 characters 1028 characters get sent to the server (the extra 4bytes are gibberish). Also passing the "size" to "client.write" doesn't help with anything and results in a bad request that is incompleted.

Strangely the extra characters also appear when Serial.println(fileBuffer) is used for debugging (1028 characters are printed), yet the array has a size of 1024.

Here is the code that sends and formats the file:
  1. void readPost(fs::FS &fs, const char * path) {
  2.   Serial.printf("Reading file: %s\n", path);
  3.  
  4.   File file = fs.open(path);
  5.   if(!file) {
  6.     Serial.println("Failed to open file for reading");
  7.     return;
  8.   }
  9.  
  10.   size_t fLen = file.size();
  11.   char * fileBuffer = new char[BUFSIZE + 1];
  12.  
  13.   WiFiClient client;
  14.   Serial.println("\nStarting connection to server...");
  15.   if (!client.connect(httpHost, 80)) {
  16.     Serial.println("Connection failed!");
  17.     return;
  18.   } else {
  19.     Serial.println("Connected to server!");
  20.     client.println();
  21.     client.println();
  22.     client.println("POST " + String(httpPath) + " HTTP/1.1");
  23.  
  24.     // Make a HTTP request
  25.     client.println("Host: " + String(httpHost));
  26.     client.println("Connection: close");
  27.     client.println("Content-Type: text/plain");
  28.     client.print("Content-Length: ");
  29.     client.print(fLen + 29);
  30.     client.print("\r\n");
  31.     client.println();
  32.  
  33.     client.print("{\"DeviceId\":\"");
  34.     client.print(deviceId);
  35.     client.print("\",\"Data\":\"");
  36.  
  37.     uint8_t skip = 0;
  38.     uint8_t written = 0;
  39.     while(file.available()){
  40.       size_t toRead = file.available();
  41.       if(toRead > BUFSIZE) {
  42.         toRead = BUFSIZE;
  43.       }
  44.       for(i = 0; i < toRead; i++) {
  45.         if (skip == 0) {
  46.           fileBuffer[i] = file.read();
  47.           if (fileBuffer[i] == '\r') {
  48.             if (file.peek() == '\n') {
  49.               skip = 1;
  50.               fileBuffer[i] = '*';
  51.             }
  52.           }
  53.         } else {
  54.           skip = 0;
  55.           i -= 1;
  56.           file.read();
  57.         }
  58.       }
  59.       written = client.write(fileBuffer);
  60.       Serial.println(written);
  61.       delay(100);
  62.     }
  63.     client.print("\"}");
  64.     client.print("\r\n");
  65.  
  66.     while (client.connected()) {
  67.       String line = client.readStringUntil('\n');
  68.       if (line == "\r") {
  69.         Serial.println("headers received");
  70.         break;
  71.       }
  72.     }
  73.     while (client.available()) {
  74.       char c = client.read();
  75.       Serial.write(c);
  76.     }
  77.     Serial.println();
  78.   }
  79.   client.stop();
  80.   delete [] fileBuffer;
  81.   return;
  82. }

Example of the read file (each line has a " \r\n" at their end):
file.png
file.png (57.6 KiB) Viewed 951 times
Example of the message recieved on the server side (the first 1024 bytes written + the 4 byte extra gibberish, with "\r\n"s replaced with * ):
serv.png
serv.png (35.62 KiB) Viewed 951 times

Any recommendations or corrections? What may be the cause of this?

Who is online

Users browsing this forum: No registered users and 46 guests