Go Down

Topic: UDP Send Delay (Read 2001 times) previous topic - next topic

Angry_n00b

Hi,
I am working on a project that involves rapidly collecting data from a remote XBee (every 50ms or so) and sending that data immediately to the computer over Ethernet, with as little time delay as possible.
The problem I am having is that for some reason the Arduino is only sending the data over the Ethernet connection once a second, and not as soon as the data arrives, and I don't understand why there should be such a delay.
I'm using an EtherTen board from Freetronics, which uses the same Wiznet W5100 chip as the standard Ethernet shield, and the latest version of the Arduino software.

This is the code that I'm using, and it works perfectly well with no delays when the UDP code is taken out and it's only printing to Serial.
Code: [Select]

#include <Ethernet.h>
#include <EthernetUdp.h>
#include <XBee.h>
#include <SPI.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 10);
unsigned int localPort = 8888;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];

IPAddress otherIP(192, 168, 1, 2);
unsigned int otherPort = 1111;

EthernetUDP Udp;

XBee xbee = XBee();

Rx16Response rx16 = Rx16Response();

int RSSI;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);

  Serial.begin(9600);
  xbee.begin(9600);
}

void loop()
{
  packetRead();
}

void packetRead()
{
  xbee.readPacket(50);
  if (xbee.getResponse().isAvailable()){
    if (xbee.getResponse().getApiId() == RX_16_RESPONSE){
      xbee.getResponse().getRx16Response(rx16);
      RSSI = rx16.getRssi();
     
      // Gets the data from the packet and sends it to the computer.
      Udp.beginPacket(otherIP, otherPort);   
      Serial.print(RSSI);
      Udp.write(RSSI);
      Serial.print(" ");
      Udp.write(" ");
      for(int i = 0; i < rx16.getDataLength(); i++){
        Serial.print(rx16.getData(i),HEX);
        Udp.write(rx16.getData(i));
      }
      Udp.endPacket();
      Serial.println();     
    }
    else {
      Serial.println("Wrong Api");
    }
  }
  else if (xbee.getResponse().isError()){
    Serial.println("No Packet");
    Serial.println(xbee.getResponse().getErrorCode());
  }
}


I feel like I must be missing something really simple here in the way that the UDP code works, but for the life of me I can't figure it out.

PaulS

There is no guarantee that UDP packets will be delivered, or, if they are, that they will be be in some specific order. Are you sure you want to use UDP?
The art of getting good answers lies in asking good questions.

Angry_n00b

I've tried setting the Arduino up as a standard TCP client as well, but I had the same problem with delays in sending the data. UDP should be fine, as sending the data quickly with little overhead is more important than every packet making it or making it out of order.

zoomkat

I just tried the below meta refresh web page code with the refresh set to 0, and the page refreshed in IE ~900 times in ~60 seconds, or ~15 times a second. Not sure what your slow problem might be.

Code: [Select]

// zoomkat meta refresh server test code
// arduino IDE 1.0
// for W5100 ethernet shield
// the IP address will be dependent on your local network/router
// port 80 is default for HTTP, but can be changed as needed
// use IP address like http://192.168.1.102:84 in your brouser
// or http://zoomkat.no-ip.com:84 with dynamic IP service
// use the \ slash to escape the " in the html
// meta refresh set for 2 seconds

#include <SPI.h>
#include <Ethernet.h>

int x=0; //set refresh counter to 0
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,102); // ip in lan
EthernetServer server(84); //server is using port 84

void setup()
{
  // start the server
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
     while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // see if HTTP request has ended with blank line
        if (c == '\n') {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
         
          //meta-refresh page every 2 seconds
          x=x+1; //page upload counter
          client.println("<HTML>");
          client.print("<HEAD>");
          client.print("<meta http-equiv=\"refresh\" content=\"0\">");
          client.print("<TITLE />Zoomkat's meta-refresh test</title>");
          client.print("</head>");
          client.println("<BODY>");
          client.print("Zoomkat's meta-refresh test IDE 1.0");
          client.println("<br />");
                   
          client.print("page refresh number ");
          client.println(x); //current refresh count
          client.println("<br />");
          client.println("<br />");
         
          client.print("Zoomkat's arduino analog input values:");
          client.println("<br />");
          client.println("<br />");
         
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(analogRead(analogChannel));
            client.println("<br />");
            }
           break;
          client.println("</BODY>");
          client.println("</HTML>");
         }
        }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Angry_n00b

Thanks, that solution works.

Joris75

Hello,

I have the same problem than Angry_n00b !

But I'm sorry, I don't understand the solution which has been implemented.

Does Somebody could explain me how to prevent Udp function (begin, write, end) to introduce delay ?

Thanks a lot for your help !


Joris

SurferTim

Hello,

I have the same problem than Angry_n00b !

But I'm sorry, I don't understand the solution which has been implemented.

Does Somebody could explain me how to prevent Udp function (begin, write, end) to introduce delay ?

Thanks a lot for your help !


Joris

I use UDP packets on a localnet at 20 per second without any problems. How much delay are you getting?

This is a thread from 2012. You should start a new one next time.

Go Up