Arduino + Ethernet. A page long loading

Hi, I need help. I have an Arduino Uno R3 and the Ethernet shield as my setup. I tried this code that turn 7 individual LEDs on and off. That work fine.

So I played around with the code, basically just add few buttons, radio button and combo box in html. The initial load after I upload the sketch load quite fast. But once I pressed one of the submit button, it goes to a very long loading. After that happened I couldn't access the page from a different tab on my browser. I have to press the reset button on my Arduino Uno to be able access it again. Sometime I have to plug out the usb (power) and plug it back to view the page again.

May I know the cause of this issue?
And is there a way to load external html source code?

please and thank you

And where is that code? We haven't seen it yet, so we cannot help you.

Sorry, here is the code

#include <Ethernet.h>
#include <SPI.h>

//network NB: Pins 10, 11, 12 and 13 are reserved for Ethernet module. 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {10, 100, 156, 61};
byte gateway[] = {10, 100, 156, 62};
byte subnet[] = { 255, 255, 255, 192 };

//variables
String inString = String(100); //URL read

//output pins
int servoPin = 9;

//portion range
// range 0 to 3. 0=null, 1=small, 2=medium, 3=large
int portion=0;

//interval options
//range 0 to 9. 8 options : 5m, 30m, 1h, 3h, 5h, 7h, 9h, 11h
int intervalOptions=0; 

//mode
//range 0 to 2. 0=none. 1=manual, 2=auto
int mode=0;

//range based on option in combo box
int timeOption=0;  

EthernetServer server(80);
String data;

//FUNCTIONS
//

void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac, ip,gateway,subnet); 
  server.begin();
   //set pin mode
  for (int j = 1; j < (numofleds + 1); j++){
    pinMode(led[j], OUTPUT);
  }
  pinMode(servoPin, OUTPUT);
  
  Serial.println("Serial READY");
  Serial.println("Ethernet READY");
  Serial.println("Server READY");
}

void loop(){
  EthernetClient client = server.available();
  
  if(client){
    // an http request ends with a blank line
    boolean current_line_is_blank = true;
    while (client.connected()) {
     
      if(client.available()) {
      
        char c = client.read();
        // if we've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so we can send a reply
        if (inString.length() < 100) {
            inString.concat(c);
         } 
        if (c == '\n' && current_line_is_blank) {
                    
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<html><title>NSMF</title>");
          client.println();
          client.println("<body>");
          client.println("<p>NSMF</p>");
          client.println();
          client.println("<form method='post'>");
          client.println(   "<fieldset>");
          client.println(      "<legend>Manual Feed</legend>");
          client.println(      "Portion : ");
          client.println(      "<input type='radio' name='portionM' value='s'>Small</input>");
          client.println(      "<input type='radio' name='portionM' value='m'>Medium</input>");
          client.println(      "<input type='radio' name='portionM' value='l'>Large</input>");
          client.println(      "<button type='submit'>Feed Now</button>");
          client.println(   "</fieldset>");
          client.println("</form>");
          client.println();
          client.println("<form method='get'>");
          client.println(   "<fieldset>");
          client.println(      "<legend>Auto Feed</legend>");
          client.println(      "Portion : ");
          client.println(      "<input type='radio' name='portionA' value='s'>Small</input>");
          client.println(      "<input type='radio' name='portionA' value='m'>Medium</input>");
          client.println(      "<input type='radio' name='portionA' value='l'>Large</input>");
          client.println(      "
");
          client.println(      "Time interval : ");
          client.println(      "<select name='time'>");
          client.println(         "<option value='5m'>5 minutes</option>");
          client.println(         "<option value='30m'>30 minutes</option>");
          client.println(         "<option value='1h'>1 Hour</option>");
          client.println(         "<option value='3h'>3 Hours</option>");
          client.println(         "<option value='5h'>5 Hours</option>");
          client.println(         "<option value='7h'>7 Hours</option>");
          client.println(         "<option value='9h'>9 Hours</option>");
          client.println(         "<option value='11h'>11 Hours</option>");
          client.println(      "</select>");
          client.println();
          client.println(      "<button type='submit'>Auto Feed</button>");
          client.println(   "</fieldset>");
          client.println("</form>");
          client.println();
          client.println();
          client.println();
          
          
          //search from URL - could be done in function
          //manual mode
          if(inString.indexOf("portionM=s")>0){
            mode=1;
            portion=1;
          }
          else if(inString.indexOf("portionM=m")>0){
            mode=1;
            portion=2;
            //client.println("manual : m");
          }
          else if(inString.indexOf("portionM=l")>0){
            mode=1;
            portion=3;
            //client.println("manual : l");
          }
          //auto mode
          if(inString.indexOf("portionA=s")>0){
            mode=2;    //auto
            portion=1; //small
          }
          else if(inString.indexOf("portionA=m")>0){
            mode=2;    //auto
            portion=2; //medium
          }
          else if(inString.indexOf("portionA=l")>0){
            mode=2;    //auto
            portion=3; //large
          }
          //get the time
          if(inString.indexOf("time=5m")>0)
            client.println("
5 minutes");

          
          //PRINT
          //mode print
          client.println("
Current Mode : ");
          if(mode==1)      //manual
            client.println("Manual. ");
          else if(mode==2) //auto
            client.println("Auto. ");
          else
            client.println("null");
          //portion print  
          client.println();
          client.println("
Portion : ");
          if(portion==1)
            client.println("Small. ");
          else if(portion==2)
            client.println("Medium. ");
          else if(portion==3)
            client.println("Large. ");
          else
            client.println("null");

         
         client.println("</body></html>");
          break;
        }//if end
        if (c == '\n') {
          // we're starting a new line
          current_line_is_blank = true;
        } 
        else if (c != '\r') {
          // we've gotten a character on the current line
          current_line_is_blank = false;
        }
      }//if end
    }//while end
    // give the web browser time to receive the data
    delay(1);
    
    inString = "";
    client.stop();

  }//if end
} //loop end

First of all, you're using the String class, so sooner or later your code will crash. If you plan to run your sketch for more than the first few requests, get rid of that class and work with character arrays only.

The next problem and probably the cause of your slow downs is that you only read the header of the HTTP request and then you send the whole page, ignoring the rest of the request.

Fix these two issues and post again if the problem still occurs.

I didn't change my string to char, because I don't know how to. But someone told me to try to use the client.println(F("text")); . Gladly it solve the slow load any all the unexpected characters displays.

About the 'HTTP request' I dont understand. But I am trying to find how can I not hard code my html in the Arduino codes. I want to put it as index.html and store it in the microSD card inside the slot on the ethernet shield, but I have not yet close to getting this.

I didn't change my string to char, because I don't know how to.

You didn't have to because it probably didn't have anything to do with the slow page loading issue.

soooo... any progress? any thoughts on what is causing the slow loading? i have pretty much the same issue.

anyone?

I have a thought. My Mega 2560 and ethernet shield are working really good together! :slight_smile:
I use my own code tho.
http://playground.arduino.cc/Code/WebClient

irab88:
soooo... any progress? any thoughts on what is causing the slow loading? i have pretty much the same issue.

anyone?

The code origionally posted doesn't compile for me, so I go with bad code.

zoomkat:

irab88:
soooo... any progress? any thoughts on what is causing the slow loading? i have pretty much the same issue.

anyone?

The code origionally posted doesn't compile for me, so I go with bad code.

I agree. I've clean up my code a bit and it load fine. Thanks zoomkat.

SurferTim, thanks for your code.