Ok, So I finally came up with something interesting.
As I said before, each time we call
client.print(blabla);
we are sending 1 TCP packet. So if I want to make an HTML table like the web server example included in the compiler, we are going to send a lot of small packets. Specially each time we send an int number (just 2 bytes).
The header of the TCP packet is about 20 bytes so it's a lot of overhead for sending small amounts of info.
By implementing the functions I wrote before on the web server example, I reduced the time in a 33% (I changed the table from 5 to 150 entries).
Take care about the buffer size because of 2 reasons:
1) It has to be bigger than the info we want to put inside.
2) For reducing the time of refreshing the web page, I found an optimum buffer of 60 Bytes.
I though that a bigger buffer will be even better, but it's not. Maybe the time of writing/reading the data in the buffer takes more time than the overhead of the TCP protocol. Any way, here is the example with the implemented functions. Try to change the buffer size and refresh many time the web page and you will see the effect.
Hope it will be useful for someone!
#include <SPI.h>
#include <Ethernet.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,1, 177);
const int Buffer_length = 60; //Optimum buffer size
char buffer[Buffer_length]; //buffer for TCP packets
// 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()
{
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.begin(9600);
}
void loop()
{
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// 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) {
int inicio=millis();
// send a standard http response header
printC("HTTP/1.1 200 OK\n",client);
printC("Content-Type: text/html\n",client);
printC("\n",client);
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 150; analogChannel++) {
printC("analog input ",client);
printI(analogChannel,client);
printC(" is ",client);
printI(analogChannel*100,client);
printC("<br />",client);
}
printflush(client);
int fin=millis();
Serial.print("Elapsed time= ");
Serial.println(fin-inicio);
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();
}
}
void printI(int num, EthernetClient client){
if (strlen(buffer)<Buffer_length-6) //we add the int value only if we have enough space!
{
sprintf(buffer,"%s%d",buffer,num); //we add the int value to the buffer
// Serial.print("agrege int a buffer= "); Serial.println(num);
}
else {
client.println(buffer); //we send the buffer though the net
// Serial.println(buffer);
buffer[0]='\0'; //we reset the buffer
sprintf(buffer,"%s%d",buffer,num);
// Serial.println("mande el buffer");
}
}
void printC(char info[], EthernetClient client){
int length_array1=strlen(buffer);
int length_array2=strlen(info);
if (Buffer_length-1>length_array1+length_array2)
{
strcat(buffer,info);
// Serial.println("agrege char a buffer");
}
else {
client.println(buffer);
// Serial.println("mande el buffer");
// Serial.println(buffer);
buffer[0]='\0';
strcat(buffer,info);
}
}
void printime(int h, int m, int s, EthernetClient client) { //size 8 bytes
if (strlen(buffer)<Buffer_length-9)
{
sprintf(buffer,"%s%02d:%02d:%02d",buffer,h,m,s);
}
else {
client.println(buffer);
buffer[0]='\0';
sprintf(buffer,"%s%02d:%02d:%02d",buffer,h,m,s);
}
}
void printdate (int d, int m, int y, EthernetClient client) { //size 10 bytes
if (strlen(buffer)<Buffer_length-11)
{
sprintf(buffer,"%s%02d:%02d:%02d",buffer,d,m,y);
}
else {
client.println(buffer);
buffer[0]='\0';
sprintf(buffer,"%s%02d:%02d:%02d",buffer,d,m,y);
}
}
void printflush(EthernetClient client){ //we send any information left
client.println(buffer);
// Serial.println("flushed el buffer");
// Serial.println(buffer);
buffer[0]='\0';
}