Android Question UDP acknowledge - Wait For with timeout

Blueforcer

Active Member
Licensed User
Hello,

I have written a UDP client that is talking to an ESP8266. With UDP it can occur that packets do not arrive.
What's the best way to do it, that the app sends a command and if there is no answer, sends the data up to 3 times again until an answer is received?
I'm just breaking my head:)
 

Didier9

Active Member
Licensed User
If you need to make sure your data has been received, you should use TCP. That's what it's for.
UDP is precisely for those cases where you do not need to make sure the packet is received.
Trying to make UDP as robust as TCP in the application program is impractical and will invariably result in poor performance.

Now, what you are proposing to do (retry 3 times) will make UDP more robust, it is up to you to decide if it is worth it.
 

Blueforcer

Active Member
Licensed User
I am of course aware of the difference between TCP and UDP,
Unfortunately, the circumstances do not allow it to go to TCP because it is unicast only..
The user doesn't have the possibility to simply get to the IP address, so data is sent via UDP broadcast.
 
Last edited:

Erel

Administrator
Staff member
Licensed User
Untested code:
B4X:
Sub Send
   For i = 1 To 3
       usocket.Send(...)
       Wait For (CheckResponse(5000)) Complete (Success As Boolean)
       If Success Then Exit
   Next
   If i = 4 Then
       Log("Failed to send...")
   End If
End Sub

Sub CheckResponse (Timeout As Int) As ResumableSub
   Dim result(1) As Object
   Dim rs As ResumableSub = WaitForMessageHelper(result)
   Do While Timeout > 0 And rs.Completed = False
       Sleep(100)
       Timeout = Timeout - 100
   Loop
   If rs.Completed Then
       Dim packet As UDPSocket = result(0)
       'check the response
       Return True
   Else
       Return False
   End If
End Sub

Sub WaitForMessageHelper (Result() As Object) As ResumableSub
   Wait For usocket_PacketArrived (Packet As UDPPacket)
   Result(0) = Packet
   Return Null
End Sub
 

Didier9

Active Member
Licensed User
I am of course aware of the difference between TCP and UDP,
Unfortunately, the circumstances do not allow it to go to TCP because it is unicast only..
The user doesn't have the possibility to simply get to the IP address, so data is sent via UDP broadcast.
I have been doing IoT for a while and as far as I am concerned there is not one single solution that beats all others in all use cases (I am currently using different solutions in different cases) so I am curious about any new or different way to do that and would like to understand what you are doing.

So the client (assuming it is an Android phone or tablet) initiates communication with the ESP8266 via UDP broadcasts and the ESP8266 replies to the sender's address via UDP unicast? (since it has the address of the client)
What if you have more than one ESP8266 devices on the LAN?

I assume that you could assign a serial number to each ESP8266 device so that the initial query from the tablet would contain the serial number and only that unit would respond.
In a similar situation, I used the last 4 of the MAC address as a serial number, so the firmware in the ESP8266 would read it and you did not have to create a unique flash image for each target. I simply write these last 4 digits on the device's case so I know which is which :)
 
Last edited:

Blueforcer

Active Member
Licensed User
Thats right. ive build a RGB Matrix clock and want configure it with an app. You can change the UDP listen Port via App, so you could have more devices. With Erels Code its perfect now. No IP-adress blabla:)
27797395_1972359496131115_5233059043896114343_o.jpg 27661809_1972189392814792_1552235805_n.png
 

Didier9

Active Member
Licensed User
Thats right. ive build a RGB Matrix clock and want configure it with an app. You can change the UDP listen Port via App, so you could have more devices. With Erels Code its perfect now. No IP-adress blabla:)
View attachment 64417 View attachment 64418
Yes, a good solution for this application. You still have to configure the ESP8266 to attach to your router. You could setup the ESP8266 as a soft access point and create a web page through which the tablet can configure the ESP8266 with your router SSID and password.
 
Top