N Nitin Joshi Active Member Licensed User Longtime User Nov 1, 2018 #1 Hi, my APP is using UDP protocol to communicate with Esp8266. My APP needs to send UDP packets 2 or 3 times for successful transmission. This leads to malfunctioning. Please guide me some codes lines to avoid multiple UDP packets transmission. Note: I have to keep local port fix.
Hi, my APP is using UDP protocol to communicate with Esp8266. My APP needs to send UDP packets 2 or 3 times for successful transmission. This leads to malfunctioning. Please guide me some codes lines to avoid multiple UDP packets transmission. Note: I have to keep local port fix.
Erel B4X founder Staff member Licensed User Longtime User Nov 1, 2018 #2 Nitin Joshi said: My APP needs to send UDP packets 2 or 3 times for successful transmission. This leads to malfunctioning. Click to expand... More information please. What are you doing and what happens? Upvote 0
Nitin Joshi said: My APP needs to send UDP packets 2 or 3 times for successful transmission. This leads to malfunctioning. Click to expand... More information please. What are you doing and what happens?
N Nitin Joshi Active Member Licensed User Longtime User Nov 1, 2018 #3 About project. 1. ESP8266 and APP are in same WiFi network. 2. APP communicating with ESP8266 on UDP protocol. 3. When APP sends a packet to device, device in returns sends acknowledgement packet. 4. When I send the packet from APP, device does not receive that UDP packet. I am using serial monitor of esp8266 to verify. 5. I have send UDP packet from APP for 2 or 3 times at least. I am using FOR loop for the same. 6. Sometimes device gets all packers some times 1 or 2 less. For example if I send packet 3 times, device receives 3 times or less. Later device sends acknowledgement packets that many times. 7. This is leading to malfunctioning in my APP. What I am looking for. UDP packet should get deliver in first attempt. Hope now clear. Upvote 0
About project. 1. ESP8266 and APP are in same WiFi network. 2. APP communicating with ESP8266 on UDP protocol. 3. When APP sends a packet to device, device in returns sends acknowledgement packet. 4. When I send the packet from APP, device does not receive that UDP packet. I am using serial monitor of esp8266 to verify. 5. I have send UDP packet from APP for 2 or 3 times at least. I am using FOR loop for the same. 6. Sometimes device gets all packers some times 1 or 2 less. For example if I send packet 3 times, device receives 3 times or less. Later device sends acknowledgement packets that many times. 7. This is leading to malfunctioning in my APP. What I am looking for. UDP packet should get deliver in first attempt. Hope now clear.
N Nitin Joshi Active Member Licensed User Longtime User Nov 1, 2018 #4 This is the code I am using Sub UDP_Send (Str As String, remoteIP As String) Dim Data() As Byte Dim i As Int For i=1 To 3 If UDPSkt.IsInitialized=False Then UDPSkt.Initialize("UDP", 4211, 255) End If Data=Str.GetBytes("UTF8") UDPPkt.Initialize(Data, remoteIP, 4210) Sleep(500) UDPSkt.Send(UDPPkt) Next End Sub Upvote 0
This is the code I am using Sub UDP_Send (Str As String, remoteIP As String) Dim Data() As Byte Dim i As Int For i=1 To 3 If UDPSkt.IsInitialized=False Then UDPSkt.Initialize("UDP", 4211, 255) End If Data=Str.GetBytes("UTF8") UDPPkt.Initialize(Data, remoteIP, 4210) Sleep(500) UDPSkt.Send(UDPPkt) Next End Sub
Erel B4X founder Staff member Licensed User Longtime User Nov 1, 2018 #5 Please use [code]code here...[/code] tags when posting code. Are you using B4R to develop the ESP8266 program? Why are you using UDP? Try this example: [B4X] B4RSerializator - Send and receive objects instead of bytes Upvote 0
Please use [code]code here...[/code] tags when posting code. Are you using B4R to develop the ESP8266 program? Why are you using UDP? Try this example: [B4X] B4RSerializator - Send and receive objects instead of bytes
N Nitin Joshi Active Member Licensed User Longtime User Nov 1, 2018 #6 I am using Arduino for ESP8266 program. I am using UDP because, communication from APP to device is not frequent. Upvote 0
I am using Arduino for ESP8266 program. I am using UDP because, communication from APP to device is not frequent.
Erel B4X founder Staff member Licensed User Longtime User Nov 1, 2018 #7 B4R is a free tool. Download it and try the example I showed you. You can later switch to UDP. Upvote 0
N Nitin Joshi Active Member Licensed User Longtime User Nov 1, 2018 #8 Thanks for advice, but I can not switch back to other software. Please suggest to my original query...why do i need to send UDP message more than 1 time? Upvote 0
Thanks for advice, but I can not switch back to other software. Please suggest to my original query...why do i need to send UDP message more than 1 time?
OliverA Expert Licensed User Longtime User Nov 1, 2018 #9 Nitin Joshi said: Thanks for advice, but I can not switch back to other software. Please suggest to my original query...why do i need to send UDP message more than 1 time? Click to expand... Plenty of folks here have used B4A for sending/receiving UDP packets without issue. So either 1) your target IP and/or port is wrong, 2) your device is not picking up the UDP packet, or 3) you have a really crappy network connection. Most likely it's #2. I guess you could put a log in your For/Next loop to see it execute 3 times (or debug it). If it sent out the 3 packets and you're only getting 1 or 2, then it is on the other device end (or you have a really bad network). Upvote 0
Nitin Joshi said: Thanks for advice, but I can not switch back to other software. Please suggest to my original query...why do i need to send UDP message more than 1 time? Click to expand... Plenty of folks here have used B4A for sending/receiving UDP packets without issue. So either 1) your target IP and/or port is wrong, 2) your device is not picking up the UDP packet, or 3) you have a really crappy network connection. Most likely it's #2. I guess you could put a log in your For/Next loop to see it execute 3 times (or debug it). If it sent out the 3 packets and you're only getting 1 or 2, then it is on the other device end (or you have a really bad network).
OliverA Expert Licensed User Longtime User Nov 1, 2018 #10 Nitin Joshi said: What I am looking for. UDP packet should get deliver in first attempt. Click to expand... BTW, you realize that UDP is not guaranteed. That's the whole point of UDP, it trades speed for guaranteed delivery. Upvote 0
Nitin Joshi said: What I am looking for. UDP packet should get deliver in first attempt. Click to expand... BTW, you realize that UDP is not guaranteed. That's the whole point of UDP, it trades speed for guaranteed delivery.
Computersmith64 Well-Known Member Licensed User Longtime User Nov 2, 2018 #11 If you need guaranteed delivery, you should use TCP. I have a B4A/B4R system that sends/receives large amounts of data to/from an ESP8266 at 2Hz to 5Hz via TCP without any problems. For the times that there is no data being sent/received, I use a watchdog timer to keep the connection alive. - Colin. Upvote 0
If you need guaranteed delivery, you should use TCP. I have a B4A/B4R system that sends/receives large amounts of data to/from an ESP8266 at 2Hz to 5Hz via TCP without any problems. For the times that there is no data being sent/received, I use a watchdog timer to keep the connection alive. - Colin.
N Nitin Joshi Active Member Licensed User Longtime User Nov 2, 2018 #12 Oh thanks. Unfortunately, I can not revise my solution to TCP. Meanwhile, I am looking a solution to check connection is live or not. Can I use watchdog timer? Please help with more details.. Upvote 0
Oh thanks. Unfortunately, I can not revise my solution to TCP. Meanwhile, I am looking a solution to check connection is live or not. Can I use watchdog timer? Please help with more details..
Computersmith64 Well-Known Member Licensed User Longtime User Nov 2, 2018 #13 Why are you initializing your UDP socket & packet on different ports? Code: Copy UDPSkt.Initialize("UDP", 4211, 255) UDPPkt.Initialize(Data, remoteIP, 4210) Shouldn't they both be on the same port? Eg: here's the initialization code I use in one of my apps that uses UDP broadcast to find other instances of the app on the network, then switches to TCP. It works 100% of the time (except when the network has UDP broadcast disabled). Code: Copy Private UDPPort As Int = 13130 UDPSocket1.Initialize("UDP", UDPPort, 256) Packet.Initialize(data, IPAddr, UDPPort) - Colin. Upvote 0
Why are you initializing your UDP socket & packet on different ports? Code: Copy UDPSkt.Initialize("UDP", 4211, 255) UDPPkt.Initialize(Data, remoteIP, 4210) Shouldn't they both be on the same port? Eg: here's the initialization code I use in one of my apps that uses UDP broadcast to find other instances of the app on the network, then switches to TCP. It works 100% of the time (except when the network has UDP broadcast disabled). Code: Copy Private UDPPort As Int = 13130 UDPSocket1.Initialize("UDP", UDPPort, 256) Packet.Initialize(data, IPAddr, UDPPort) - Colin.
N Nitin Joshi Active Member Licensed User Longtime User Nov 2, 2018 #14 Please see my comment beside command. Code: Copy UDPSkt.Initialize("UDP", 4211, 255) ' here 4211 is local port UDPPkt.Initialize(Data, remoteIP, 4210) 'here 4210 is remote port. Am I wrong in above code? Upvote 0
Please see my comment beside command. Code: Copy UDPSkt.Initialize("UDP", 4211, 255) ' here 4211 is local port UDPPkt.Initialize(Data, remoteIP, 4210) 'here 4210 is remote port. Am I wrong in above code?
OliverA Expert Licensed User Longtime User Nov 2, 2018 #15 Nitin Joshi said: Meanwhile, I am looking a solution to check connection is live or not. Click to expand... UDP is a connection less protocol. UDP should be used in cases where packet loss (up to a certain point) is not an issue (such is VOIP and Video). In your case, it looks like packet loss is detrimental to your application. Therefore, you have to build your own retry mechanism (which TCP has already build in). Instead of just sending out x number of packets and then expecting a response, you need to send out each packet individually, and then wait for each packets response before sending the next one*. Worse, you have to somehow ID the packets, since acknowledgements may come out of order. This is all the stuff that TCP does already for you and that you have to manage yourself when using UDP. (*) You can also sent out a group of packets and then wait for one response that list the packets that were received on only resend packets that seem to have been lost. Once more, this is something that TCP does for you. Upvote 0
Nitin Joshi said: Meanwhile, I am looking a solution to check connection is live or not. Click to expand... UDP is a connection less protocol. UDP should be used in cases where packet loss (up to a certain point) is not an issue (such is VOIP and Video). In your case, it looks like packet loss is detrimental to your application. Therefore, you have to build your own retry mechanism (which TCP has already build in). Instead of just sending out x number of packets and then expecting a response, you need to send out each packet individually, and then wait for each packets response before sending the next one*. Worse, you have to somehow ID the packets, since acknowledgements may come out of order. This is all the stuff that TCP does already for you and that you have to manage yourself when using UDP. (*) You can also sent out a group of packets and then wait for one response that list the packets that were received on only resend packets that seem to have been lost. Once more, this is something that TCP does for you.
Computersmith64 Well-Known Member Licensed User Longtime User Nov 2, 2018 #16 Nitin Joshi said: Please see my comment beside command. Code: Copy UDPSkt.Initialize("UDP", 4211, 255) ' here 4211 is local port UDPPkt.Initialize(Data, remoteIP, 4210) 'here 4210 is remote port. Am I wrong in above code? Click to expand... Looking at the example in the UDPSocket documentation, I guess it should work using 2 different ports (assuming you are sending / receiving on the correct ports at the other end). I've always just used a single port for UDP (which also works fine), so it seemed odd to me... - Colin. Upvote 0
Nitin Joshi said: Please see my comment beside command. Code: Copy UDPSkt.Initialize("UDP", 4211, 255) ' here 4211 is local port UDPPkt.Initialize(Data, remoteIP, 4210) 'here 4210 is remote port. Am I wrong in above code? Click to expand... Looking at the example in the UDPSocket documentation, I guess it should work using 2 different ports (assuming you are sending / receiving on the correct ports at the other end). I've always just used a single port for UDP (which also works fine), so it seemed odd to me... - Colin.