Stray characters on ethernet svr

I am playing with a network card that I have connected to a laptop. The Arduino has only one input used, (PIR) to keep it all simple. I also have a 16x2 I2C LCD for local display.

The program reads a browser in the form of 192.168.0.199/?0 to return the value of counter 0. It is straight out of the examples in the library.

I have found that the processing loop receives many more characters from the laptop and this processing slows down the loop. The extra characters are displayed on the LCD via the attached code.

The attached code has most of my explanation at the point in question and is marked by *******

I would like to know why and how to stop it.

/*  
 Set up network shield with IP to receive command from browser in 
 form of 192.168.0.199/?234 where ?234 is the command to return the
 value of counter 2, 3, and 4 in sequence.
  */
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Ethernet.h>
#include <SPI.h>
#define BACKLIGHT_PIN     13

boolean reading = false;
boolean clrCount = false;            //command to clear counters

int swNum [] = {
  2,3,4,5,6,7,8,9};                 //list of input pins to test

const int arraySize = (sizeof(swNum) / sizeof(swNum[0]));  
int swFlag[arraySize] ;                   //flag for each switch operated.
int countArray [arraySize] ;              //array for switch counters

int count;                        //general purpose count

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

//CONFIGURE
byte ip[] = { 
  192, 168, 0, 199 };   //Manual setup only
byte gateway[] = {
  192, 168, 0, 1 }; //Manual setup only
byte subnet[] = {
  255, 255, 255, 0 }; //Manual setup only

// if need to change the MAC address (Very Rare)
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server = EthernetServer(80); //port 80

////////////////////////////////////////////////////////////////////////

void setup()
{
  //Serial.begin(9600);


  pinMode ( BACKLIGHT_PIN, OUTPUT );   // Switch on the backlight
  digitalWrite ( BACKLIGHT_PIN, HIGH );

  Ethernet.begin(mac, ip, gateway, subnet); //for manual setup
  server.begin();

  lcd.begin(16,2);               // initialize the lcd 
  lcd.home ();                   // go home
  lcd.print("Hello ");  
  delay(2000);
  lcd.clear();
}

void loop(){      // listen for incoming clients, and process request.

  EthernetClient client = server.available();

  if (client) 
  {
    boolean currentLineIsBlank = true;
    boolean sentHeader = false;
    clrCount = false;
    reading = false;
    while (client.connected()) 
    {
      if (client.available()) 
      { 
        if(!sentHeader)
        {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();      // an http request ends with a blank line
          sentHeader = true; 
        }
        char c = client.read();

        if(reading && c == ' ') reading = false;  //found end of string, stop processing
        if(c == '?') reading = true; //found the ?, begin reading the info
        //*********************************************       
        /* the next part shows many characters being processed even though only 
         192.168.0.199/?0 is sent from the browser.
         
         The sequence for this as printed is   0G0E0T 0/1/100 then off screen.
         Then continues next line with 0 and char which repeats with 0 and different char .
         Eventually the loop exits. 
         
         This processing takes time and causes the browser to show "Waiting for....
         info is coming from browser as stops when ethernet cable removed.
         
         */
        lcd.print(reading);            // state of the reading flag
        lcd.print(c);
        delay (1000);                   // delay to enable viewing

        //***********************************************        
        if(reading )
        {
          switch (c)                 
          {
          case 'c':          //received clear command
            {
              clrCount = true;          
            }
            break;


          case '0':
            if (!clrCount)
              outputCount(0, client);
            else {
              countArray [0] = 0;
              outputCount(0, client);
            }
            break;
          case '1':
            if (!clrCount)
              outputCount(1, client);
            else {
              countArray [1] = 0;
              outputCount(1, client);
            }
            break;
          case '2':
            if (!clrCount)
              outputCount(0, client);
            else {
              countArray [0] = 0;
              outputCount(0, client);
            }
            break;

            //case for other counters removed for clarity.
          }
        }

        if (c == '\n' && currentLineIsBlank)  break;

        if (c == '\n') 
        {
          currentLineIsBlank = true; 
        }
        else 
          if (c != '\r') {
          currentLineIsBlank = false;  
        }
      }
    }
    delay(1);                  // give the web browser time to receive the data
    client.stop();             // close the connection:
  }

  //  read the inputs and count pulses.
  // uses PIR for input testing. sensors will be open collector NPN so change HIGH to LOW etc.
  for (count =0; count < arraySize; count++)
  {
    if (digitalRead(swNum [count]) == HIGH && swFlag[count] == 0){         
      countArray [count]++;
      swFlag[count] = 1;          //set the flag

      lcd.clear();
      lcd.print (countArray[count]);
    }
    else
    { 
      if(digitalRead(swNum [count]) == LOW && swFlag[count] == 1)
        swFlag[count] = 0;          //reset the flag
    }
  }
}


