Help-me with String and Ethernet

Hi guys, I'm with big problem, I make a webserve with java and arduindo via TCP/IP, but I needed send a standard message via socket, well, I can doing this but in arduino I can't capture this message, I'm using client.read () for this, but in my output, my string is showing char by char, I going paste my code for someone Can I help...Sorry for my english.

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
IPAddress ip( 10, 1, 1, 18 ) ;    
EthernetServer server(90);  
int frente = 9; 
int meio = 10;
int atras = 11;


void setup()
{

	Serial.begin(9600);
	Ethernet.begin(mac, ip);    
	server.begin();       
	Serial.print("server is at\n");
	pinMode(frente, OUTPUT);
	pinMode(meio, OUTPUT);
	pinMode(atras, OUTPUT);  

}

void loop()
{
	EthernetClient client = server.available();    

	if (client)
	{    
                int i = 0;
		while(client.connected()) 
		{       
                        char aux[3] = "";  
			if (client.available())
			{
				int i = 0;
                                while(i<1){
                                  char c = client.read();
                                  aux[i] += c; 
                                  i++; 
                                }
				//int k = atoi(aux);
                                aux[2] = '\0';
                                int codigo = atoi(aux);
                                if (strcmp(aux, "31") == 0){
                                  Serial.println("Sou 31");
                                }
                                int grupo = codigo / 3;
                                int estado = codigo % 3; 
                              
                                //msg[1] = client.read();
                                //char grupo = msg[0];
                                //char  estado = msg[1];
                                Serial.println("31");
                                //if (c == '3')Serial.println("Sou 3");
                                switch(grupo){

				case 10: //Frente
					Serial.println("Entrei no 10");
                                        if (estado == 1)
					{
						digitalWrite(frente, HIGH);
						//Serial.println("Ligando");
						client.println("1");
					}
					else
					{
						digitalWrite(frente, LOW);
						//Serial.println("Apagando");
						client.println("0");
					}
					break;

				case 20: //Meio
					Serial.println("Entrei no 20");
                                        if (estado == 1)
					{
						digitalWrite(meio, HIGH);
						//Serial.println("Ligando");
						client.println("1");
					}
					else
					{
						digitalWrite(meio, LOW);
						//Serial.println("Apagando");
						client.println("0");
					}
					break;

				case 30: //Atras
					Serial.println("Entrei no 30");
                                        if (estado == 1)
					{
						digitalWrite(atras, HIGH);
						//Serial.println("Ligando");
						client.println("1");
					}
					else
					{
						digitalWrite(atras, LOW);
						//Serial.println("Apagando");
						client.println("0");
					}
					break;
				}
				delay(1); 

			}
			
		}
		client.stop();    
	}
}
while(i<1){
                                  char c = client.read();
                                  aux[i] += c; 
                                  i++; 
                                }

Why use a while loop to iterate once? Looks clueless, to me.

Why are you ADDING c to the value already in aux[ 0 ]? The 0th element can only hold one character, so that is NOT appending the character to what is already there.

                                if (strcmp(aux, "31") == 0){

Since you are only reading one character, and then mangling the array element, there is not a snowballs chance in hell that this will ever be true.

Simple web server code that captures a request from a web client and acts upon what is in the request.

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

        }
      }
    }
  }
}

PaulS:

while(i<1){

char c = client.read();
                                  aux[i] += c;
                                  i++;
                                }



Why use a while loop to iterate once? Looks clueless, to me.

Why are you ADDING c to the value already in aux[ 0 ]? The 0th element can only hold one character, so that is NOT appending the character to what is already there.



if (strcmp(aux, "31") == 0){



Since you are only reading one character, and then mangling the array element, there is not a snowballs chance in hell that this will ever be true.
  1. Why use a while loop to iterate once? Looks clueless to me.

First, thanks for the reply, I used this loop already in despair understands, I'm 24 hours without sleep on top of this problem, so some things are no longer making much sense, but in this case was the following, I wanted to get element by element in Case 3:01 and put together a string, something got qn, n I can read a string as a whole, it seems that le char by char, it ta confusing me a bit, then did issso to try to mount this string, but still when I send to Serial.print () it prints the elements individually.

  1. Why are you c ADDING to the value already in aux [0]? The 0th element can only hold one character, so that is NOT appending the character to what is already there.
    I thought that if the position 0 would be the first element of the string, as in C. ..

  2. Since you are only reading one character, and then mangling the array element, there is not a snowballs chance in hell que this will ever be true.

Enter that part on purpose to see if it had any effect on the result which would be printed on the serial port, but even that still coming out equal.

Actually I just wanted to be able to capture a string that represents an application for my code, the string would be 0F F indentificaria what a circuit and the 0 state (connected or not). But I've tried everything and I'm not having results, as absurd as it seems and the closest I got, as he read what I'm sending my application via socket, so you can not decode. Thanks again and feel free to criticize the code avontade friend, I am still in its infancy arduino .. hehe