B4R Question how to free memory and stack ?

peacemaker

Expert
Licensed User
Longtime User
Hi, All

My app for ESP8266 has a huge sub preparing JSON to send to a server. It makes the string only if "#StackBufferSize: 700", bigger value does not help.
So JSON string is made OK, but HttpJob cannot be posted - error. It works if to send some short text, the server responds OK.


B4X:
Public Sub Send_Save_Log
    HttpJob.Initialize("API_SAVE_logs")
    Dim text() As Byte = "{""action"":""savearray"",""scanner"":""%SCANNERNAME%"",""key"":""%KEY%"",""timestamp"":%TIMESTAMP%,""dbtable"":""logs"",""data"":{""0"":{""scanner_name"":""%SCANNERNAME%"",""time"":""%TIME%"",""stamp"":""mcu"",""text"":""%TEXT%"",""type"":""%TYPE%""}}}"
    Dim text1() As Byte = others.ReplaceString(text, "%SCANNERNAME%", Main.bc.HexFromBytes(others.MacArray))
    Dim text2() As Byte = others.ReplaceString(text1, "%KEY%", API_KEY)
    Dim tz As String = others.TimeZone
    Dim text3() As Byte = others.ReplaceString(text2, "%TIMESTAMP%", tz)
    Dim text4() As Byte = others.ReplaceString(text3, "%TIME%", tz)
    Dim text5() As Byte = others.ReplaceString(text4, "%TEXT%", others.Collect_Info)
    Dim b(1) As Byte
    b(0) = 0
    Dim text6() As Byte = others.ReplaceString(text5, "%TYPE%", b)
    Log("text = ", text6)

    HttpJob.Post(URL, text6)
End Sub

B4X:
trying to connect to: server.com port: 80 ssl: 0

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Exception (9):
epc1=0x402370d3 epc2=0x00000000 epc3=0x00000000 excvaddr=0x0204011b depc=0x00000000

So, how to optimize memory usage in this situation ?
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
Thanks, correct ! If to separate partially the mandatory calls in a separate sub - it helps to execute all without error.

UPD: but this helps not so much - if to move the call of this sub to another place (more correct) - again error.
It's strange - available lots of memory and stack...
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Finally most troubleless solution is ... manual JSON generation:
B4X:
Public Sub Send_Save_Log
    If Main.InternetConnected = False Then Return
    
    Dim json1 As String = JoinStrings(Array As String("{""action"":""savearray"",""scanner"":""", Main.bc.HexFromBytes(others.MacArray), QUOTE))
    Dim json2 As String = JoinStrings(Array As String(",""key"":""", API_KEY, QUOTE))
    Dim tz As String = others.TimeZone * 1000
    Dim json3 As String = JoinStrings(Array As String(",""timestamp"":", tz))
    Dim json4 As String = JoinStrings(Array As String(",""dbtable"":""logs"",""data"":{""0"":{""scanner_name"":""", Main.bc.HexFromBytes(others.MacArray), QUOTE))
    Dim json5 As String = JoinStrings(Array As String(",""time"":""", tz, QUOTE))
    Dim json6 As String = JoinStrings(Array As String(",""stamp"":""", tz, QUOTE))
    Dim json7 As String = JoinStrings(Array As String(",""text"":""", others.Collect_Info, QUOTE))
    Dim json8 As String = JoinStrings(Array As String(",""type"":""", "0", QUOTE, "}}}"))
    Dim text As String = JoinStrings(Array As String(json1, json2, json3, json4, json5, json6, json7, json8))
    
    Log("text = ", text)
    HttpJob.Initialize("API_SAVE_logs")
    HttpJob.AddHeader("Content-Type", "application/json;charset=UTF-8")
    HttpJob.Post(URL, text)
End Sub
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Maybee this
Yes, i saw this, but how can this help, if here needed a runtime various JSON-strings generation and using them in HttpJobs ? JSON is always a variable.
Trouble is the error in a sub that uses many calls of other subs.
 
Upvote 0
Top