ide0021 looking for alternative to contains

Hi, I'm a noob :slight_smile:

Im using ide0021, a non sd ethernet shield and a sketch http://www.scienceprog.com/getting-hands-on-arduino-ethernet-shield/ that was designed for an earlier IDE that didnt include the String library.

I cannot find a way of making the LED go high from the readString .

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




byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 
  192,168,1,9 };       // ip in lan
byte gateway[] = { 
  192, 168, 1, 1 };       // internet access via router
byte subnet[] = { 
  255, 255, 255, 0 }; //subnet mask
Server server(80); //server port
int ledPin = 4; // LED pin
char link[]="http://www.google.co.uk"; //link data
// char c[10];
String readString = String(30); //string for fetching data from address

boolean LEDON = false; //LED status flag
//String String2 = "L=1";

void setup()
{
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  //Set pin 4 to output

    pinMode(ledPin, OUTPUT);
  //enable serial datada 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() < 30) {
          //store characters to string

          readString +(c); ///changed from" readString.append(c);"
        } 
        //output chars to serial port
        Serial.print(c);
        //delay(100);
        //if HTTP request has ended
        if (c == '\n') 

        {
          // check if LED should be lighted


          if(readString.contains("L=1"))///heres the problem, giving error: 'class string' has no member named 'contains'.
          //I've inexpertly tried using indexOf

          {
            //led has to be turned ON
            digitalWrite(ledPin, HIGH); // set the LED on
            //delay(1000); 
            LEDON = true;
            Serial.println( "ON");
          }

          else

          {
            //led has to be turned OFF
            digitalWrite(ledPin, LOW); // set the LED OFF
            Serial.println ("off");
            LEDON = false;
          }






          // now output HTML data starting with standart header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          //set background to yellow
          client.print("<body style=background-color:grey>");

          //send first heading
          client.println("<font color='red'><h1> WEB ARDUINO TEST</font></h1>");
          client.println("<hr />");
          client.println("<hr />");

          //controlling led via checkbox
          client.println("<h1>LED control</h1>");
          //address will look like http://192.168.1.110/?L=1 when submited
          //client.println("<form method=get name=LED><input type=checkbox name=L value=1>LED
<input type=submit value=submit></form>");
          client.println("<form method=get name=LED><input type=checkbox name=L value=1>LED
<input type=submit value=submit></form>");
          client.println("
");

          //printing LED status
          client.print("<font size='5[ch8242]>LED status: ");
          if (LEDON)
            client.println("<font color='green' size='5[ch8242]>ON");
          else
            client.println("<font color='grey' size='5[ch8242]>OFF");
          client.println("<hr />");
          client.println("<hr />");
          client.println("</body></html>");
          //clearing string for next read
          readString="";
          //stopping client
          client.stop();
        }



      }



    }




  }
}
//*******************************

Any answers or hints or clues would be most gratefully received-Im finding it hard to visualise how the readString is behaving.
many thanx
mmj

readString +(c); ///changed from" readString.append(c);"

Why did you make this change? The readString variable is NOT modified by this statement.

This might explain why the rest of the code doesn't work, since the rest of the code sees an empty string.

thanks for response
i changed it because it gave error: 'class String' has no member named 'append'

readString +(c);

If you want to append 'c' to 'readString' use:

readString += c;

thanks, I've amended the code and am now trying to get a consequence from the LED "on" URL.. the old code had it like this:

if(readString.contains("L=1"))
           {
             //led has to be turned ON
             digitalWrite(ledPin, HIGH);    // set the LED on
             LEDON = true;
           }else{
             //led has to be turned OFF
             digitalWrite(ledPin, LOW);    // set the LED OFF
             LEDON = false;

but it seems that doesnt wash anymore
thanks again for your help

Hi
Ive used the length function with success, though its a bit blunt..Now ive got a working system I can tweak it till its better :slight_smile:

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




byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 
  192,168,1,9 };       // ip in lan
byte gateway[] = { 
  192, 168, 1, 1 };       // internet access via router
byte subnet[] = { 
  255, 255, 255, 0 }; //subnet mask
Server server(80); //server port
int ledPin = 4; // LED pin
char link[]="http://www.google.co.uk"; //link data
// char c[10];
String readString = String(30); //string for fetching data from address

boolean LEDON = false; //LED status flag
//String String2 = "L=1";

void setup()
{
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  //Set pin 4 to output

    pinMode(ledPin, OUTPUT);
  //enable serial datada 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() < 30) {
          //store characters to string

         readString += c; 
        } 
        //output chars to serial port
        //Serial.print(c);
        delay(100);
        Serial.println ( readString);
        //delay(100);
        //if HTTP request has ended
        if (c == '\n') 

        {
          // check if LED should be lighted


if (readString.length() >19) // this works but seems rather inelegant:)
          

          {
            //led has to be turned ON
            digitalWrite(ledPin, HIGH); // set the LED on
            //delay(1000); 
            LEDON = true;
            Serial.println( "ON");
          }

          else

          {
            //led has to be turned OFF
            digitalWrite(ledPin, LOW); // set the LED OFF
            Serial.println ("off");
            LEDON = false;
          }






          // now output HTML data starting with standart header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          //set background to yellow
          client.print("<body style=background-color:grey>");

          //send first heading
          client.println("<font color='red'><h1> WEB ARDUINO TEST</font></h1>");
          client.println("<hr />");
          client.println("<hr />");

          //controlling led via checkbox
          client.println("<h1>LED control</h1>");
          //address will look like http://192.168.1.110/?L=1 when submited
          //client.println("<form method=get name=LED><input type=checkbox name=L value=1>LED
<input type=submit value=submit></form>");
          client.println("<form method=get name=LED><input type=checkbox name=L value=1>LED
<input type=submit value=submit></form>");
          client.println("
");

          //printing LED status
          client.print("<font size='5[ch8242]>LED status: ");
          if (LEDON)
            client.println("<font color='green' size='5[ch8242]>ON");
          else
            client.println("<font color='grey' size='5[ch8242]>OFF");
          client.println("<hr />");
          client.println("<hr />");
          client.println("</body></html>");
          //clearing string for next read
          readString="";
          //stopping client
          client.stop();
        }



      }

    }

  }
}
//*******************************

