Hi,
I'm trying to cover the situation where the router fails then recovers, i.e. it's power goes off then back on.
An ESP32 monitors a ping message and, if it doesn't arrive in time, checks the router connection then loops, trying to reconnect.
if I use WiFi.Connect then it works OK but I don't want the code blocked while it waits (up to 15 seconds).
Using WiFi.ConnectAsync to avoid doesn't work, the 'connected' sub is called immediately (2mS) with 'success' set to fail.
I can't see any way to set the wait time.
If I put a delay (3000 mS) immediately after the WiFi.ConnectAsync then it works OK, but, of course, it blocks again.
The stripped out code is given below.
Is anybody able to help with what I am doing wrong?
Thanks
I'm trying to cover the situation where the router fails then recovers, i.e. it's power goes off then back on.
An ESP32 monitors a ping message and, if it doesn't arrive in time, checks the router connection then loops, trying to reconnect.
if I use WiFi.Connect then it works OK but I don't want the code blocked while it waits (up to 15 seconds).
Using WiFi.ConnectAsync to avoid doesn't work, the 'connected' sub is called immediately (2mS) with 'success' set to fail.
I can't see any way to set the wait time.
If I put a delay (3000 mS) immediately after the WiFi.ConnectAsync then it works OK, but, of course, it blocks again.
The stripped out code is given below.
Is anybody able to help with what I am doing wrong?
Thanks
B4X:
'loops = 1 to 250. > 250 = infinite loops. 0 = do nothing
private Sub RouterConnectAsync(Loops As Byte)
If Loops = 0 Then
ReceiveWatchdog.Enabled = True
Return 'do nothing or 'loop countdown' has reached 0
End If
ReconnectLoops = Loops
Dim SSID As String = ReadSSID
Dim Password As String = ReadP
watchstart2 = Millis
Main.eWiFi.ConnectAsync(SSID, Password, 0, Null, "wifi_Connected")
' Delay(3000)
' Dim x As Boolean = Main.eWiFi.Connect2(SSID, Password)
' If x Then
' WiFi_Connected(True)
' Else
' WiFi_Connected(False)
' End If
End Sub
Sub WiFi_Connected(Success As Boolean)
Log("WiFi_Connected: ", Success, " after: ", (Millis - watchstart2), "mS")
If Success Then
SetFixedIP(ReadClientIP) 'in case the router tries to change the IP
AsyncReceiveServer.Listen
ReceiveWatchdog.Enabled = True
Return
End If
'loop, waiting for wifi
If ReconnectLoops > 250 Then
CallSubPlus("RouterConnectAsync", 1000, ReconnectLoops) 'infinite tries
Else
CallSubPlus("RouterConnectAsync", 1000, (ReconnectLoops - 1)) 'loop tries
End If
End Sub
Sub ReceiveWatchdog_Tick
Log("Receive WDOG Tick")
ReceiveWatchdog.Enabled = False
If Not(Main.eWiFi.IsConnected) Then
CallSubPlus("RouterConnectAsync", 1, 255) 'infinite tries
Else
ReceiveWatchdog.Enabled = True
End If
End Sub