Speed (latency) of ENC28J60 module / ethernet

Hi,

I run the standard example for Ethernet with a web server. The response time is about 500ms for each request. (a long wait) (checked with chrome developer mode)
Could I speed this up or should I use a different protocol (serial?) to get faster response times to interface between arduino and a PC?

Thanks,

Jeroen

Post the sketch you're running as well as a link to the library you're using. What speed do you expect from the Arduino? Depending on these needs you may have to change the Arduino you're using and choose a Yun (or something similar) as there an embedded Linux system is handling network requests which enhances latency by factors.

BTW: the value you get in browser debuggers is not the network latency but the time need for a complete request/response cycle which usually means that multiple packets are transferred over the network.

Using an Arduino Ethernet Shield (with a WizNet5100 chip) will enhance the latency but not necessarily the response times of your Arduino application.

As I already wrote, a Yun may be your Arduino of choice if you need fast response times. If you need even more speed, change to an embedded Linux system (Raspberry Pi, CubieBoard, PCduino, etc.).

Hey Pylon,

Thanks for your response!
I do not want to go the YUN or Pi way, but I think half a second is quite something!
Do UDP or serial reduces response times? I can imagine that serial is the quickest possible way?
Will the W5100 increase response times as well? I just need bytes in return ... just need around 16 bytes would be enough,
and my response time should be less or around 100ms.
I'm currently using the MEGA by the way.

Thanks again,

Jeroen

Do UDP or serial reduces response times?

UDP will reduce response times but you cannot use it from a browser. What kind of serial are you thinking about? A network is also a serial connection.

I do not want to go the YUN or Pi way, but I think half a second is quite something!

Yes, half a second is more than I would expect. Please post the code that is producing this response time. And provide a link to the library you're using for that (there a several different libraries for the ENC28J60 chip).

Will the W5100 increase response times as well?

It might as most of the low level TCP/IP stuff is done in the chip directly without bothering the ATmega of the Arduino.
What is the Arduino doing to deliver the response? What kind of data is in these 16 bytes?

I do not completely agree with the first response, HTML5 allows UDP sockets, however I don't want to go that way preferably.

This is the code that is giving me a wait for half a second

#include <UIPEthernet.h>
#include <UIPServer.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,1);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println("Connection: close");  // the connection will be closed after completion of the response
            client.println();
            client.println("Hello you there!");
          break;
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

The 16 bytes (max) is a response for a RFID id, but it can have statusses of led's and relays as well,
in this format 011011100001, I was planning to extract it on the server and I have a fixed order, so I know the second 1 is a green led (as an example)

As you might noticed, I'm using

#include <UIPEthernet.h>
#include <UIPServer.h>

The serial I mean is the serial thru USB, the one that also is shown in the arduino program under extra > serial monitor

To make your requests faster, remove the serial output to a 9600 baud interface, every character sent to this interface needs about 1ms to be sent. A typical browser request header is almost 500 bytes in size, so you get almost to the delay you experienced.

Also change the output of the result page to a version where you use the two argument write() method and no print() or println() methods because these send the data out using one argument write() calls, so every byte might be sent in a single packet.

Using these two optimizations you probably get the response time down to very few dozens of milliseconds (or less).

pylon:
Also change the output of the result page to a version where you use the two argument write() method and no print() or println() methods because these send the data out using one argument write() calls, so every byte might be sent in a single packet.

with UIPEthernet the use of print()/println() is rather save as uip implements nagles algorithm (sort of...). Fragmentation into small packets would only occur on networks with very low latency where the ACKs are arriving faster then the Stream being written on the arduino.

Stock Ethernet-lib for WIZ5100 shields is different as it sends a packet for every single call of write/print/println...

regards,

Norbert