any input is really welcome, and thanks again for your help
mmj

What does this print? What is the output in the serial monitor?

Serial.println (readString);

it was to check that the readString was getting filled. it results in:

3

G
GE
GET
GET 
GET /
GET /?
GET /? 
GET /? H
GET /? HT
GET /? HTT
GET /? HTTP
GET /? HTTP/
GET /? HTTP/1
GET /? HTTP/1.
GET /? HTTP/1.1
GET /? HTTP/1.1

GET /? HTTP/1.1

off
G
GE
GET
GET 
GET /
GET /?
GET /?L
GET /?L=
GET /?L=1
GET /?L=1 
GET /?L=1 H
GET /?L=1 HT
GET /?L=1 HTT
GET /?L=1 HTTP
GET /?L=1 HTTP/
GET /?L=1 HTTP/1
GET /?L=1 HTTP/1.
GET /?L=1 HTTP/1.1
GET /?L=1 HTTP/1.1

GET /?L=1 HTTP/1.1

ON

so I then used the length method and found the tipping point number by trial and error :-[

so I then used the length method and found the tipping point number by trial and error

Why? You notice (or perhaps you didn't) that the message ends with a carriage return. So, terminate reading when a carriage return is detected, and don't add that to the string.

Suppose the string had been "GET /?L=0 HTTP/1.1". Would you still turn the LED on?

Turning the LED on because you have read 19 characters is by no means the correct thing to do.

Now that you are getting a complete string (although not quite correctly), it's time to learn how to properly parse the string. The indexOf function will tell you where the ? is in the string. Create a substring from the next character on ("L=0 HTTP/1.1"). Then, use indexOf on that string to find the space. Create a substring up to that position ("L=0"). Then, parse this string the same way to get the L and the 0 or 1. Decide whether to turn the LED on or off intelligently.

You may want to look at the code in the below post to see how to avoid the looping in the example you used.

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

thanks, thats all very helpful and appreciated
mmj

Hi, here's a slightly better version.

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




byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 
  192,168,2,2 };       // ip in lan
byte gateway[] = { 
  //192, 168, 1, 1 };// internet access via router
  192,168,2,1 };       // internet access via ethernet connection on mac
byte subnet[] = { 
  255, 255, 255, 0 }; //subnet mask
Server server(80); //server port
int ledPin = 4; // LED pin

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

boolean LEDON = false; //LED status flag

 void setup()
{
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  //Set pin 4 to output
    pinMode(ledPin, OUTPUT);
  //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() < 30) 
        {
          readString += c; //store characters to string
        } 
        //if HTTP request has ended
        if (c == '\n') 

        {
          // check if LED should be lighted

          delay(1000);
          int locateQuestionmark = readString.indexOf('?');
          Serial.println (readString);
          Serial.println ("The index of the ? in the string " + readString + " is " + locateQuestionmark);


          if (readString.substring(6,9) == "L=1")       

          {
            //led has to be turned ON
            digitalWrite(ledPin, HIGH); // set the LED on
            //delay(1000); 
            LEDON = true;
            Serial.println( "ON");
          }

          else

          {
            //led has to be turned OFF
            digitalWrite(ledPin, LOW); // set the LED OFF
            Serial.println ("off");
            LEDON = false;
          }






          // now output HTML data starting with standart header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          //set background to yellow
          client.print("<body style=background-color:grey>");

          //send first heading
          client.println("<font color='red'><h1>  WEB ARDUINO TEST</font></h1>");
          client.println("<hr />");
          client.println("<hr />");

          //controlling led via checkbox
          client.println("<h1>LED control</h1>");
          //address will look like http://192.168.1.110/?L=1 when submited
          //client.println("<form method=get name=LED><input type=checkbox name=L value=1>LED
<input type=submit value=submit></form>");
          client.println("<form method=get name=LED><input type=checkbox name=L value=1>LED
<input type=submit value=submit></form>");
          client.println("
");

          //printing LED status
          client.print("<font size='5[ch8242]>LED status: ");
          if (LEDON)
            client.println("<font color='green' size='5[ch8242]>ON");
          else
            client.println("<font color='grey' size='5[ch8242]>OFF");
          client.println("<hr />");
          client.println("<hr />");
          client.println("</body></html>");
          //clearing string for next read
          readString="";
          //stopping client
          client.stop();
        }



      }



    }




  }
}
//*******************************

next stage is to find out how to take this off my LAN and accessible via the www.

You probably don't need to use the #include <String.h> with the latest IDE. Also, if you want to make the arduino accessable from the outside, if you use a router, in the router port foward port 80 to the arduino IP address on your lan, and then use a dynamic IP address service like you.no-ip.com to connect from the net.