Arduino WebServer

I'm making a site with the arduino and when I add more lines of code (more precisely "println") fails to make the site load.

The code follows below:

#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(169,254,190, 177);


// 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;
    String vars;
    int varMenu=0;
    int var2Menu=0;
    int var2Dig=0;
    int var3Dig=0;
    
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        vars.concat(c);
        
        if(vars.endsWith("=Entradas"))   varMenu=1;
        else if (vars.endsWith("=Matlab"))   varMenu=2;
        /*else if (vars.endsWith("=EMail"))   varMenu=3;*/
        /*else if (vars.endsWith("=2on"))   var2Dig=1;
        else if (vars.endsWith("=2off"))   var2Dig=2;
        else if (vars.endsWith("=2on"))   var3Dig=1;
        else if (vars.endsWith("=2off"))   var3Dig=2;*/
        // 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) {
          // Cliente terminou de comunicar e o arduino tem de enviar
          // o seguinte cabeçalho para o browser
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<head>");
          // meta tag -> Faz refresh ao browser de 5 em 5 segundos
          //client.println("<meta http-equiv=\"refresh\" content=\"5\">");
          client.println("<title>Webduino</title>");
          client.println("<style type='text/css'>");
          client.println("body {");
          client.println("background-color: #CCC;");
          client.println("}");
          client.println("</style>");
          client.println("</head>");
          client.println("<body>");
          client.println("<img src='http://img856.imageshack.us/img856/5865/webduino.gif' width='180' height='108' /></p>");
          client.println("<hr />");
          client.println("<form action='' name='menu' enctype='text/plain'>");
          client.println("<input type='submit' name='Home' id='Home' value='Home' />");
          client.println("<input type='submit' name='es' id='es' value='Entradas'/>");
          client.println("<input type='submit' name='matlab' id='Matlab' value='Matlab' />");
          client.println("<input type='submit' name='sugestoes' id='sugestoes' value='EMail' />");
          client.println("</form>");
          client.println("<hr />");
          
          if (varMenu==0) //MAIN
          {
          client.println("<h1>Projecto Final</h1>");
          client.println("Trabalho realizado usando um Arduino UNO R3 + Ethernet Shield");
          client.println("<h3>Objectivo do site:</h3>");
          client.println("- Neste website e possivel activar as entradas/saidas do arduino independentemente.
- E possivel gravar valores de uma porta analogica num ficheiro *txt para servir como historico mais tarde.
- Atraves do matlab pode criar graficos desses valores para poder estuda-los devidamente.
");
         // client.println("- E possivel verificar processos de um automato atraves da ferramenta Matlab+OPC e mostra-los no site");
          }

          else if (varMenu==1) //Entradas/Saidas
          {
            client.println("<h1>Entradas/Saídas</h1>");
            client.println("<p>Saídas Digitais:</p>");
            client.println("<form action='' name='menu' enctype='text/plain'>");
            client.println("<p>Digital 2 -");
            client.println("<input type='submit' name='on2' id='on2' value='On' />  /");
            client.println("<input type='submit' name='off2' id='off2' value='Off' />  | Digital 3 -");
            client.println("<input type='submit' name='on3' id='on3' value='On' />  /");
            client.println("<input type='submit' name='off3' id='off3' value='Off' /></form>");
          
      }
          else if (varMenu==2) //Matlab
          {
            client.println("Matlab Ainda nao feita");
          }
          /*if (varMenu==3) //Sugestoes
          {
            client.println("Sugestoes - Ainda não feita");
          }*/
          /*else
          {
           client.println("Erro ao mostrar o site");
           client.println("</body>");
           client.println("</html>");
           break;
          }*/
          /*
          //output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
          int sensorReading = analogRead(analogChannel);
          client.print("analog input ");
          client.print(analogChannel);
          client.print(" is ");
          client.print(sensorReading);
          client.println("
");       
          }*/
                    
          
          client.println("</body>");
          client.println("</html>");
          // FIM CODIGO HTML
          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");
  }
}

For example:

if (varMenu==0) //MAIN
          {
          client.println("<h1>Projecto Final</h1>");
          client.println("Trabalho realizado usando um Arduino UNO R3 + Ethernet Shield");
          client.println("<h3>Objectivo do site:</h3>");
          client.println("- Neste website e possivel activar as entradas/saidas do arduino independentemente.
- E possivel gravar valores de uma porta analogica num ficheiro *txt para servir como historico mais tarde.
- Atraves do matlab pode criar graficos desses valores para poder estuda-los devidamente.
");
         // client.println("- E possivel verificar processos de um automato atraves da ferramenta Matlab+OPC e mostra-los no site");
          }

If I delete the comment the site does not work.

Someone can help me with the code or tell me a better way to program?

Try using the F() macro for every constant string in your code that is directly given to an object that inherits the Print class (as Serial or EthernetXXX).

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

will then get

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

This way the constant string is stored in program memory (the flash) and you free your RAM for other uses. Otherwise loading so many strings fills up your RAM in no time and your Arduino constantly reboots when you try to access it.

Works fine.. Thank you very much :slight_smile: