B4R Question Issues with the ESP8266 FileSystem

Marcel Cornelisse

New Member
Licensed User
Hi,
I'm new to the platform so I apologize upfront in case this turns out to be a stupid question.

I have an ESP01 (ESP8266) module and I want to store some data that will survive restarts of the ESP01. If I understood well, one of the options to achieve this is to use the ESP8266 file system implemented by the rESP8266FileSystem (V1.0) library. I used code as shown. The code doesnt work and I dont see any errors at runtime either.

The logging indicates there are 10 bytes written which is as I expected, but there are 0 bytes (no data) read and the file position remains 0

Furthermore the ListFiles Sub doesnt list any files at all, although the ReadFile Sub can find the file. I also tried to format the file system first (code removed), but that doesnt help either.

What am I doing wrong?

B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 1000
#End Region

Sub Process_Globals
    Public Serial1 As Serial
    Private fs As ESP8266FileSystem
    Private myFile As String = "myFiles/file1"
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Delay(1000)
    Log("AppStart")
    Start
    End Sub

Sub Start
    If(fs.initialize) Then
        Log(" file system successfully initialized")
    Else
        Log("could not initialize filesystem")
    End If

    WriteFile
    Readfile
    ListFiles
    

End Sub

Sub WriteFile
    If (fs.OpenReadWrite(myFile)) Then
        Log("file successfyully openend for write processing")
        Private buffer(10) As Byte ="1234567890".GetBytes
        Log("Bytes written: ", fs.Stream.WriteBytes(buffer,0,buffer.Length))
        Log("file system total size ", fs.TotalSize)
        Log("file system used size ", fs.UsedSize)
        Log("file size ", fs.CurrentFile.size)
        fs.Close
    End If
End Sub

Sub Readfile
    If (fs.OpenRead(myFile)) Then
        Log("file successfyully openend for read processing")
        Log("file name ", fs.CurrentFile.Name)
        Log("file size ", fs.CurrentFile.size)
        fs.Position=0
        Private buffer(10) As Byte
        Log ("number of bytes read: ", fs.Stream.ReadBytes(buffer,0,buffer.Length))
        Log("position after reading file: ", fs.Position)
        Private conv As ByteConverter
        Log("data read: ", conv.StringFromBytes(buffer))
        fs.Close
    Else
        Log("cannot open file for read: ", myFile)
    End If

End Sub

Sub ListFiles
    For Each f As File In fs.ListFiles("/")
        Log("Name: ", f.Name, ", Size: ", f.Size)
    Next
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Your code looks correct. This is the output when I run it:

file system successfully initialized
file successfyully openend for write processing
Bytes written: 10
file system total size 2949250
file system used size 100651
file size 10
file successfyully openend for read processing
file name myFiles/file1
file size 10
number of bytes read: 10
position after reading file: 10
data read: 1234567890


Try to set the Flash Size in the board configuration to 4M3M.
 
Upvote 0

Marcel Cornelisse

New Member
Licensed User
Hi Erel,
Thx for your fast response. Glad to see my code is correct :).
I tried your suggestion but this made the program crash immediately with the following message:

ets Jan 8 2013,rst cause:4, boot mode1,7)
wdt reset

Although Im pretty sure I have a 1M/512 board (ESP01), I tried some other flash configurations as well.
None of them worked.

Before using B4R, I have used the Expressif flash tool to blank the flash. I also used the Arduino "eeprom_clear" sketch to clear the EEPROM.
Could this have permanently damaged the file system?

There is some good news as well, storing data into flash using the rEEPROM library works fine.
 
Upvote 0
Top