I am finding that it takes a long time for an iOS browser (e.g Safari on iOS 14.8) to get the first page from a basic ESP8266 server. Sometimes can take more than 30s or even not get it at all. Having stripped the code to pretty much the bare minimum and with some judicious logging I have noticed that the delay occurs between the time of initial connection (Server_NewConnection triggered) and the point at which the Astream_NewData is triggered. In fact it looks like iOS closes the connection and retries before success. See code below:
I have had similar experiences with ESP32 devices
If I use AP mode and connect the iOS device to the ESP's AP then I don't seem to get this issue
Anyone have a clue?
Just add to my observations: The issue appears to occur only when the first connection from the browser is made. If I reload the page the server appears to respond to the next GET request very quickly. Even if I reset the ESP device but just click on reload page on the browser after the ESP has re-established the server, it still responds quickly. However if I close the browser down, restart it and then try and connect same long delay.
The typical log output from an attempted connection is:#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 3000
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Public wifi As ESP8266WiFi
Public Astream As AsyncStreams
Public server As WiFiServerSocket
Public st As ULong
End Sub
Private Sub AppStart
st=Millis
Serial1.Initialize(115200)
Log("AppStart")
Log("after start time: ",Millis-st)
RunNative("SetSTA", Null)
Log("Before connect time: ",Millis-st)
If Not(wifi.IsConnected) Then wifi.Connect2("xyz","12345678")
Log("After connect time: ",Millis-st)
Log("IP: ",wifi.LocalIp)
server.Initialize(80, "Server_NewConnection")
server.Listen
Log("After server start time: ",Millis-st)
End Sub
Private Sub Server_NewConnection (NewSocket As WiFiSocket)
Log("NEW server CONNECTION ********** at: ", Millis-st)
Astream.Initialize(NewSocket.Stream, "astream_NewData", "astream_Error")
Astream.MaxBufferSize=1000
End Sub
Private Sub Astream_NewData (Buffer() As Byte)
Log("Astream Data -------------------------------------- ", Millis-st)
Log("Length: ",Buffer.Length,CRLF)
Log(Buffer)
Log(CRLF)
Log("Time: ",Millis-st)
Astream.Write("HTTP/1.1 200").Write(CRLF)
Astream.Write("Content-Type: text/html").Write(CRLF).Write(CRLF)
Astream.Write("Hello World").Write(CRLF)
CallSubPlus("CloseConnection",200,0)
End Sub
Private Sub AStream_Error
Log("Disconnected at Astream Error sub")
server.Listen
End Sub
Private Sub CloseConnection(u As Byte)
Log("close connection sub at: ", Millis-st)
If server.Socket.Connected Then
Log("server was connected")
server.Socket.Stream.Flush
server.Socket.Close
Else
Log("server was not connected")
End If
End Sub
#if C
void SetSTA(B4R::Object* o) {
WiFi.mode(WIFI_STA);
}
#end if
Firefox from IOs device yields similar results. On the other hand if I try to connect to the same server using a browser on an Android device (native or Firefox) or from a LAN connected Windows PC, then the page is served up pretty much immediately.after start time: 1
Before connect time: 3
After connect time: 5854
IP: 192.168.2.18
After server start time: 5858
NEW server CONNECTION ********** at: 31911
Disconnected at Astream Error sub
NEW server CONNECTION ********** at: 62219
Astream Data -------------------------------------- 62231
Length: 372
GET / HTTP/1.1
Host: 192.168.2.18
Upgrade-Insecure-Requests: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 14_8 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Mobile/15E148 Safari/604.1
Accept-Language: en-gb
Accept-Encoding: gzip, deflate
Connection: keep-alive
Time: 62270
close connection sub at: 62471
server was connected
Disconnected at Astream Error sub
I have had similar experiences with ESP32 devices
If I use AP mode and connect the iOS device to the ESP's AP then I don't seem to get this issue
Anyone have a clue?
Just add to my observations: The issue appears to occur only when the first connection from the browser is made. If I reload the page the server appears to respond to the next GET request very quickly. Even if I reset the ESP device but just click on reload page on the browser after the ESP has re-established the server, it still responds quickly. However if I close the browser down, restart it and then try and connect same long delay.
Last edited: