UDP PACKET SENDING VIA W5100+mega2560 HANGS AFTER A WHILE

I am unable to find the error.

I am using udp connection with my w5100. I have a udp client app install on my pc.
when the udp client on pc received “connect” message it replies me back “ok” via udp protocol. This is very simple. I have also implemented udp connection fail checking logic by continuously checking the udp.parsepacket feild for specific amount of time and if i dont get the packet from pc client I just close the connection and retry again .
But i found that my after a while my mega 2560 hangs in udp.print(“connect”); and doesnt go further.

String ip="192.168.1.200";
setup()
{
Serial.begin(115200);
Serial.println("udp tester");
}

loop()
{
if(!ping_local_servers(ip) )
{
Serial.println("client replied ok");
}
else
{
  Serial.println("client replied fail");
}
delay(2000);
}


_Bool ping_local_servers(String &str)
{   
 char ip_buffer[18]={"\0"};
 char  packetBuffer[10]={"\0"}; //buffer to hold incoming packet,
 String packet="\0";
 _Bool success=1; //1 means false 0 means ok
 
 if(udpport)
 { 
   Udp.begin(udpport);  
   delay(100);
   if(str.length())
       {
         str.toCharArray(ip_buffer,17);//converting string datatype ip address to char array and saving in buffer
      
         Udp.beginPacket(ip_buffer,56414);//56414 is deafult port number for server app
         Udp.print(P("connect"));//send "connect" command as ping command
         Udp.endPacket();
         delay(50);      
         int timer=0;
         int8_t n=0;
         while(!n && timer<450)//wait 450millisec until packet not received else break
         {
           wdt_rst();
           n=Udp.parsePacket();
           delay(1);
           timer++;
         }
         if(timer<450)
         { 
            Udp.read(packetBuffer,10);
         }
         packet=String(packetBuffer);
         packet.trim();
         
         if(packet.equals("ok"))
         {
          success=0;
         }
                  
       } 
       Udp.stop();  
 }   
return success;
}

I searched the net and found that udp.endpacket() ;// doesnt come out when there is timeout

please help as i am struggling for 3 days now

Well, use of String objects can cause symptoms like that. Grab a copy of the freemem function and see if you're leaking memory. Better, just get rid of the Strings and use char arrays instead.

But I am aware string object corrupts ram. but mega has 6Kb ram remaining. Also i am converting the string data back to char array and then passing the array containing ip address to udp.beginpacket(ip_array,port);

using freemem function and see if you're leaking memory.
leaking memory ? i didnt get it. this means the ram getting full under iteration

made use of it reporting almost 6045bytes free ram

int freeRam () {
  extern int __heap_start, *__brkval;
  int v;
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}

If you call the freemem function in loop, does it fall over time?

Also, I see that you're not null terminating your packetBuffer, make it one byte longer and terminate it before you do anything with it.

I have also implemented UDP connection fail checking logic by continuously checking the udp.parsepacket feild for specific amount of time and if I don’t get the packet from pc client I just close the connection and retry again

Hello PRB_tech,

You do know that UDP is connectionless? You cannot create a connection with UDP. That does not mean that you can’t write your own software that creates a connection of its own and uses UDP to transport the data, but UDP itself does not and cannot establish a connection.

UDP works on a ‘send and forget’ principal’; which means you send some data out using the sending device without any pre-established connection and without checking that the receiving device is actually able to receive the data, and without any check that the data has been received.

PerryBebbington

Yes correct udp working principal is cleared with me. But the udp.parsepacket is just to check after sending message "connect" to receiver-udp-device whether my trasnmitter-udp device is getting some reply as "ok" i am waiting for some 550ms to see the packet. But the problem is not here.

Code abruptly hangs at udp.endpacket(); function and never comes out of it . Some times my code works for several hours and then just hangs on udp.endpacket();

I have installed new 1.8.10 adruino ide and the ethernet library is updated. I am using W5100 with no sd-card. CS(chip-select-pin) is for w5100. No sd card used.

why the library doesnt come out of udp.endapacket(); technically it should auto time out.

This happens at no specific time .

