Android Question Wifi disconnects

mterveen

Member
Licensed User
Longtime User
Connecting android/b4a application as client to an esp32 as the server using the MLScan/MLWifi library with a regular socket on the b4a side. i can connect easily to the esp32 server (softAP) and transmit data/files back and forth, but the wifi connection randomly disconnects. it "appears" it may be every 60 seconds or so but it is not that specific. it will even disconnect during a file transfer as well as sitting idle. i am trying to determine which side is causing the problem, is the client dropping or is the server timing out? for testing the phone/esp32 are withing 3 feet of each other. here is the b4a message when the astream errors out:

********b4a logger astream error:*********
android.system.ErrnoException: recvfrom failed: ETIMEDOUT (Connection timed out)
at:1517615820075

and the corresponding log when the esp times out:

********esp error **********************
Opening port
Port open
I (3139) wifi: wifi firmware version: 708a055
I (3140) wifi: config NVS flash: enabled
I (3140) wifi: config nano formating: disabled
I (3147) wifi: Init dynamic tx buffer num: 32
I (3147) wifi: Init data frame dynamic rx buffer num: 64
I (3147) wifi: Init management frame dynamic rx buffer num: 64
I (3151) wifi: wifi driver task: 3ffd4780, prio:23, stack:4096
I (3156) wifi: Init static rx buffer num: 10
I (3160) wifi: Init dynamic rx buffer num: 0
I (3165) wifi: wifi power manager task: 0x3ffd94bc prio: 21 stack: 2560
I (6440) wifi: mode : softAP (30:ae:a4:04:93:99)
wifi event:13
AP Started
IP address:192.168.4.1
IP Mac address:30:AE:A4:04:93:99
Host name:espressif
I (13626) wifi: n:1 0, o:1 0, ap:1 1, sta:255 255, prof:1
I (13626) wifi: station: 08:d4:2b:10:9c:06 join, AID=1, g, 20
wifi event:15
AP station connected to esp32
time (ms)=10386
client connected:false
server has client:false
---loop until client found---
server has client:true
server is available:true
found client
time (ms)=13149
client.connected:true
I (77912) wifi: station: 08:d4:2b:10:9c:06 leave, AID = 1
I (77913) wifi: n:1 0, o:1 0, ap:1 1, sta:255 255, prof:1
wifi event:16
AP station disconnected from esp32
time (ms)=74673
client connected:true
server has client:false

Port closed
----------------------------------------------------

Question:
1) what does the b4a error message mean? how do i determine which side is causing the issue?
2) is it possible the phone is continually searching for the strongest signal and will automatically switch to my home router? if so can i prevent this?

B4X:
Sub ConnectToSocket
    Log("connecting to socket at " & DateTime.Now)
    Log(espWifi.WifiSSID)
    Log(espWifi.WifiStrength)
    Log(espWifi.WifiLinkSpeed)
    If espsocket.IsInitialized Then espsocket.Close
    Sleep(250)
    If espWifi.WifiSSID = "mpscilink" Then
        Log("connect to socket at " & DateTime.Now)
        If espWifi.isSavedWifiAP(espWifi.WifiSSID) = False Then
            Log("save mpscilink to phone")
        End If
        If espWifi.isWifiHeldOn = False Then
            Log("turn on wifi hold with lock")
            Dim hld As Boolean = espWifi.holdWifiOn2(espWifi.WIFI_MODE_FULL_HIGH_PERF)
            Log("holdwifion2 results:" & hld)
        Else
            Log("wifiheldon already set")
        End If
        Dim espsocket As Socket
        espsocket.Initialize("socket")
        espsocket.Connect(esphost,espport,10000)   'esphost=192.168.4.1  espport=21351
    Else
        CallSubDelayed2(Main,"mainscreenmsg","connection to mpscilink failed")
        Log("couldn't connect to mpscilink. using default")
    End If
End Sub

Sub socket_NewConnection (Successful As Boolean, NewSocket As Socket)
    Log("new ss connection")
End Sub

Sub socket_Connected (Successful As Boolean)
    Log("socket connected is " & Successful & " at " & DateTime.Now)
    espconnected = Successful
    If espconnected Then
        ph.KeepAlive(True)
        Log("connected")
        CallSubDelayed2(Main,"mainscreenmsg","connected to mpscilink")
        Log("start service")
    Else
        CallSubDelayed2(Main,"mainscreenmsg","connection to mpscilink failed")
    End If
End Sub
 

Didier9

Well-Known Member
Licensed User
Longtime User
Are you connected through a residential router/access point or is the ESP32 configured as a soft access point?
I have noticed that when connected directly to a (different) WiFi module as access point (with no internet access), the connection is not always reliable.

I have documented my experiments there:
https://www.b4x.com/android/forum/t...p-wifi-when-away-from-home.80034/#post-507257

In my case, the connection is solid even when connected directly to the WiFi module as long as there is an Internet enabled router in the vicinity, even though I am not connected to it. When out in the country with no other WiFi signal, it drops after 30 seconds to a minute.

As of today, I have not resolved that issue. I now use Bluetooth in that environment.
 
Upvote 0

mterveen

