how do I use a stream of data

Hi, After finishing a 'hello led world' with this nice lib and the example provided there: http://srmonk.blogspot.com/2010/03/simplified-ethernet-library-for-28j60.html

I now want to send a GET to Arduino to set a servo position, so it must be a number. Unfortunately there is no hint there on the lib page how to work with values: 1. What is the best practice to command like "/?servopos=110&led=off" and yet parse it so that I get the servopos as integer and led as string in two variables? 2. How then convert from string to integer there, if I have servopos=110 please?

The code there performs:

if (strcmp(params, "?cmd=on") == 0)

But well, I don't know what servo value will be send, it could be any number between 0 to 179 that then I have to send to myservo.write(servopos).

Thank you in advance for help!

Unfortunately there is no hint there on the lib page how to work with values:

Please correct your thread title. Your question is a basic "how do I use a stream of data", not anything specific to the Ethernet library provided by the Arduino team. In fact, you aren't even using that library.

You need to collect the data in an array (apparently done, or strcmp() could not be used), and use strtok() to parse the string, and atoi() to convert numeric tokens to ints. There are similar functions to convert strings to other types, if that becomes necessary.

In order to provide any more help, we need to see your code, and the data that you are receiving from the client.

Paul, thanks, I actually don’t know how to correct post subject here I think it could be done only by forum admin?

Also, I thought there may be some function in that lib that does this and I don’t know…

Ok so my question is data streaming, I actually still didn’t write the complete code but simple ‘proof of concept test code’ to make sure I can send and receive servo pos and rgb-led set values ok. No servo pos code yet written, I only realized example I found in that Ethernet lib, and without standard detail this is the code:

try {
socket = new Socket("162.122.1.1", 80);//my arduino ip
				OutputStream out = socket.getOutputStream();
				PrintWriter outw = new PrintWriter(out, false);
				String strHttpGET = "GET /?" + textOut.getText().toString() + " HTTP/1.0\r\n"; 
		        outw.print(strHttpGET);
		        outw.print("Accept: text/plain, text/html, text/*\r\n");
		        outw.print("\r\n");
		        outw.flush();
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			finally{
				if (socket != null){
					try {
						socket.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
........

in Arduino side, I used simply the example from that lib this way:

void loop()
{
  char* params;
  if (params = e.serviceRequest())
  {
    e.print("<H1>Web Remote</H1>");
    if (strcmp(params, "?cmd=on") == 0)
    {
      digitalWrite(outputPin, HIGH);
      e.print("<A HREF='?cmd=off'>Turn off</A>");
    }
    else
    {
      digitalWrite(outputPin, LOW);
      e.print("<A HREF='?cmd=on'>Turn on</A>");
    }
    e.respond();
  }
}

Now right here, I came to this question of how to send values. As I understand, you mean I have to extract the string between ‘?’ and & characters and between two ‘&’ characters for my command:

"http://162.122.1.1/?servopos=120&led=on&r=20&g=10&b=30"

Then in Arduino side, I will use these:

  1. strtok() for parsing
  2. atoi() to get integer
  3. some substring functions

Did I get all right please?

I think it could be done only by forum admin?

No. Modify your original post. The subject field is editable.

  char* params;
  if (params = e.serviceRequest())
  {

So, this function is collecting the response in a string.

As I understand, you mean I have to extract the string between '?' and & characters and between two '&' characters for my command

Yes. The tokens you get will then be "servopos=120", "led=on", "r=20", "g=10", and "b=30".

You need to store these tokens in an array, using strdup() to make copies of them.

Then, you need to parse each token, with "=" as the delimiter, to get "servopos" and "120", for example. Then, use atoi() on the second token to get 120, and use strcmp() with the first token to see where to store the value or how to use it.

Thanks, now it is clear to me

Old server servo code that needs to be updated to the 1.0 IDE standards.

//zoomkat 10-22-10
//routerbot code
//for use with IDE 0021
//open serial monitor to see what the arduino receives
//use a url like below in a browser to test
// http://192.168.1.102:84/?-1450-1550 

#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="";

        }
      }
    }
  }
}

Thank you very much, man this is directly about what I'm experimenting...I learned new things by your code, thank you and thank you Arduino!