String on Flash esp32

How can i write a string on esp32’s flash memory? I tried to convert a string to bytearray(16) but it exit with typeError

1 Like

I found the easiest way was to construct a JSON object containing all the information I wanted to write to flash. It is always usually more than just a string and if you use a JSON object it is easy to add to it.
Then I used the following:
jsond = {
“Field1” : “Whatever”,
“Field2” : 123,

}
jsons = json.dumps( jsond )
EspFlash = flash.FlashFileStream( constants.FLASH_START, constants.FLASH_SIZE )
EspFlash.write( len( jsons ) )
EspFlash.write( jsons )
EspFlash.flush()
EspFlash.close()

1 Like

thank you very much :grinning:

how can i read it then?

I used the following

    EspFlash = flash.FlashFileStream( constants.FLASH_START, constants.FLASH_SIZE )
    size = EspFlash.read_int()                # get size of following buffer 
   # do a few checks on validity of size which I have omitted
    paramsb = bytearray()                       # create an array to hold flash
    for i in range(4,size+4):                     # copy to array from flash
        paramsb.append( EspFlash[i]  )
    paramss = str( paramsb )                        # convert to a string 
    paramsj = json.loads( paramss )                 # convert to JSON

paramsj now has JSON object
I made the first field in the JSON object a constant specific value to check that the JSON object was valid

1 Like

This is the error on the last code line:
[Thread 1 exited with exception JSONError @[0043:0026:0050:006C:0000:0071:0000:0000]](javascript:window.except(’@[0043:0026:0050:006C:0000:0071:0000:0000]’,'Thread 1 exited with exception JSONError '))
Why? :cry:

Off hand I don’t know. The code was lifted straight from my working code. The only thing I can think of is that the write was not done correctly or the json object is corrupted