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.

New Connection
GET /set HTTP/1.1
Host: 192.168.4.1
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Inse
Get Request
Set Page
Found 7 Networks
Adding Option: BTHub5-RSNF
Adding Option: BTWifi-with-FON
Exception (3):
epc1=0x401000b7 epc2=0x00000000 epc3=0x00000000 excvaddr=0x4001048c depc=0x00000000
ctx: cont
sp: 3ffef850 end: 3ffefc70 offset: 01a0
>>>stack>>>
3ffef9f0: 00000009 0000000e 3fff14c6 402061c4
3ffefa00: 40211bca 00000001 3ffeec50 3ffeeb64
3ffefa10: 00000010 3ffefab0 00000000 4010053d
3ffefa20: 3ffefa60 00000002 00000000 402010b7
3ffefa30: 3ffe8514 00000002 3fff14dc 3ffefb60
3ffefa40: 00000010 3ffefab0 3ffefab0 402058fc
3ffefa50: 3fff1e26 00000008 3ffefab0 4020594b
3ffefa60: 3ffe8468 00000000 3ffefab0 4020597d
3ffefa70: 3ffeea4c 3ffeea74 3ffefab0 402059ce
3ffefa80: 3fff1e26 3ffeea74 3ffefab0 4010068c
3ffefa90: 3ffe8468 00000000 3ffefab0 402048a4
3ffefaa0: 3ffeea4c 00000009 3ffe8514 40203f68
3ffefab0: 00000000 00000000 00000000 40201efe
3ffefac0: 3ffeea4c 3ffeea74 3ffe8514 40203ba3
3ffefad0: 402329d4 00e478ef 00000000 4000050c
3ffefae0: 00000000 40102d00 00000000 00000000
3ffefaf0: 00000000 3ffeea00 00000000 00000000
3ffefb00: 00000000 00000000 00000000 ffffff00
3ffefb10: 00000000 00000000 00000000 00000000
3ffefb20: 00000000 00000000 00000000 ffffff00
3ffefb30: 00000000 3fffc600 00000000 3ffeea00
3ffefb40: 00000000 3fffda00 00000000 00000000
3ffefb50: 00000000 00000000 3ffe8644 ffffff01
3ffefb60: 3ffe8638 3ffee901 3ffe8514 40204d01
3ffefb70: 3ffe862c 00000001 3ffe8514 40205e01
3ffefb80: 3ffe8608 3fffc601 3ffe8514 40205701
3ffefb90: 3ffe85f0 3ffeec01 3ffe8514 40205701
3ffefba0: 3ffe85dc 00000001 3ffe85d4 3fff1301
3ffefbb0: 3ffe85cc 00000001 3ffe85c8 40205e01
3ffefbc0: 3fff13ac 00000064 3ffee94c 40205dda
3ffefbd0: 0000006c 00000002 00000002 00000006
3ffefbe0: 00000000 3ffeea30 3ffeea4c 0000006c
3ffefbf0: 3ffee784 3fff13ac 3ffeea4c 40201e80
3ffefc00: 00000000 00000000 000003e8 3ffee780
3ffefc10: 00000000 3fff1c44 3ffe86ec 3ffeec44
3ffefc20: 3fffdad0 3fff139c 3ffee7a4 40202a98
3ffefc30: 3fffdad0 3ffee7ac 00000000 40202aff
3ffefc40: feefeffe 00000000 3ffeec3c 40204143
3ffefc50: feefeffe feefeffe feefeffe 40205b0c
3ffefc60: feefeffe feefeffe 3ffeec50 40100718
<<<stack<<<
ets Jan 8 2013,rst cause:2, boot mode:(1,7)
ets Jan 8 2013,rst cause:4, boot mode:(1,7)
wdt reset

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:

New Connection
GET /set HTTP/1.1
Host: 192.168.4.1
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Inse
Get Request
Set Page
Found 6 Networks
Adding Option: BTHub5-RSNF
Adding Option: BTWifi-with-FON
Adding Option: BTWifi-X
Adding Option: TALKTALK-01A4C8
Adding Option: BarkerNet
Adding Option: ComWIFI
1
2
Exception (28):
epc1=0x40204d2e epc2=0x00000000 epc3=0x00000000 excvaddr=0x000001f4 depc=0x00000000
ctx: cont
sp: 3ffef9a0 end: 3ffefc50 offset: 01a0
>>>stack>>>
3ffefb40: 3fff0d94 3ffee92c 00000000 40204d1d
3ffefb50: 3fff1994 3fff1998 3ffee92c 40205e20
3ffefb60: 40105900 007e1f4f 3ffee92c 40205747
3ffefb70: 3ffee190 3ffeeaa8 00000190 4020578b
3ffefb80: 00000000 3ffeec24 40205a50 3fff138c
3ffefb90: 3ffee90c 000084b0 00000000 40205da8
3ffefba0: 00000000 00000009 3fff0504 40205d6a
3ffefbb0: 00000000 3fff137c 3ffee92c 40205e06
3ffefbc0: 00000000 3ffeea10 3ffeea2c 00000000
3ffefbd0: 3ffee764 3fff138c 3ffeea2c 40201e40
3ffefbe0: 00000000 00000000 000003e8 3ffee760
3ffefbf0: 00000000 3fff0504 3ffe86d4 3ffeec24
3ffefc00: 3fffdad0 3fff137c 3ffee784 40202a98
3ffefc10: 3fffdad0 3ffee78c 3fff04d4 40202aff
3ffefc20: feefeffe 00000000 3ffeec1c 402040d3
3ffefc30: feefeffe feefeffe feefeffe 40205a9c
3ffefc40: feefeffe feefeffe 3ffeec30 40100718
<<<stack<<<
ets Jan 8 2013,rst cause:2, boot mode:(1,6)
ets Jan 8 2013,rst cause:4, boot mode:(1,6)
wdt reset

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
Top