Need help getting variable from arduino ethernet url

Hello,

I have an arduino with an ethernet shield. The networking is setup correctly and I can access it without any problem. The webpage is designed to have an input box that allows a user to enter a numeric value. Once the value is entered, it is “transferred” to the url. So,
192.168.1.555 becomes 192.168.1.555/?temp=48. I want to be able to store the temp value to a variable in the sketch. I tried using the example from Arduino Web forms - tutorial and a couple other places, but when I submit the value in the form, the Serial.println() command returns the letter “G”… There is no G anywhere in the webpage and the numbers being entered into the form are not the ascii value for G so I’m not sure why this is happening. The code is 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[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
IPAddress ip(192,168,1,555);
byte gateway[] = {192,168,1,254 };
byte subnet[] = {255,255,255,0 };

// 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, subnet, gateway);
  server.begin();

  //This seems to "force" the webpage to load correctly...
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  Serial.begin(9600);
}
void loop() {
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.println(c);
        // send a standard http response header
        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println();

        //Single line to save precious bytes
        client.println("<html><body bgcolor=#cccccc style='font-size:16pt; text-align:center;'><h1>Motor</h1><hr><h3>Speed</h3><form action=''><input style='font-size:16pt;width:50px;' name='temp' value='80'>&deg;F</form><hr></body></html>");
        break;
      }
    }
    // close the connection:
    client.stop();
  }
}

Thanks,

I rewrote the code to match the example and then changed the webpage to display what I needed it to and now it works. Apperently, the “G” was the first byte http://arduino.cc/en/Reference/ClientRead so thats why it was printed. Also, It appears that the code required a “check” to see where the end of the url was. For anyone else trying to get url data, I have posted the fixed code 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[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
IPAddress ip(192,168,1,555);
byte gateway[] = {192,168,1,254 };
byte subnet[] = {255,255,255,0 };

String HttpHeader = String(100);

// 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, subnet, gateway);
  server.begin();

  //This seems to "force" the webpage to load correctly...
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  Serial.begin(9600);
  
  HttpHeader="";
}
void loop() {
  EthernetClient client = server.available();
  if (client) {
      if (client.available()) {

         char c = client.read();
         //read MaxHeaderLength number of characters in the HTTP header
         //discard the rest until \n
         if (HttpHeader.length() < 100) {
           //store characters to string
           HttpHeader = HttpHeader + c;
         }
         //if HTTP request has ended
         if (c == '\n') {
           // show the string on the monitor
           Serial.println(HttpHeader);
          // start of web page
           client.println("HTTP/1.1 200 OK");
           client.println("Content-Type: text/html");
           client.println();
           client.println("<html><body bgcolor=#cccccc style='font-size:16pt; text-align:center;'><h1>Fan</h1><hr><h3>Activate
Temp</h3><form action=''><input style='font-size:16pt;width:50px;' name='temp' value='80'>&deg;F</form><hr></body></html>");
           //clearing string for next read
           HttpHeader="";
           //stopping client
            client.stop();
         }

      }
  }
}

Basic server code with a form text box.

//zoomkat 12-08-12
//get submit box code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html or use a '
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605 

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84);; //server port

String readString; 

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

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server text box test1"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //see what was captured

          //now output HTML data header

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>HTML form GET example</H1>");

          client.println("<FORM ACTION='/' method=get >"); //uses IP/port of web page

          client.println("Pin 5 'on' or 'off': <INPUT TYPE=TEXT NAME='LED' VALUE='' SIZE='25' MAXLENGTH='50'>
");

          client.println("<INPUT TYPE=SUBMIT NAME='submit' VALUE='Change Pin 5!'>");

          client.println("</FORM>");

          client.println("
");

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();

          /////////////////////
          if(readString.indexOf("on") >0)//checks for on
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

kindly upload your arduino and HTML code.