Web Server

Hello, Is there any maximum size for HTML code using an Arduino ATMEGA328 as webserver + W5100 shield? Thanks, Alberto.

Is there any maximum size for HTML code using an Arduino ATMEGA328 as webserver + W5100 shield?

Yes, and no. If you store the page description on a file on the SD card, the size is virtually unlimited. However, there is a practical limit, based on the time it takes to serve up the page.

If you don't use the SD card, but do use PROGMEM, then the page size is limited by the amount of flash space (32,000 bytes, roughly).

If you don't use PROGMEM, then the size is limited to the amount of SRAM (2,000 bytes, roughly).

Hello PaulS,
Thanks for your reply. My code is about 16000 bytes so it should work, however it does not.
I am trying to control 9 lights. It works with 6 but when I try to incrise one more, it fails.

#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,2,100);

int PIN_LED1=1;
String state1="OF";
int PIN_LED2=2;
String state2="OF";
int PIN_LED3=3;
String state3="OF";
int PIN_LED4=4;
String state4="OF";
int PIN_LED5=5;
String state5="OF";
int PIN_LED6=6;
String state6="OF";
int PIN_LED7=7;
String state7="OF";
String readString1=String(30);

// 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);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  pinMode(PIN_LED1,OUTPUT);
  digitalWrite(PIN_LED1,LOW);
  pinMode(PIN_LED2,OUTPUT);
  digitalWrite(PIN_LED2,LOW);
  pinMode(PIN_LED3,OUTPUT);
  digitalWrite(PIN_LED3,LOW);
  pinMode(PIN_LED4,OUTPUT);
  digitalWrite(PIN_LED4,LOW);
  pinMode(PIN_LED5,OUTPUT);
  digitalWrite(PIN_LED5,LOW);
  pinMode(PIN_LED6,OUTPUT);
  digitalWrite(PIN_LED6,LOW);
  pinMode(PIN_LED7,OUTPUT);
  digitalWrite(PIN_LED7,LOW);
}


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();
        if(readString1.length()<100)//Leemos petición HTTP caracter a caracter
        {
          readString1+=c; //Almacenar los caracteres en la variable readString
        
        }
        if (c == '\n' && currentLineIsBlank) {
          if(readString1.indexOf("LED1ON")>0)
          {
            digitalWrite(PIN_LED1,HIGH);
            state1="ON";
            Serial.print("led on");
          } else if (readString1.indexOf("LED1OFF")>0)
          {
            digitalWrite(PIN_LED1,LOW);
            state1="OF";
            Serial.print("led off");
          }
            if(readString1.indexOf("LED2ON")>0)
          {
            digitalWrite(PIN_LED2,HIGH);
            state2="ON";
            Serial.print("led on");
          } else if (readString1.indexOf("LED2OFF")>0)
          {
            digitalWrite(PIN_LED2,LOW);
            state2="OF";
            Serial.print("led off");
          }
            if(readString1.indexOf("LED3ON")>0)
          {
            digitalWrite(PIN_LED3,HIGH);
            state3="ON";
            Serial.print("led on");
          } else if (readString1.indexOf("LED3OFF")>0)
          {
            digitalWrite(PIN_LED3,LOW);
            state3="OF";
            Serial.print("led off");
          }
            if(readString1.indexOf("LED4ON")>0)
          {
            digitalWrite(PIN_LED4,HIGH);
            state4="ON";
            Serial.print("led on");
          } else if (readString1.indexOf("LED4OFF")>0)
          {
            digitalWrite(PIN_LED4,LOW);
            state4="OF";
            Serial.print("led off");
          }
            if(readString1.indexOf("LED5ON")>0)
          {
            digitalWrite(PIN_LED5,HIGH);
            state5="ON";
            Serial.print("led on");
          } else if (readString1.indexOf("LED5OFF")>0)
          {
            digitalWrite(PIN_LED5,LOW);
            state5="OF";
            Serial.print("led off");
          }
            if(readString1.indexOf("LED6ON")>0)
          {
            digitalWrite(PIN_LED6,HIGH);
            state6="ON";
            Serial.print("led on");
          } else if (readString1.indexOf("LED6OFF")>0)
          {
            digitalWrite(PIN_LED6,LOW);
            state6="OF";
            Serial.print("led off");
          }
            if(readString1.indexOf("LED7ON")>0)
          {
            digitalWrite(PIN_LED7,HIGH);
            state7="ON";
            Serial.print("led on");
          } else if (readString1.indexOf("LED7OFF")>0)
          {
            digitalWrite(PIN_LED7,LOW);
            state7="OF";
            Serial.print("led off");
          }
          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("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<head>");
          client.println("<title>LAMPARA ON/OFF</title>");
          client.println("</head>");
          client.println("<body width=100% height=100%>");
          client.println("<center>");
          client.println("<h1>LAMPARA ON/OFF</h1>");
          client.print("

");
          client.print("ESTADO DE LA LAMPARA 1: ");
          client.print(state1);
          client.print("

");
          client.println("<a href=\"/LED1ON\" target=\"inlineframe\">ON</a>"); 
          client.println("<a href=\"/LED1OFF\" target=\"inlineframe\">OFF</a>"); 
          client.print("

");
          client.print("ESTADO DE LA LAMPARA 2: ");
          client.print(state2);
          client.print("

");
          client.println("<a href=\"/LED2ON\" target=\"inlineframe\">ON</a>"); 
          client.println("<a href=\"/LED2OFF\" target=\"inlineframe\">OFF</a>"); 
          client.print("

");
          client.print("ESTADO DE LA LAMPARA 3: ");
          client.print(state3);
          client.print("

");
          client.println("<a href=\"/LED3ON\" target=\"inlineframe\">ON</a>"); 
          client.println("<a href=\"/LED3OFF\" target=\"inlineframe\">OFF</a>"); 
          client.print("

");
          client.print("ESTADO DE LA LAMPARA 4: ");
          client.print(state4);
          client.print("

");
          client.println("<a href=\"/LED4ON\" target=\"inlineframe\">ON</a>"); 
          client.println("<a href=\"/LED4OFF\" target=\"inlineframe\">OFF</a>"); 
          client.print("

");
          client.print("ESTADO DE LA LAMPARA 5: ");
          client.print(state5);
          client.print("

");
          client.println("<a href=\"/LED5ON\" target=\"inlineframe\">ON</a>"); 
          client.println("<a href=\"/LED5OFF\" target=\"inlineframe\">OFF</a>"); 
          client.print("

");
          client.print("ESTADO DE LA LAMPARA 6: ");
          client.print(state6);
          client.print("

");
          client.println("<a href=\"/LED6ON\" target=\"inlineframe\">ON</a>"); 
          client.println("<a href=\"/LED6OFF\" target=\"inlineframe\">OFF</a>");
          client.print("

");
          client.print("ESTADO DE LA LAMPARA 7: ");
          client.print(state7);
          client.print("

");
          client.println("<a href=\"/LED7ON\" target=\"inlineframe\">ON</a>"); 
          client.println("<a href=\"/LED7OFF\" target=\"inlineframe\">OFF</a>"); 
          client.print("

");
          client.println("</center>");
          client.println("</body>");         
          
          // send a standard http response header
         
          client.println("</html>");
          readString1="";
          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");
  }
}

Any idea?

You are probably running out of SRAM. Try the F() function to keep your static strings in program memory. Otherwise, it copies them to SRAM before using them. A couple examples:

Serial.println(F("new client"));
client.println(F("HTTP/1.1 200 OK"));

Do that to the rest of your static strings.

I am trying to control 9 lights. It works with 6 but when I try to incrise one more, it fails.

String state1="OF";
String state2="OF";
String state3="OF";
String state4="OF";
String state5="OF";
String state6="OF";
String state7="OF";
String readString1=String(30);

You don't have unlimited amounts of memory. Quit pissing it away uselessly.

You don't need to use a String to keep track of a bit. One int will hold the state of 16 LEDs, and take up far less space. Using bitRead() and bitWrite() are far more efficient that the String class.

Whenever you find yourself appending a number to variable names, that's a sure sign that you should be using an array.

int PIN_LED1=1;

Which Arduino are you writing code for? Do you really need to allow for more than 255 pins? If not, use byte for pin numbers, not int.

By the way, you can't have an LED and the Serial instance using pin 1 at the same time.

Below is an example of bundling a section of your html code into an F() macro setup to save memory (not tested).

client.print(F("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n"
"<!DOCTYPE HTML>"
"<html>"
"<head>")
"<title>LAMPARA ON/OFF</title>"
"</head>"
"<body width=100% height=100%>"
"<center>"
"<h1>LAMPARA ON/OFF</h1>"
"

"
"ESTADO DE LA LAMPARA 1: "));

Thank a lot!! I applied your advices and now it works perfectly. Thanks again for your support.