WiFi.Udp class

Hi,

I'm using arduino mega256, and the wifishield.

Currently, I can perfectly send and receive udp.packets through port 5060.

After this comunication using the port 5060. I want to receive udp packages through port 16470 and then, send udp packages to 7012. But it seems like I cant do that....
I could either read through 16470 but then im not able to send to 7012, or send to 7012 but then im not able to read from 16470...
Question: Why is that happening?? Is there a way I can open 2 ports at the same time? I think that might solve my issue...but I couldnt figure out how.

Thanks in advance!

That depends. Are you sending and receiving to a device on the same localnet?

You will need to start two instances of UDP. I haven't tried that recently. The wifi shield has some firmware problems, so I quit playing with it.

Thank you for your fast reply.

Yes everything is done through the same localnet. I already tried to start two instances...

WiFiUDP sip;
WiFiUDP rtp;

sip.begin(5060);
rtp.begin(16470);
....

And then didnt work none of them.

Also... I've tried to do something else in order to use one port less, avoiding using the 16470 port. But then the arduino restarts.

I tried:

sip.begin(5060);
i got a normal communication... and then I need to receieve 50 packages per second, and at the end I send a package to (7012), just right after sending the package correctly restarts...
I cant understand why is that... because as I mentioned previously I could read those 50 packages per second, but then I couldnt send an answer back. In this case the microcontroller didnt restart, but couldnt send to (7012).

I'll post my code in 1min so I make myself more understandable :slight_smile:

kokaly:
Also... I've tried to do something else in order to use one port less, avoiding using the 16470 port. But then the arduino restarts.

If the Arduino is restarting, you have run out of SRAM or overflowing an array.

Attached you can find my code...what I have so far, please notice that the relevant part is marked with an square made of asterisk.

And as an answer of your last reply. That was my first thought, but how could be that possible If previously I was capable to read perfectly through 16470... I just changed the port to 5060, nothing else.

My thoght is that the port 5060 somehow got "overloaded" having so many task to do.... dont really know.... Thats why I wanted to use an extra port...

So.. is there any library that allows me to use more than 1 port at the same time ?
Or maybe its just me that im using wrongly the functions from the WiFiUDP library ?

Final_project_v5.20.post.ino (23.1 KB)

Looks like you are using a bunch of SRAM in your functions. Even a Mega will run out of SRAM.

Yes, that its true. But even though I barely use 40% of the memory, so i dont think that is the main issue...i guess.

Wht i do:

udp.begin(5060)
// In here I got a good comunication send/recieve.
udp.stop() <--
udp.begin(16470); <--
reading();
udp.beginPacket(IP,7012);
udp.write(message);
udp.endPacket();

The result of this test is that after having the good comunication through "5060", it will start receiving the packages from port 16470. But it wont send the packet to "IP,7012". But if instead I remove the two lines where i marked with arrows "udp.stop()" and "udp.begin(16470);" I wont be able to read anything from 16470 but the packet to "IP,7012" will be sent correctly.

I would really appreciate any suggestion that can guide me to be able to receive from 16470 and send correctly to 7012.

Do you think that the main issue lies on the SRAM ?
Is it me that im not using correctly the "WiFiUDP" libraries? Or maybe i must use other libraries that allows me to do what i want?

Anyways, thanks in advance.

kokaly:
Yes, that its true. But even though I barely use 40% of the memory, so i dont think that is the main issue...i guess.

You guess? How did you figure that? The compiler only reports global memory. For example, the following sketch reports 1015 bytes used in global memory, but this code will use over 3000 bytes, too much for an Uno.

char tBuf[1000];

void setup() {
  // put your setup code here, to run once:
  strcpy(tBuf,"test");
}

void loop() {
  // put your main code here, to run repeatedly:
  char xBuf[1000];
  strcpy(xBuf,"test");
  testFunct();
}

void testFunct() {
  
  char zBuf[1000];
  strcpy(zBuf,"test");
}

okay, I understand your explanation. I have modified the functions in a way that I have removed a lot of arrays from it. But still having the same issue.

what i can not understand is that when im reading from port 16470 the microcontroller is still alive and working. but when I change the port to 5060. Then the microcontroller restarts. Q: how come that using 2 different ports, the SRAM got affected differently? what could it be the reason?
Notice that, when using both ports, both of them are receiving exactly the same information. The unique difference is that, previously in the loop the 5060 has been used to send packages while the 16470 it is the first time that is used.

