How to send data from web browser to Arduino?

I have read the book Beginning Arduino written by Michael McRoberts and I have gotten the ethernet projects working using the Arduino Ethernet Shield. I have no problem pushing data to email or a web page.

My question is how do I push data (user entering data in real time on a web page) back to the Arduino? I assume the Arduino sends the HTTP code for a web page that has blank entry fields and a SUBMIT button? And if so, then would the Arduino listen for what in response? I just want to have the user send a 2 digit code, thats all.

// Very small part of example code to read each char from web
void serialEvent() {

// Read a char
char inChar = client.read();

if (inChar == 'A1') {
//do something
} else if (inChar == 'A2') {
//do something else
etc,etc

I know I am probably not explaining this very well. If you need more explanation, please let me know.

JC

Simple server test code.

//zoomkat 4-1-12
//simple button GET for servo and pin 5
//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 ' instead of " 
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

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

#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

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();

  myservo.write(90); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server servo/pin 5 test 1.0"); // 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); //print to serial monitor for debuging 

          client.println("HTTP/1.1 200 OK"); //send new page
          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>Zoomkat's simple Arduino button</H1>");
          
          client.println("<a href=\"/?on\">ON</a>"); 
          client.println("<a href=\"/?off\">OFF</a>"); 

          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(1);
          //stopping client
          client.stop();

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

        }
      }
    }
  }
}

Excellent! Thank you. I am at work right now but will try when I get home.

I tried to look up the code but I couldn't find it. I don't understand the "\ "/?on"" part.

If the first "" refers to the web root, why isn't is "/"?

And could you please explain the "/?on" part? Thanks. I know it works, I just would like to understand why.

JC

" is an escaped double quote. When the string being sent contains double quotes, they need to be escaped, to distinguish them from the end of string double quote.

The / in /?on is the name of the web page (i.e. none). The ? separates the form data from the URL. The form data, a single value, since there is no & in the string, is on.

Thank you PaulS. That explains it. Once you mention the double quote thing it all made sense. Wow.

JC

Once you mention the double quote thing it all made sense. Wow.

In the html, a single quote ' can replace the double quote " so the escaping of the double quote is not required.

In the html, a single quote ' can replace the double quote " so the escaping of the double quote is not required.

Then why do you do it?

          client.println("<a href=\"/?on\">ON</a>"); 
          client.println("<a href=\"/?off\">OFF</a>");

Then why do you do it?

To give you something to do! 8) Otherwise it provides an example of escaping a " in some other context, and escaping in general.

Hii

Can I modify the code for an official arduino wifi shield? What changes have to be made?

Thanks