How to read a form field values (Ethernet Shield)

Hi,

i want to configure some sketch variables using a html form. I bought the Ethernet shield and now i am going to make a small formular where i can set the ip number for exmple. I am going to do this using four text fields.

I do not know how to read the field values!

To switch some leds I can make:

if(string.contains("LED1=on")){
  // turn the led on
}
if(string.contains("LED1=off")){
  //turn the led off
}

With two values 'on' and 'off' this way is ok. But now one field has more then only two values (in my exemple 0 - 255).

Do you have any idea how to solve my problem? Thanx.

RGB LED?

I would also be interested in knowing this, for maybe reading a text string, or setting individual port bits

Not for RGB LED Lyght.
It was just an example.

I want to configure a IP number.
For that I want to use four text fields.
And now how to read the values?

Well this post has it reading RGB values, from website... can modify for ip?

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238498454

It’s not complete but it gives you a place to start. You also need:
#include <WString.h> which you have to download.

void loop(){
  Client client = server.available();
  if (client) 
  {
    while (client.connected()) 
    {
      if (client.available()) 
        {
        char c = client.read();
        if (readString.length() < 100)
            {
            readString.append(c);
          }
        if (c == '\n') 
            {
              //code for pin 8
            if(readString.contains("toggle8"))
                {
             int state = toggle(8);
                  if(debug){ Serial.println("toggle8 received "); Serial.println(state); }
                  client.print("done8");
                  client.print(state);
                 
                }

Below is some server code that collects a “get” request (query_string) from a web brouser, does some string operations on the obtained string, and sends a reply back to the browser. You may have to look into the Ethernet.h code to see if it can obtain all the info sent by the browser.

//zoomkat 5-24-10

#include <WString.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
Server server(84); //server port

String readString = String(100); //string for fetching data from address

///////////////////////
 String teststring = String(100);
 String finalstring = String(100);
 String flag = String(2);
 int ind1 = 0;
 int ind2 = 0;
 int pos = 0;
 //////////////////////

void setup(){

//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();

//enable serial data print 
Serial.begin(9600); }

void loop(){
// Create a client connection
Client 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.append(c); 
} 

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

///////////////
//Serial.println(readString);
//readString looks like "GET /?-0p1555-1p500t1000 HTTP/1.1"

  if(readString.contains("-")) { //test for servo control sring
  readString.replace('-', '#');
  pos = readString.length(); //capture string length
  //find start of servo command string (#)
  ind1 = readString.indexOf('#');
  //capture front part of command string
  teststring = readString.substring(ind1, pos);
  //locate the end of the command string
  ind2 = teststring.indexOf(' ');
  //capturing the servo command string from readString
  finalstring = readString.substring(ind1, ind2+ind1);
  //print "finalstring" to com port;
  Serial.println(finalstring); //print string with CR
    }
  ////////////////////////
  //GET /?Slidervalue0=1800&Submit=Sub+0 HTTP/1.1
  if(readString.contains("Slidervalue")) {
  ind1 = readString.indexOf('u');
  ind2 = readString.indexOf('&');
  finalstring = readString.substring(ind1+1, ind2);
  finalstring.replace('e', '#');
  finalstring.replace('=', 'p');
  Serial.println(finalstring);
  }
  ///////////////////
  
  //now output HTML data header
  client.println("HTTP/1.1 204 Zoomkat");
  client.println();
  client.println();
  delay(1);
  //stopping client
client.stop();

/////////////////////
//clearing string for next read
readString="";
teststring="";
finalstring="";
  
}}}}}

Does anyone know how to get this to work with Arduino21? I'm having difficulty getting past the HTML headers without being able to use the .append function that you could use with older versions of Arduino.

I'm also having problems using:

c == '\n'

I don't seem to be able to detect this. within the string I'm reading

any help much appreciated

Boppyer

Below is some server code that works with IDE 0021.

//zoomkat 10-22-10
//routerbot code
//for use with IDE 0021
//open serial monitor to see what the arduino receives
// 

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.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
Server server(84); //server port

String readString, servo1, servo2; 
 
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;
 //////////////////////

void setup(){

//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();

//enable serial data print 
Serial.begin(9600); 
myservo1.attach(7);
myservo2.attach(6);
Serial.println("bot21"); // so I can keep track of what is loaded
}

void loop(){
// Create a client connection
Client 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; 
} 

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

///////////////
Serial.println(readString);

//readString looks like "GET /?-1500-1500 HTTP/1.1"

      if (readString.length() >0) {
      Serial.println(readString);
            
      servo1 = readString.substring(7, 11);
      servo2 = readString.substring(12, 16);
      
      Serial.println(servo1);
      Serial.println(servo2);
      
      int n1;
      int n2;
      
      char carray1[6];
      servo1.toCharArray(carray1, sizeof(carray1));
      n1 = atoi(carray1); 
      
      char carray2[6];
      servo2.toCharArray(carray2, sizeof(carray2));
      n2 = atoi(carray2); 
      
      myservo1.writeMicroseconds(n1);
      myservo2.writeMicroseconds(n2);
      
      //myservo.write(n);
      readString="";
      } 
  ///////////////////
  
  //now output HTML data header
  client.println("HTTP/1.1 204 Zoomkat");
  client.println();
  client.println();
  delay(1);
  //stopping client
client.stop();

/////////////////////
//clearing string for next read
readString="";
  
}}}}}

.append has been replaced by .concat or you can us +=.

Thanks for the help,

In the end I used TextFinder.h to look for my values, works well and doesn’t take up much space too.

// Boppyer
// A little programme to read values from a webserver
//


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

int pollDelay = 2000; // default these to 5000


//////////////////////////////////////////////////////////////////
// Ethernet vars
//////////////////////////////////////////////////////////////////
byte mac[] = { 
  0xDE, 0xAA, 0xBB, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 24, 94 }; // ethernet shield
byte gateway[] = { 192, 168, 24, 1 }; //  router


//////////////////////////////////////////////////////////////////
//  webserver
//////////////////////////////////////////////////////////////////
char urlAddress[90] = "GET /snow.xml HTTP/1.0"; 
////// snow.xml
////// <ID>24</ID><amount>2000</amount>
//////

byte server[] = { 192, 168, 24, 60 }; // server
Client client(server, 80); // s
TextFinder  finder( client );  

//////////////////////////////////////////////////////////////////
// Setup
//////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(9600); // start the serial library:
  Ethernet.begin(mac, ip, gateway); // start the Ethernet connection:
  delay(1000); // give the Ethernet shield a second to initialize:
  Serial.println("Starting up Controller...");
}

//////////////////////////////////////////////////////////////////
// Loop
//////////////////////////////////////////////////////////////////
void loop() {
  if (client.connect()) {
    Serial.println();
    Serial.println("Connected");
    Serial.println(urlAddress); // tests the url
    client.println(urlAddress); 
    client.println();
  } 
  else {
    Serial.println(" connection failed");
  } 
  if (client.connected()) {
    
    // get the feild see if we need to snow
    if(finder.find("<ID>") )
    {    

      int snowID = finder.getValue();   
      Serial.print("I've found an ID: ");
      Serial.println(snowID);
      if(finder.find("<amount>") )
      {      
        int amount = finder.getValue();
        Serial.print("I've found an Amount: ");
        Serial.println(amount);
        
      }

    }
    else{
      Serial.print("Could not find a Snow trigger"); 
    }
  }


  else {
    Serial.println("Disconnected"); 
  }
  client.stop();
  Serial.println();
  Serial.println("Client Stopped");
  client.flush();  
  delay(pollDelay); // wait a minute before next update
}