So
@OliverA , is really instructive your reply, and you are 100% right on first point. ?
Seem to work as I supposed on post #3:
May it just get the disconnection if server side disconnect the client by software ?
Is different if I just switch off server side (ESP32) ?
Now may I know a bit better how it works, please correct me if I'm wrong here:
when switch off one side (the WiFi Network, not the connection by software or closing app), do no matter if it is a client or server, because I switch off the network it cannot notify other side (client or server). On ESP switch off it by removing power switch off the WiFi instantly, this is the same that switch off WiFi from Android.
This works the same even if ESP is the client that connect to Android server, right ?
Disconnect Android client (not tried) from ESP (by software) should fire AStream_Error, right ?
As your advices I did follow test:
- 1) Connect Android client to ESP server
- 2) Switch off Android WiFi, as you said ESP do not recognize disconnection
- 3) ESP recognize disconnection only next time Android try to reconnect to it
This is tested with real device (Android 6) and with emulator (Android 12) and do some result.
Next I did another test:
- 1) Connect Android client to ESP server
- 2) Reboot (or switch off/on) my router
- 3) ESP server get a regular disconnection after 5-6 seconds and notify it on printer LCD.
Android client do not recognize it
- 4) Rebooting the router require a bit of time, like 2-3 minutes
- 5) After the network returns my ESP server detect that Public IP is changed, it check every 3 ninutes by doing a web request, show a notify on a printer LCD (see attached image) and control a printer buzzer to do a long beep (3 seconds) indicating that IP Address is changed. Next ESP show on a printer LCD 5 seconds Local IP Address and 5 seconds Public IP Address, intermittent these in an'infinite cycle until the client connect (note that max 3 clients are accepted, managed it by code, but probably in final stage I just use 1 client), this way the user can see the IP Address to connect to. In local both works, outside only public works, eventually on Android app I can use ResolveHost to use a DDNS instead of IP Address. Note that both methods need a router port-forwarding that point to ESP.
- 6) Network returned but Android remains connected and AStream_Error is not fired, even after 30 minutes, never fired at all
For this:
Actually, the correct answer was already given to you in post #2
I know it from start, Erel's answer was good to me, but I asked if it is possible to do it other ways:
I need to use a timer to continue ping the server to know it ? I want avoid to do this, there are other solutions ?
but this seem to be an issue in certain cases so 'heartbeat' always should work, if you disconnect, if no network signal occour, if router switch off and so....
I end up to make server that ping (simply send ALIVE) to client/s but this is not too different.
I wanted avoid to do 'heartbeat', because ESP-Android already exchange others data, even I need to check if this influences when I upload over WiFi to ESP the gcode file, that exchange a lots of data, may works, but not sure 100%, I need to test, may false disconnections can occour.
Even doing this, if ESP disconnect, Android know a disconnection after 1-2 seconds, if app send data, or user upload a gcode file etc... in these 1-2 seconds probably I've some strange things.
Note that ESP have uSD card, so files are upload here, and note that I even wrote this for Raspberry using B4J, it upload files to uSD, the same server works on desktop device and it upload to PC ssd.
Many Thanks