I actually implemented this to achieve "ping" terminology to check whether remote udp-receiver-arduino is live or not. I just send "connect " and wait for 550ms to see if i get "ok" or not else i close the connection. and again do the ping.

I found no workable library for actualy icmp ping .Icmp library available shows many errors. So If anybody know how to actually ping the remote arduino usind tcp.ip it would be great help.

Yes correct udp working principal is cleared with me.

OK, good. It wasn't clear to me from your original post if you understood that or not. I wasn't sure if it made a difference to the problem you were looking for but it seemed possible to me that if you thought UDP made a connection then you would be looking in the wrong place for the problem.

I think you should do as wildbill suggests and get rid of Strings. I understand your thinking that it can't be the cause, but, in my experience, when trying to resolve a problem fix the obvious stuff first, then look deeper. I've had loads of problems just disappear when I have fixed something obvious that didn't seem to be related to the main problem I was having.

I would ditch the delays as well, again, not because I know they will cause a problem but they are just bad to have and the above comment applies to delay as well; fix the obvious stuff. Do you know how to write code without the delays or do you need help?

My only experience of using UDP is I have 2 ESP8266s passing data between a PIC based project and a Nextion display. This is unfinished but working as I expect it to, and I've had my fair share of problems with it. What I'm saying is I'm not the expert here, just trying to help with what I know.

I hope somewhere in the above is something helpful, I can't think of anything else that might help. Maybe someone more expert will comment.

PerryBebbington:
OK, good. It wasn't clear to me from your original post if you understood that or not. I wasn't sure if it made a difference to the problem you were looking for but it seemed possible to me that if you thought UDP made a connection then you would be looking in the wrong place for the problem.

I think you should do as wildbill suggests and get rid of Strings. I understand your thinking that it can't be the cause, but, in my experience, when trying to resolve a problem fix the obvious stuff first, then look deeper. I've had loads of problems just disappear when I have fixed something obvious that didn't seem to be related to the main problem I was having.

I would ditch the delays as well, again, not because I know they will cause a problem but they are just bad to have and the above comment applies to delay as well; fix the obvious stuff. Do you know how to write code without the delays or do you need help?

My only experience of using UDP is I have 2 ESP8266s passing data between a PIC based project and a Nextion display. This is unfinished but working as I expect it to, and I've had my fair share of problems with it. What I'm saying is I'm not the expert here, just trying to help with what I know.

I hope somewhere in the above is something helpful, I can't think of anything else that might help. Maybe someone more expert will comment.

I am not expert but a beginner diy kinda person.

(1)I really appreciate you help brother. You said to write code without delays how to do that?,This will add new knowledge to my repository.

My only experience of using UDP is I have 2 ESP8266s passing data between a PIC based project

(2)Have you encountered such issues like udp hang. Although i think esp etherent library is different from arduino's.

I think you should do as wildbill suggests and get rid of Strings. I understand your thinking that it can't be the cause, but, in my experience, when trying to resolve a problem fix the obvious stuff first, then look deeper.

(3)Strings might be trouble but why in the first place string data type introduce in arduino. previously such data type was not there. but i am using mega 2560. and i run free_ram() to check an i ge almost 3450 Bytes of ram vacant. all the time. Is there any method to check heap and stack

You said to write code without delays how to do that?

Start with the example 'blink without delay' in the Arduino IDE. Read these forums, the subject comes up often.

Have you encountered such issues like udp hang.

Not sure. One of my ESP8266s hangs occasionally, sometimes if I leave it for a few hours it sorts itself out. I don't know why it does it although I did connect 5V to the 3V3 input to one of them by mistake for a second or 2. I imagine I probably damaged in some way, although I can't be sure that the one I connected 5V is the one that hangs. I will buy a new one at some time and see.

Strings might be trouble but why in the first place string data type introduce in arduino?

I assume, but I don't know, that they are there because they are a valid part of C++, they just don't work well on hardware like micro-controllers. Remember that C++ was not created specifically for micro-controllers.

Is there any method to check heap and stack?

Yes, I think so but I don't know how. Search these fora.
Trying to prove it is not Strings that are causing the problem is ignoring my advice (as you are free to do) of fixing the obvious stuff before looking deeper.