void outputCount(int countNum, EthernetClient client){
  // Client needed just for HTML output purposes. Returns comment to browser
  client.print("Counter ");
  client.print(countNum);
  client.print(" is ");
  client.println(countArray[countNum] );
  client.print("
");
}

Thanks for any help

Weedpharma

Web browsers send a bunch of parameters with a request. I don't think you can stop it, but you can ignore most of it. The part that concerns you is before the first line feed. You can ignore all after the '\n'.

The program reads a browser in the form of 192.168.0.199/?0

A browser gets data from a server, and renders an image based on the data.

There is NO way that the Arduino is getting that image from the browser.

        /* the next part shows many characters being processed even though only
         192.168.0.199/?0 is sent from the browser.

Well, clearly that is not true. If the Arduino is processing more than that, it is because the server that it accessed is sending more than that as the response.

        delay (1000);                   // delay to enable viewing

As opposed to sending to the Serial Monitor application? I'll type real slow, so as to not swamp your browser, then.

Thank you for replying to my query.

I am totally unfamiliar with networks and browser communications so have probably used wrong terminology so please bear with me.

A browser gets data from a server, and renders an image based on the data.

There is NO way that the Arduino is getting that image from the browser.

I am sending 192.168.0.199/?0 in the URL bar. That is all I know is sent.

The example in the library tells me that is what the Arduino program needs to respond with something. I have chosen a counter but kept all other parts of the example.

Well, clearly that is not true. If the Arduino is processing more than that, it is because the server that it accessed is sending more than that as the response.

As far as my knowledge goes, the above is all that is being sent. I ask my question to be enlightened.

As opposed to sending to the Serial Monitor application? I'll type real slow, so as to not swamp your browser, then.

As I said, I am using an LCD on the Arduino and this is what my question related to. This would be a valid point IF I was using the serial monitor (and I could change the program to do so if required), but I am not so this comment is redundant.

Thank you SurferTim for your information that there is more sent from the browser than what I expected. I will need to take that into account in the program.

Weedpharma

but I am not so this comment is redundant.

My point was that it would be orders of magnitude faster to send the data to the Serial Monitor app than to send the data one character per second to the LCD. If time matters…

that there is more sent from the browser than what I expected.

NOTHING is sent from a browser. A browser is one kind of client. An Arduino is another kind of client. Both kinds of clients get data from servers.

What the browser does is ditch most of the server response, showing you only what it thinks you want to see.

Your Arduino code needs to do the same thing.

Here is a GET request sent by Thunderbird when I put http://192.168.2.177 in the address bar.

GET / HTTP/1.1 Host: 192.168.2.177 User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:42.0) Gecko/20100101 Firefox/42.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate Connection: keep-alive

Here is the same request sent by Google Chrome.

GET / HTTP/1.1 Host: 192.168.2.177 Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36 Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8

That explains the GET part of the rubbish I was getting after my /?0.

PaulS., the only purpose for the delay was to enable me to see what was happening, it is not part of my program other than to fault find.

Weedpharma