Write binary file to SPIFFS folder

Hicham
Posts: 8
Joined: Sun Feb 21, 2021 12:31 am

Write binary file to SPIFFS folder

Postby Hicham » Wed Mar 10, 2021 8:08 am

Hei guys, how are u doing ,

i am trying to write a binary file into my flash memey using SPIFFS, so i can perform update

here its the idea, i am using sim800 to get the information from my FTP server, in the other hand i am listening to the serial port to read data comming from the sim800, and stock it into string, so every 1024bytes i write its to a bin file to the memory,

but in the end i find that the size is not the same as the size of the real folder,

PS : when i do the same things with a txt file , everything is good

any advice, thank you

i post just the block concerne the process
  1. File file = SPIFFS.open("/firm.bin","w");
  2.   if(!file) Serial.println("Error opening file for writing");
  3.   else Serial.println("file ready for write");
  4.  
  5.   for (int i=0;i<ii;i++)
  6.   {
  7.   Serial2.println("AT+FTPGET=2,1024");
  8.   tempFtp = Serial2.readString();
  9.   uint8_t ind1 = tempFtp.indexOf(',');
  10.   uint8_t ind2 = tempFtp.indexOf(',', ind1 + 1);
  11.   // Serial.print(tempFtp.substring(ind2+strlen(fileSize.c_str())+3,ind2+strlen(fileSize.c_str())+3+1024));
  12.   //Serial.print(tempFtp.substring(ind2+7,ind2+1031));
  13.   file.print(tempFtp.substring(ind2+7,ind2+1031));
  14.   Serial.println(i);
  15.   //Serial.println(tempFtp);
  16.   }
  17.   if(m!=0) // test if there is more data to read ( <1024 bytes )
  18.   {
  19.   char atComFileSize[size]={0};
  20.   sprintf(atComFileSize,"AT+FTPGET=2,%d",m);
  21.   Serial2.println(atComFileSize);
  22.   tempFtp = Serial2.readString();
  23.   uint8_t ind1 = tempFtp.indexOf(',');
  24.   uint8_t ind2 = tempFtp.indexOf(',', ind1 + 1);
  25.  
  26.   file.print(tempFtp.substring(ind2+3+l,ind2+m+3+l));
  27.   }
  28.   file.close();
  29.   Serial.println("Done");

ESP_Sprite
Posts: 8922
Joined: Thu Nov 26, 2015 4:08 am

Re: Write binary file to SPIFFS folder

Postby ESP_Sprite » Thu Mar 11, 2021 3:28 am

I'm not particularily sure of how it works on Arduino, but a common pitfall of using string functions (e.g. file.print, substr etc) to move binary data around is that some things in the binary data may be misinterpreted as the functions expect 'standard text' as their inputs. In particular, if the data gets converted into a C string at any point in the pipeline, you'll get problems as a C string is assumed to end at a zero character. If there's a zero character somewhere in the binary data, it means the rest of the data in the string is ignored.

Hicham
Posts: 8
Joined: Sun Feb 21, 2021 12:31 am

Re: Write binary file to SPIFFS folder

Postby Hicham » Thu Mar 11, 2021 8:30 am

Thank you, that what i discovred too, and i changed to Serial.readbytes() function. so i can read and write to spiffs file .bin .

now i am at the point of performing the update from the file stored in the flash memery, hope its work .

rajvigneshtn
Posts: 1
Joined: Wed Jun 23, 2021 3:53 pm

Re: Write binary file to SPIFFS folder

Postby rajvigneshtn » Wed Jun 23, 2021 4:20 pm

Motive: Firmware update in ESP32 WROOM 32 module over GPRS (HTTP Read)

Effort:
I have downloaded a .bin file from HTTP server into RAM of Quectel M66 GPRS module.
Checking the HTTP read file size using AT commands, returns correct file size.
However when I try to download the file from the RAM of M66 and write into SPIFFS of ESP32, most of the characters are misinterpreted when I reprint it from SPIFFS for verifying. Further if I go ahead to update the firmware I keep getting "Magic Byte Error" even though the first byte of the file in SPIFFS is 0xE9.

Please find below the snippet of code I use to download the file from RAM and store it in SPIFFS.
Always the printPercentage function dosent progress beyond 91.32%.
There is mismatch in file sizes of that in RAM of M66 and that in SPIFFS

Code: Select all

void fileWrite()
{
 long timeout = millis();
 uint32_t readLength = 0;
 uint32_t contentLength = 213280;
 int header = 1;
 File file = SPIFFS.open("/update.bin", FILE_APPEND);
 delay(1000);
 Serial2.println("AT+QFDWL=\"RAM:TMP.bin\"");
 delay(100);
 Serial.println("Start");
 timeout = millis();
 while (readLength < contentLength && millis() - timeout < 20000L)
 {
  if (header == 1 && Serial2.available() )
  {
   header = 0;
   for (int i = 0; i < 34; i++)
   {
    Serial.write(Serial2.read());
   }
   Serial.println("Finish");
  }
  else
  {
   while (Serial2.available())
   {
    if (!file.print(char(Serial2.read())))
    {
     Serial.println("Appending file");
    }
    readLength++;
    if (readLength % (contentLength / 13) == 0)
    {
     printPercent(readLength, contentLength);
    }
    timeout = millis();
   }
  }
 }
 Serial.println("");
 Serial.print("File Size: ");
 Serial.println(file.size());
 Serial.println("Finish Upload");
 file.close();
}


Who is online

Users browsing this forum: Bing [Bot] and 99 guests