B4R Question rESP8266WiFi connect fail times and error message

RJB

Active Member
Licensed User
Longtime User
I've been having problems with rESP8266WiFi so have done some testing as follows. The timeout periods seem to be excessive and there is an error message on retry. I can't find any way to reduce the timeout or to avoid the error message.

Test Code (modified 'blank' esp code):
Test code:
Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Dim ConnectStart As ULong
    Dim count As Int
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    'example of connecting to a local network
    connectloop(0)

End Sub

Sub connectloop(loopcount As Byte)
    'non-blocking delay - doesn't seem to help with error message
    If loopcount > 4 Then Return
    Log("Connect: ", loopcount)
    Connect
    CallSubPlus("connectloop", 10000, loopcount + 1)
    
End Sub
    
Sub Connect
    ConnectStart = Millis
'    wifi.ConnectAsync("SSID", "Password", 0, Null, "wifi_Connected")
    If wifi.Connect2("SSID", "Password") Then
        Log("Connected to network after ", (Millis - ConnectStart), " Millis")
    Else
        Log("Failed to connect to network after", (Millis - ConnectStart), " Millis")
        'wifi.Disconnect 'doesn't make any difference
    End If
End Sub

Sub wifi_Connected(Success As Boolean)
    If Success Then
        Log("Async success after ", (Millis - ConnectStart), " Millis")
    Else
        Log("Async failed after ", (Millis - ConnectStart), " Millis")
    End If

End Sub

Results:
Sync
----
SSID and Password correct: connection between 600 and 1500 mS
SSID incorrect: failed at about 15000 mS
Password incorrect: failed at 75000 mS, i.e. 75 seconds!
Both incorrect: failed at about 15000 mS

15 seconds/ 75 seconds seem unnecessarily long!

Ansync
------
SSID and Password correct: connection at about 150 mS
SSID incorrect: failed at about 2500 mS
Password incorrect: failed at between 450000 mS and 900000 mS, i.e. up to 15 minutes!!
Both incorrect: failed at about 2500 mS

7.5/ 15minutes is just unbelievable !

Error message on each retry: "E (107629) wifi:sta is connecting, return error" , the number increases by 25000 (i.e. must be a 'millis' value, 25 Seconds, 15 S to fail + 10 S waiting for retry) each time:

Logs:
AppStart
Connect: 0
Failed to connect to network after15098 Millis
Connect: 1
E (32611) wifi:sta is connecting, return error
Failed to connect to network after15001 Millis
Connect: 2
E (57617) wifi:sta is connecting, return error
Failed to connect to network after15001 Millis
Connect: 3
E (82623) wifi:sta is connecting, return error
Failed to connect to network after15001 Millis
Connect: 4
E (107629) wifi:sta is connecting, return error
Failed to connect to network after15001 Millis

Any suggestions? Am I doing something wrong?
 

peacemaker

Expert
Licensed User
Longtime User
rESP8266WiFi
Check the source code of rESP8266WiFi.cpp lib: https://github.com/AnywhereSoftware/rESP8266WiFi/blob/master/rESP8266WiFi.cpp

Including:

C++:
    void B4RESPWiFi::ConnectAsync(B4RString* SSID, B4RString* Password, Long Channel, ArrayByte* bssid, SubVoidBool ConnectedSub) {
        this->connected = ConnectedSub;
        FunctionUnion fu;
        fu.PollerFunction = checkForConnected;
        pnode.functionUnion = fu;
        pnode.tag = this;
        if (pnode.next == NULL) {
            pollers.add(&pnode);
        }
        WiFi.begin(SSID->data, (Password != NULL && Password->getLength() > 0)? Password->data : NULL, Channel,
            bssid != NULL ? (uint8_t*)bssid->data : NULL, true);
    }
    
    //static
    void B4RESPWiFi::checkForConnected(void* b) {
        B4RESPWiFi* me = (B4RESPWiFi*) b;
        delay(1);
        if (WiFi.status() != WL_IDLE_STATUS && WiFi.status() != WL_DISCONNECTED &&  WiFi.status() != WL_CONNECTION_LOST &&  WiFi.status() != WL_NO_SSID_AVAIL) {
            me->connected(WiFi.status() == WL_CONNECTED);
            pollers.remove(&me->pnode);
        }
    }
 
  • Like
Reactions: RJB
Upvote 0

RJB

Active Member
Licensed User
Longtime User
Thanks for the suggestion but I'm afraid it means absolutely nothing to me! I use B4R to avoid having to get into that sort of detail.
I don't see anything about a timeout period or the error message, but as I say I don't understand a word of it!
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
Thanks.
I hadn't seem that thread. It explains some things (e.g. 15/ 75 seconds) and gives me some ideas to experiment with.
The problem with changing the code in someone else's source/ libraries is that the changes will be lost when they make updates, won't they?
My current problem is with the non-async (blocking) code so I'll experiment some more.
Thanks for your help
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Do not forget to publish any useful experiment results and tested code example...
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
Of course!

Adding the 'RunNative("StopAutoReconnect",Null)' gets ride of the error message.
I can't experiment with 'RunNative("StopAutoConnect",Null)' as apparently "class WiFiClass' has no member named 'setAutoConnect';"

Changing 'return WiFi.waitForConnectResult() == WL_CONNECTED;' to 'return WiFi.status() == WL_CONNECTED;' in rESP8266WiFi.cpp reduces the fail time from 75 seconds to 15 seconds which is usable - but I don't know if that breaks anything else and it will obviously get lost on update/ installation on another pc. Perhaps Erel could help with that?

The only change to the test code is the addition of the 'RunNative("StopAutoReconnect",Null)' after 'Log("AppStart")' plus the c code at the end:

B4X:
#if C
  void SetSTA(B4R::Object* o) {
  WiFi.mode(WIFI_STA);
  }
  void EnAutoReconnect(B4R::Object* o) {
  WiFi.setAutoReconnect(true);
  }
  void StopAutoReconnect(B4R::Object* o) {
  WiFi.setAutoReconnect(false);
  }
  void EnAutoConnect(B4R::Object* o) {
//  WiFi.setAutoConnect(true);
  }
  void StopAutoConnect(B4R::Object* o) {
 // WiFi.setAutoConnect(false);
  }
  void EnPersistent(B4R::Object* o) {
  WiFi.persistent(true);
  }
  void StopPersistent(B4R::Object* o) {
  WiFi.persistent(false);
  }
#end if
 
Upvote 0
Top