I was disappointed with the Ethernet library's performance, then I heard of Ethernet2, but it was pretty much the same speed. I was able to get 4.70KB/s over an HTTP connection with the standard Client.read() interface. I thought that the hardware must be able to do better, so I wrote a small addition to the library, which allowed me to achieve 81.92KB/s... See if you can beat my speed!
Addition to Client.cpp:
// returns # of bytes read, n is # of bytes requested,
// b is a pointer to at least n bytes.
uint16_t Client::readn(uint16_t n, uint8_t *b) {
if (!available())
return -1;
return recv(_sock, b, n < available() ? n : available());
}
Addition to Client.h:
//in public:
uint16_t readn(uint16_t, uint8_t*);
Here is my RSS reader (early):
#include <Ethernet2.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 200 };
byte server[] = { 74,125,113,121 }; // rss.slashdot.org
const int BUFSZ (1024);
char buf[BUFSZ];
Client client(server, 80);
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(115200);
delay(2000);
Serial.println("connecting...");
if (client.connect()) {
Serial.println("connected");
client.println("GET /Slashdot/slashdot HTTP/1.1");
client.println("Host: rss.slashdot.org");
client.println();
} else {
Serial.println("connection failed");
}
}
void loop()
{
unsigned long bytes(0);
unsigned long beg(millis());
if(client.available()) {
while (client.available()) {
bytes += client.readn(BUFSZ, (uint8_t*)buf);
//Serial.print(buf);
}
Serial.print("Bytes: ");
Serial.println(bytes);
Serial.print("millis: ");
Serial.println(millis()-beg);
Serial.print("Speed: ");
Serial.println(bytes/(float)(millis()-beg));
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
while(1);
}
}
I know, my sketch is rough around the edges still, but the performance improvement I thought was worth sharing...
What do you think?