B4R Question ESP8266 error when listing Networks

barx

Well-Known Member
Licensed User
Longtime User
Hi guys and girls

I know the ESP8266 is a bit of an unknown here, but just chucking this out there in case anybody has had (and overcome) the issue before.

I have a sub to list the detected Wifi Networks on a web page and send it back to the user for selection.

B4X:
Sub astream_NewData (Buffer() As Byte)
    Log(Buffer)
    If bc.IndexOf(Buffer, "GET") <> -1 Then 'we have a GET request to process
        Log("Get Request")
       
        If bc.IndexOf(Buffer, "/set?") <> -1 Then
            'save details
        else If bc.IndexOf(Buffer, "/set") <> -1 Then
            'Show Settings page
            Log("Set Page")
            BeginOKResponse
            astream.Write("<h1>Settings</h1>").Write(CRLF)
            astream.Write("<div class=""section"">").Write(CRLF)
            astream.Write("<form action=""/set"" method=""get"">").Write(CRLF)
            astream.Write("<select>").Write(CRLF)
            'get available networks
            Dim NetworkCount As Int = Main.Wifi.Scan
            Log("Found ", NetworkCount, " Networks")
            For i = 0 To NetworkCount - 1
                Log("Adding Option: ", Main.Wifi.ScannedSSID(i))
                astream.Write("<option>").Write(Main.Wifi.ScannedSSID(i)).Write("</option>")
                'astream.Write("<option value=""").Write(Main.Wifi.ScannedSSID(i)).Write(""">").Write(Main.Wifi.ScannedSSID(i)).Write("</option>").Write(CRLF)
            Next
            Log(1)
            astream.Write("</select>").Write(CRLF)
            astream.Write("Password: <input type=""password"" name=""password""></input>").Write(CRLF)
            astream.Write("<input type=""submit"" value=""Save""").Write(CRLF)
            astream.Write("</form>").Write(CRLF)
            astream.Write("</div>").Write(CRLF)
            Log(2)
            EndResponse
        Else If bc.IndexOf(Buffer, " / ") <> -1 Then
            'Home page
            Log("Home Page")
            BeginOKResponse
            astream.Write("home page").Write(CRLF)
            EndResponse
        Else
            'Page not found
            Log("No Page")
            astream.Write("HTTP/1.1 404").Write(CRLF)
        End If
        CallSubPlus("CloseConnection", 200, 0)   
    End If
   
End Sub

When the above code is run, I get the following in the log.


So, the interesting part is, there are 7 networks detected but the device craps out after looping 2 (sometimes 3 or 4)

If I comment out the astream.write() in the For loop and just watch the logs I get:


So, this time it finds 6 (number 7 is very weak and sometimes shows, sometimes not) and it loops through all 6 (Logs the name). But still craps out.

Any clues?
 

barx

Well-Known Member
Licensed User
Longtime User
Note: commenting out the For Loop, the error goes and the page renders ok (with a blank Select Option List)
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
I think that most B4R developers use ESP8266.

Try to increase #StackBufferSize to 1000.

I increased to 800 and it seem to run ok so 1000 will be better I guess. Is there an adverse effect to this?

Am I having to increase the StackBuffer due to the astream.write("")'s?
Is there a better way to do the above? would concatinating a string and then doing 1 .write() save anything??
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Is there a better way to do the above? would concatinating a string and then doing 1 .write() save anything??
No. Avoid using strings and especially avoid concatenating strings.

Is there an adverse effect to this?
It means that 1000 bytes are allocated for the stack buffer: https://www.b4x.com/android/forum/threads/memory-variables-and-objects.65672/#content
It is not problematic on ESP8266. You can make it larger if needed.

Is there a better way to do the above?
I wouldn't change anything except of the buffer size.

It happens because of this call which uses the stack buffer to store the string:
Main.Wifi.ScannedSSID(i)

The memory is released once the sub ends. You can move it to a different sub and call it:
B4X:
Sub WriteSSID(i As Int)
 AStream.Write(Main.Wifi.ScannedSSID(i))
End Sub

This way the memory will be released after each call.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…