Android Question Socket client know when server disconnect or stop

max123

Well-Known Member
Licensed User
Longtime User
Hi all,

My B4A app control a 3D Printer by connecting to an ESP32 over TCP socket.

My app is a client and ESP32 is a server.

The ESP32 is connected to a 3D printer over USB cable with USB Host Shield, so ESP32 acts as true host (the same than pc) and can control a 3D Printer, even receive feedback that is sent to my app. ESP32 even have uSD card slot where read gcode files transferred from my app.

All this works. Now the problem is:
If I connect my app (client) to ESP32 (server), when I disconnect or just close the app, ESP32 know a disconnection without problems.

This do not happen on client side, if I switch off ESP32 while client is connected, my app do not know that server goes offline.

I use AsyncStreams and SocketAStreams_Terminated or SocketAStreams_Error do not fires at all.

Do this happen because it is a client and not a server ?
How I can know that server stay online or offline or just disconnect the client ?
I need to use a timer to continue ping the server to know it ? I want avoid to do this, there are other solutions ?

Many thanks
 
Last edited:

max123

Well-Known Member
Licensed User
Longtime User
Thanks for reply Erel.

I do not receive nothing, Terminated event not fires, even after minutes.
I want to know because this happen.

I have a connection button to change state from DISCONNECT to CONNECT in the Terminated event, but it remain
unchanged after server disconnect.

May it just get the disconnection if server side disconnect the client by software ?
Is different if I just switch off server side (ESP32) ?

The problem here is that because Terminated event is not fired, some stuffs remain unchanged , even the socket and stream remains open and in connected state.
When I next reconnect to ESP32 I can connect but have problems to send data, for now after this the only solution is restarting the app.

Thanks

B4X:
Sub SocketAStreams_Error
    LogMessage("Socket Astream Error: [" & LastException.Message & "]")

    SocketAStreams_Terminated
End Sub

Sub SocketAStreams_Terminated

    If SocketAStreams.IsInitialized Then SocketAStreams.Close
    If WiFiConn.IsInitialized Then WiFiConn.Close
 
    If Settings.CNCType = Settings.TYPE_CNC_PRINTER Then
        TempCheck.TempTimer.Enabled = False
    End If
 
    LockControls(True)
 
    btnStart.Value = 0
    btnStart.Enabled = True
    btnPause.Value = 0
    btnStop.Value = 0
    
    RadioSimulate.Enabled = True
    RadioUSB.Enabled = True
 
    btnRemoteConnect.Text = "CONNECT"
    btnRemoteConnect.Invalidate
 
    lblRemoteStatus.Text = "DISCONNECTED"
 
    LogMessage("DISCONNECTED  [" & txtIPAddress.Text.Trim & "]")
    ToastMessageShow("DISCONNECTED  [" & txtIPAddress.Text.Trim & "]", False)
End Sub
 
Last edited:
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
See this thread. It may be of some help:

 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Thanks @amino
I tried it but not works for me.
Error event sub is not fired like you, please, because you solved, you have a simple demo project that works for you to test with ?
May I do something wrong. ?

Thanks for help
 
Last edited:
Upvote 0

hatzisn

Expert
Licensed User
Longtime User

Just out of curiosity, which is your printer and how are you able to know the protocol with the printer to upload the GCODE or is it just "write the GCODE in serial" ?
 
Last edited:
Upvote 0

teddybear

Well-Known Member
Licensed User
This do not happen on client side, if I switch off ESP32 while client is connected, my app do not know that server goes offline.

I use AsyncStreams and SocketAStreams_Terminated or SocketAStreams_Error do not fires at all.
I don't know how you connect to ESP32 Server, but the AStreams_Error event will be fired when server is down
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Just out of curiosity, which is your printer and how are you able to know the protocol with the printer to upload the GCODE or is it just "write the GCODE in serial" ?
Hi @hatzisn I reply to your questions, and may explain better what I doing.... Just to be clear, my 3D printer is Creality Ender 3 but this should work on most of 3D printers. The app can control 3D printer in two ways, as USB serial host (with USB OTG cable) or over network with ESP8266/ESP32 or Raspberry PI (tested on PI Zero W) as server connected to a 3D printer over USB. The Raspberry code is writed with B4J, it not only works on Raspberry, but even on desktop pc connected to printer over USB. Anyway ESP32 have uSD card, the gcode file is inside Android device, when user use USB, the file is read from Android and then the app send line by line over USB to a printer. When the user print online (over network) the app send the gcode file to ESP or Raspberry or Pc, it save and then acts as host over USB. In this case ESP or RPI or PC is completely indipendent, like any app to print out, is an Host, even do feedback over network to Android App. Is a bit complex, I started the project iin 2018 and I'm near to finish.
 
