B4R Question connect 2 android devise to esp socket server

mzelecom

Member
hi .
i want to connect more than 1 devise to esp socket server at the same time but when i add server.Listen when 1 client connected it is not working and client disconnected.
how can i manage 2 or more than devise in server.?
 

mzelecom

Member
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Private server As WiFiServerSocket
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    
    RunNative( "SetIP" , Null )

    If wifi.Connect2 ("","") Then
        Log(wifi.LocalIp )
        Else
        Log("error")
    End If
    server.Initialize(48569, "server_NewConnection")
    server.Listen
End Sub

Sub Server_NewConnection (NewSocket As WiFiSocket)
    Log("Client connected")
    Private astream As AsyncStreams
    astream.Initialize(NewSocket.Stream, "astream_NewData", "astream_Error")
    server.Listen
End Sub

Sub AStream_NewData (Buffer() As Byte)
    Dim a As ByteConverter
    
    Dim data As String=   a.StringFromBytes(Buffer)
    Log(data)
    
End Sub

Sub AStream_Error
    Log("Error")
    server.Listen
    
End Sub

#if C
  void SetIP(B4R::Object* o) {

  IPAddress ip(192, 168, 1, 55);  // (Replace xxx with desired IP)
  IPAddress gateway(192, 168, 1, 1);
  IPAddress subnet(255, 255, 255, 0);
  WiFi.config(ip, gateway, subnet);

  }
  #end if
 

mzelecom

Member
The server supports a single connection. You can create multiple server objects, listening to different ports.

Why do the clients need to stay connected? It will be simpler to read the data from the client, send the response and close the socket.

Another option is to use MQTT with an external broker.
i need to get data realtime .
i tested mqtt broker .but when i connected 1 devise and reconnected for about 6 time .the broker not accept new client .
i think it has some limit.
 

miker2069

Active Member
Licensed User
i need to get data realtime .
i tested mqtt broker .but when i connected 1 devise and reconnected for about 6 time .the broker not accept new client .
i think it has some limit.
How many concurrent connections are you looking to support?

Are you using a free mqtt service like cloudmqtt? Could be they are throttling your connections (so that you'd have to pay for it). I know for example i had an issue with the free plans on cloud mqtt services when my payload was large than 20KB. I deployed my own mosquito mqtt server and I've had no issues.

If you don't want to go that route, could you perhaps use UDP to broadcast back and forth with the client? Then you don't have to worry about maintaining a persistent connection
 
Top