Thanks again.

I don't know what to tell you. I had problems with UDP and the wifi shield.

The primary reasons I abandoned using it were:

  1. the way the UDP responds to a packet. It is sent from a different port than it received the packet on.
  2. the UDP packet is limited to 96 bytes or smaller. Anything larger causes a fail.

The TCP server firmware wasn't any better. It uses only one socket, and has real problems if more than one client sends a request. It can send corrupt or incorrect files.

Well, thanks anyway.

Regarding:

SurferTim:
2) the UDP packet is limited to 96 bytes or smaller. Anything larger causes a fail.

I have noticed that, to be precisly it is 90bytes.As i have experienced that I have wasted a lot of my time on that issue... In case someone else get into the forum and read it... just to mention that of course it is possible to send packages larger than 90 bytes. Actually Im sending packages of 900 bytes, I havent tried to send anything larger than that.
The thing is, when you are typing:
char message[900];
udp.beginpacket(IP,port);
udp.write(message);
udp.endPacket();
"message" can not be larger than 90bytes.
but what you could do is to split "message" in 10 parts or 11, and then:

udp.beginPacket(IP,port);
udp.write(message1);
udp.write(message2);
udp.write(message3);
etc...
udp.endPacket();

Of course this is not the most smart way to do it, but it is a good way to understand it...
In my case, what I did is the following:

1.- I create the buffer, my message.

char *createSDP() {
char temp[120];
char sdpBuffer[400];

sprintf(temp, "v=0\r\n"); strcpy(sdpBuffer, temp);
sprintf(temp, "o=- 104852528 104852528 IN IP4 %s\r\n", IP_caller); strcat(sdpBuffer, temp);
sprintf(temp, "s=-\r\n"); strcat(sdpBuffer, temp);
sprintf(temp, "c=IN IP4 %s\r\n", IP_caller); strcat(sdpBuffer, temp);
sprintf(temp, "t=0 0\r\n"); strcat(sdpBuffer, temp);
sprintf(temp, "m=audio %s RTP/AVP 0 101\r\n", charRTPport); strcat(sdpBuffer, temp);
sprintf(temp, "a=rtpmap:0 PCMU/8000\r\n"); strcat(sdpBuffer, temp);
sprintf(temp, "a=rtpmap:101 telephone-event/8000\r\n"); strcat(sdpBuffer, temp);
sprintf(temp, "a=fmtp:101 0-15\r\n"); strcat(sdpBuffer, temp);
sprintf(temp, "a=ptime:20\r\n"); strcat(sdpBuffer, temp);
sprintf(temp, "a=sendrecv\r\n"); strcat(sdpBuffer, temp);

return sdpBuffer;
}

2.-Then I create another function that it will divide the buffer in 88bytes:

void sendSDP(IPAddress IP_server, unsigned int Port) {
char* package = createSDP();
unsigned int bufferSize = 88;
char Buffer2[bufferSize + 1];
unsigned int sizePackage = strlen(package);
unsigned int j = 0;
udp.beginPacket(IP_server, Port);
for (int i = 0; i < sizePackage; i++) {
Buffer2[j] = package*;*

  • j++;*
  • if (j == bufferSize - 1) {*
  • udp.write(Buffer2, j);*
  • j = 0;*
  • }*
  • }*
  • udp.write(Buffer2, j);*
  • udp.endPacket();*
    }
    [/quote]
    Then the whole buffer will be sent without any problem.
    Once again, it might not be the most smart way of coding, but for me it was good enough to send larger packages. Hope it is understandable and good enough. I wasted a lot of time on that, so I just hope it can be useful for someone else.
    Cheers!

This does not send 900 bytes. It sends only the length of the zero terminated string stored in message.

char message[900];
udp.beginpacket(IP,port);
udp.write(message);
udp.endPacket();

This sends 900 bytes.

char message[900];
udp.beginpacket(IP,port);
udp.write(message,900);
udp.endPacket();

oh yes! you are right once again :slight_smile:

But I just wrote that example because my "buffer" might change while testing, and might not be always the same length (900)... so thats why i thoght its better to keep it like udp.write(message), to keep it more generic.