Last edited:
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
I don't know how you connect to ESP32 Server, but the AStreams_Error event will be fired when server is down
Hi @teddybear do you have ESP32 ?
Do you want to try ? I prepared a simple B4A project for you.

I ensure you that while client (Android) is connected to a server (ESP32), if I switch off ESP32, at this time AStreams_Error sub will not fires at all, I'm worked on this 3 days and not found a solution.

If you want to try I will post my simple B4A project with just one button to connect and one to disconnect the client.

For ESP32 side you can just try any simple TCP server that listen on.
 
Last edited:
Upvote 0

hatzisn

Expert
Licensed User
Longtime User

Please do not get me wrong.... I believe you are born to be a politician... You answered using 7 lines of text, saying a lot around it but never getting to the point.... The point was: do you just write the GCODE via the USB to the printer or is there some kind of protocol (i.e. - I am about to send 10 bytes. Is it Ok? Ok I am sending it! Ok, received? Ok, I am sending the next 10 bytes etc...).
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Ah ok, sorry.... No particular protocol. The app send one gcode line over USB and wait for ack 'ok' to arrive to send next line. If 'ok' don't arrive the app stops. It is called Ping-Pong.
There 's another alternative (I've implemented but never tested yet) that is just wait some time after any gcode line, without wait ack.
 
Upvote 0

XorAndOr

Active Member
Licensed User
Longtime User
I tried on my project with esp8266 in socket
1) started esp8266 access point....ok
2) connected with android....ok
3) disconnected power to the esp8266
4) android app still marked "Connected"
5) waiting for error..1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <--second
6) Android Log("TcpStream_Error") appears
7) at the end the event appears after about 20 seconds
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Many thanks for reply @XorAndOr , unfortunately this do not works for me with ESP32.

Please, can you try the attached project if works for you ?
For me Astream_Error is never fired when I switch off ESP32, I awaited 5 minutes but no, it do not fires at all.
May I do something wrong?

Thanks
 

Attachments

  • ClientEventDisconnect.zip
    14.9 KB · Views: 139
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Even on emulator with Android 12 does not works,
at this point seem to be ESP32 problem, but I just create a TCP server that listen on.
It is a multi-client, so accept max 3 connections, but for now I just connect
one client.

Note that while client is connected to a server, a server will continue to send
data that are 3D Printer temperatures, may it won't work because this ?

Now I will try with a simple ESP32 TCP server to see if it works.

Thanks
 
Upvote 0

XorAndOr

Active Member
Licensed User
Longtime User
I'll update the tests
1) test on esp8266 (I don't have esp32) sorry
2) app connect to server esp8266
3) unplug the esp8266 usb cable
4) I wait for log....
5) on emulator (bluestacks) error appears after 20 seconds
6) on smartphone error appears after about 3-4 seconds max
7) android 8.0

i have no idea about this problem
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Many thanks, at this point, you can upload the sane ESP8266 project so I test on ESP32 ?
You can or is your project ?

Thanks
 
Upvote 0

XorAndOr

Active Member
Licensed User
Longtime User
You can or is your project ?
it is a simple access point
android app is that of your project
B4X:
'MODE ESP ACCESS POINT SSID PASS
Sub AccessPointSsidPass(Tag As Byte)
    '    Connect Access Point pass
    If ESPAccessPoint.StartAccessPoint2("SSID","PASS") Then
        Server.Initialize(51043, "server_NewConnection")
        Server.Listen
        Log("Access Point Ready")     
    Else
        Log("AP Problem")
        Return
    End If
  
End Sub
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Grazie, ora so anche che sei italiano come me. Proveró con un semplice sketch.

Thanks, now I see that you are italian like me. I will try with a simple sketch.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…