Member
Licensed User
Longtime User
esp32 is setup as softAP. this is an android client app that interfaces with the esp32 located in a vehicle, so it generally will be away from any other APs. it is a port of an existing product using b4a and the arduino mega 2560 via bluetooth which works flawlessly. unfortunately, the esp32 classic bluetooth is not yet supported so i thought the softAP would be an even better solution. so far i'm wrong :)

p.s. my home router is available also, but in the startup routines in b4a i disconnect from the current AP (router) and reconnect to the esp32 AP. that works perfectly.
 
Last edited:
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
Have you tried the HC-05 Bluetooth module? I have been pretty lucky with it, and the price certainly is right.
 
Upvote 0

mterveen

Member
Licensed User
Longtime User
i've used both the hc-05 and hc-06 in my original arduino product. i have not used an external BT module with the esp32. the whole purpose of the esp32 was it's speed and the fact it incorporated wifi and bt. hopefully i can resolve the wifi issue since that is my preference.
 
Upvote 0

mterveen

Member
Licensed User
Longtime User
i'll give that a try as well as using some different devices. esp32 support is fairly limited so that is a bit of a problem - others seem to have the same problem to varying degrees. i'm trying to add more descriptive error reporting so we'll see. as noted in my first post, i'm just trying to determine which side of the connection is causing the problem.

is there a way to "lock" in an AP in the android OS in addition to what i've tried already in the b4a code?
 
Upvote 0

mterveen

Member
Licensed User
Longtime User
followup - i did not turn off the router but i did manually "forget" the home router network and turned off autoconnect before starting the app/esp32 and it worked flawlessly! woohoo. now i need to see how to do that programmatically if needed. might be easier to just post a message/warning!! sure wish you could "lock" in an AP.
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
followup - i did not turn off the router but i did manually "forget" the home router network and turned off autoconnect before starting the app/esp32 and it worked flawlessly! woohoo. now i need to see how to do that programmatically if needed. might be easier to just post a message/warning!! sure wish you could "lock" in an AP.

Interesting, as it is practically the opposite of what I have experienced. I use devices with Android 4.4 through 6.0 and all pretty much work the same. They will not always automatically reconnect to an access point that does not have internet but eventually will if you "insist" (by clicking "connect anyways" every time when Android says there is no Internet access (see note below). Since I (and my customer) have been using the same Android devices with many (several dozens of) WiFi modules, I can say for certain that they will reconnect to the softAP even if there is an Internet connected router in range, and stay connected for a long time (several days at a time).

What I have also found, and sounds very crazy but I have proven it several times is that Android will not stay connected to the WiFi module when I am away from any other Internet carrying WiFi traffic.

Example: placing my Android device and my SoftAP, WiFi enabled microcontroller in the car, all works fine when I am in my driveway within range of my residential and Internet connected WiFi router. It will still work fine when I drive up my street, way away from my own router but in range of my neighbor's. I can park my car 500 feet up the street and Android stays connected to my microcontroller's WiFi module.
Then if I keep driving outside the neighborhood on the highway, away from any house, it disconnects from the SoftAP that is in the car. In order to reconnect, I have to reboot the Android device, and then it disconnects again after about 30 seconds.
Then as I drive back into my street, I can reconnect again and it stays connected.

The next step was to also put a Netgear router in the car with broadcasting turned on. Of course, the Netgear is not connected to the Internet. It still did not work.

The last test I need to do is to set my mobile phone as an access point in the car since Verizon decided it was now OK for me to do that, and again connect the Android tablet to my microcontroller's SoftAP. That will be a situation similar to home, except that nothing will be connected to the phone's access point. If I need to, I can bring another tablet in the car and connect it to the phone and run YouTube or some other video or music streaming app on the tablet to make sure there is traffic.

The WiFi modules I use are WIFI232-T and C215 made by USR Technology in Shenzen, China (usriot.com). I have bought several hundred of them over the last ~5 years and they work fine in all respects. I believe the issue is with Android since I have to reboot the Android device but not my WiFi module.

Note: I know it sounds crazy that Android would know that there is Internet traffic going on in the background even though it is not connected to the access point, but considering that Android can tell when you are trying to connect to an access point that does not have Internet access *before* you actually connect to it, that means that it is snooping on the traffic before actually being connected. So while it is not clear why Android would care if there is Internet traffic in the background or not, considering the amount of behind the wall snooping that is done on our data by everybody, maybe it is not too crazy.
If I remember, netstumbler is also capable of telling you if an access point in range is capable of internet access. Not sure how they do that, maybe it is in the visible/non-encrypted metadata part of the WiFi protocol?
 
Last edited:
Upvote 0

Vahid_Maz

Member
Worth testing with your home router turned off. Android can try to switch to a different network when the current network doesn't have an internet connection.
I had the same problem. Of course, on some versions of Android.
But instead of using this command:

WiFi.connectWifiAP("Your SSID",2,"Your Password",10000)

I used the following command:

WiFi.saveWifiAP("Your SSID",2,"Your Password",True,10000)

I do not have a problem right now.
 
Upvote 0

Vahid_Maz

Member
I had the same problem. Of course, on some versions of Android.
But instead of using this command:

WiFi.connectWifiAP("Your SSID",2,"Your Password",10000)

I used the following command:

WiFi.saveWifiAP("Your SSID",2,"Your Password",True,10000)

I do not have a problem right now.
Note: You may need to forget your ESP SSID once.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0
Top