Handling Returned Data from web server

I started this thread http://arduino.cc/forum/index.php/topic,64696.0.html

but i have made it out of the concept portion and am now looking for some help dealing with the returned data.

This is what i'm having returned to the serial monitor starting simple arduino client test

connecting... connected bob: online 1 disconnecting.

I am looking to turn on an LED if "bob" returned and better yet find a way to tie in the status of online to bob.

i guess i'm really just looking for some direction, maybe a link that shows me how to manipulate data coming into the serial monitor. if that's even possible

Coming into the serial monitor? As in, on the computer?

You'll need to write a custom application to do that. There are serial communication libraries in Python and Java, to name a couple.

This is the code i’m using, is the data coming through my usb cable or through the ethernet?
i assumed it was coming from the ethernet, but i guess i could be wrong.

//zoomkat 11-13-10
//simple ethernet client test code
//for use with IDE 0021 and W5100 ethernet shield
//modify the arduino lan ip address as needed
//open serial monitor to see what the arduino receives

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 100 };
byte server[] = { 65, 23, 97, 190 }; // zoomkat

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  Serial.println("starting simple arduino client test");
  Serial.println();
  
  delay(1000);
  
  Serial.println("connecting...");
  
  if (client.connect()) {
    Serial.println("connected");
    client.println("GET http://server.net/serverpage.asp");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
   
    String stringOne = c;
     Serial.print(stringOne);
  }
  
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;);
  }
}

Any Serial.print function call writes data over the USB connection to your computer (I'm assuming you have your Arduino plugged in to your computer and no other device connected to the UART).

Any client.print function call writes data through SPI to the ethernet shield which sends the data over the ethernet connection.

Ok, it looks like i may need to take a step back then?

I'm trying to have a LED turn on if a certain value is returned from the page. Do i need to handle the data in a different way? i know i don't have to print it but could i run a condition off of the "stringOne" variable to turn on a LED? if not, i need to find out how to access the data coming back from the page

 String stringOne = c;
     Serial.print(stringOne);
  if (client.available()) {
    char c = client.read();
   
    String stringOne = c;
    Serial.print(stringOne);
  }

All that this is going to do is print the incoming ethernet data out to your serial monitor. Yes, you can do a condition check on that incoming data, which is probably what you want to do.

Keep in mind though that if you're crafting a new String object from just one character, you can probably just keep the datatype as a "char" and save your program time and memory by omitting the String object.

If you want to build up a longer String, which is a perfectly reasonable thing to do, then declare a String outside of setup() and loop()

String incoming;

void setup() { ... }

void loop() {
  ...

  while(client.available()) {
    incoming += client.read();
  }

  if(incoming == "turn on LED") {
    digitalWrite(LED_PIN, HIGH);
    incoming = "";
  }
}

Thanx, ok so now i tried to output incoming to the serial console to check what is being returned and i got a bunch of numbers code:

void loop()
{
while(client.available()) {
    incoming += client.read();
    Serial.println (incoming);
  }

  if(incoming == "turn on LED") {
   
    digitalWrite(13, HIGH);
    incoming = "";
  }
  
  
   
}

output:

starting simple arduino client test

connecting...
connected
40
4084
4084104
4084104101
408410410132
40841041013299
4084104101329997
4084104101329997117
4084104101329997117115
4084104101329997117115101
408410410132999711711510141
40841041013299971171151014188
40841041013299971171151014188116
40841041013299971171151014188116114
40841041013299971171151014188116114101
40841041013299971171151014188116114101109
40841041013299971171151014188116114101109101
4084104101329997117115101418811611410110910198
4084104101329997117115101418811611410110910198117
408410410132999711711510141881161141011091019811798
40841041013299971171151014188116114101109101981179898
4084104101329997117115101418811611410110910198117989897
408410410132999711711510141881161141011091019811798989758
40841041013299971171151014188116114101109101981179898975832
40841041013299971171151014188116114101109101981179898975832105
40841041013299971171151014188116114101109101981179898975832105110
4084104101329997117115101418811611410110910198117989897583210511045
4084104101329997117115101418811611410110910198117989897583210511045103
408410410132999711711510141881161141011091019811798989758321051104510397
408410410132999711711510141881161141011091019811798989758321051104510397109
408410410132999711711510141881161141011091019811798989758321051104510397109101
40841041013299971171151014188116114101109101981179898975832105110451039710910160
4084104101329997117115101418811611410110910198117989897583210511045103971091016098
4084104101329997117115101418811611410110910198117989897583210511045103971091016098114
408410410132999711711510141881161141011091019811798989758321051104510397109101609811462
40841041013299971171151014188116114101109101981179898975832105110451039710910160981146232
4084104101329997117115101418811611410110910198117989897583210511045103971091016098114623299
4084104101329997117115101418811611410110910198117989897583210511045103971091016098114623299104
408410410132999711711510141881161141011091019811798989758321051104510397109101609811462329910497
408410410132999711711510141881161141011091019811798989758321051104510397109101609811462329910497122
408410410132999711711510141881161141011091019811798989758321051104510397109101609811462329910497122122
408410410132999711711510141881161141011091019811798989758321051104510397109101609811462329910497122122121
40841041013299971171151014188116114101109101981179898975832105110451039710910160981146232991049712212212149
4084104101329997117115101418811611410110910198117989897583210511045103971091016098114623299104971221221214956
408410410132999711711510141881161141011091019811798989758321051104510397109101609811462329910497122122121495654
40841041013299971171151014188116114101109101981179898975832105110451039710910160981146232991049712212212149565452
4084104101329997117115101418811611410110910198117989897583210511045103971091016098114623299104971221221214956545258
408410410132999711711510141881161141011091019811798989758321051104510397109101609811462329910497122122121495654525832
408410410132999711711510141881161141011091019811798989758321051104510397109101609811462329910497122122121495654525832105
408410410132999711711510141881161141011091019811798989758321051104510397109101609811462329910497122122121495654525832105110
40841041013299971171151014188116114101109101981179898975832105110451039710910160981146232991049712212212149565452583210511045
40841041013299971171151014188116114101109101981179898975832105110451039710910160981146232991049712212212149565452583210511045103
4084104101329997117115101418811611410110910198117989897583210511045103971091016098114623299104971221221214956545258321051104510397
4084104101329997117115101418811611410110910198117989897583210511045103971091016098114623299104971221221214956545258321051104510397109
4084104101329997117115101418811611410110910198117989897583210511045103971091016098114623299104971221221214956545258321051104510397109101
408410410132999711711510141881161141011091019811798989758321051104510397109101609811462329910497122122121495654525832105110451039710910160
40841041013299971171151014188116114101109101981179898975832105110451039710910160981146232991049712212212149565452583210511045103971091016098
40841041013299971171151014188116114101109101981179898975832105110451039710910160981146232991049712212212149565452583210511045103971091016098114
4084104101329997117115101418811611410110910198117989897583210511045103971091016098114623299104971221221214956545258321051104510397109101609811462
408410410132999711711510141881161141011091019811798989758321051104510397109101609811462329910497122122121495654525832105110451039710910160981146232
40841041013299971171151014188116114101109101981179898975832105110451039710910160981146232991049712212212149565452583210511045103971091016098114623250

Future question, once i get this figured out. Is there a way to search a string for a certain word anywhere in the string? kinda like a contains?

Try casting client.read() to a char? If that fails, cast to a const char?

What you’re getting are the ASCII values of certain characters. You can check an ASCII table to see just what that data is, although figuring out how to get your string to know that it should append character data instead of number data is probably a better use of your time.

ok when i do this

void loop()
{
    while(client.available()) 
    {
    char incoming = client.read();
     Serial.println (incoming);
    
    }
    
      
    if(incoming == "turn on LED") 
    {
     
    digitalWrite(13, HIGH);
    incoming = "";
             
    }
     
}

i get the letters but they go down the screen one at a time

and when i do this( put the output outside the "While condition" it seems to just generate a bunch of blanks (the scroll bar starts shrinking rapidly)

void loop()
{
    while(client.available()) 
    {
    char incoming = client.read(); 
    }
         Serial.println (incoming);
      
    if(incoming == "turn on LED") 
    {
     
    digitalWrite(13, HIGH);
    incoming = "";
             
    }
     
}

Wait, what happened to the code I wrote? Lol, that would have worked for you!

The important bits are: declare your String outside setup() and loop(), and concatenate to your String rather than just setting it (use += instead of =).

Here's that code again, with a cast to "const char" - hopefully the compiler will know you want to add character data instead of numbers this way.

String incoming;

void setup() { ... }

void loop() {
  ...

  while(client.available()) {
    incoming += (const char)client.read();
  }

  if(incoming == "turn on LED") {
    digitalWrite(LED_PIN, HIGH);
    incoming = "";
  }
}

The other thing is that the webpage has to contain absolutely nothing but the text “turn on LED”.

But I think you’re going to get a full HTTP response including HTTP headers… which will be a PITA to parse.

I understand why it is writing it one char at a time, because i’m doing the output in the loop but what i don’t get is when i output the variable outside the loop all i get is a bunch of blanks. (non stop, it seems)

i’m pretty sure this is the only thing getting returned from the page.

(
T
h
e
 
c
a
u
s
e
)
X
t
r
e
m
e
b
u
b
b
a
:
 
i
n
-
g
a
m
e
<
b
r
>
 
c
h
a
z
z
y
1
8
6
4
:
 
i
n
-
g
a
m
e
<
b
r
>
 
2

current code

void loop()
{
    while(client.available()) 
    {
    char incomingString =+ client.read(); 

     Serial.println (incomingString);
    }
       
      
   // if(incoming == "turn on LED") 
    //{
     
    //digitalWrite(13, HIGH);
   // incoming = "";
             
    //}
     
}

JustXtreme: void loop() {     while(client.available())     {     char incoming = client.read();     }          Serial.println (incoming);           if(incoming == "turn on LED")     {     digitalWrite(13, HIGH);     incoming = "";     } }

In this case, the variable incoming is defined inside the loop. Because of variable scope, the variable "no longer exists" once the program leaves the scope it was defined in (which is the "if" block in this case).

Ok, so if i understand you correctly that if statement should work the first time around?(if my output was “turn on LED”)
if so shouldn’t i still be able to output the “incoming” variable once before it gets overwritten? nevermind, i think i understand that part

I wonder if i need to do something like this
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1222095474

Some client test code that gets wave period data from a fairly large web page.

//zoomkat 12-22-10
//simple ethernet client test code
//for use with IDE 0021 and W5100 ethernet shield
//modify the arduino lan ip address as needed
//open serial monitor to see what the arduino receives
//push the shield reset button to run client again

#include <SPI.h>
#include <Ethernet.h>
String readString, readString1;
int x=0;
char lf=10;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 102 };
byte server[] = { 140, 90, 238, 27 }; // NOAA

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  Serial.println("starting simple arduino client test");
  Serial.println();
  Serial.println("connecting...");

  if (client.connect()) {
    Serial.println("connected");
    client.println("GET /data/5day2/44013_5day.txt HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    //Serial.print(c);  // uncomment to see raw feed
    if (c==lf) x=(x+1);
    if (x==14) readString += c;
    //readString += c;
  }

  if (!client.connected()) {
     client.stop();

    Serial.println("Current data row:" );
    Serial.print(readString);
    Serial.println();
    readString1 = (readString.substring(41,43));
    Serial.println();
    Serial.print("DPD sec: ");
    Serial.println(readString1);
    Serial.println("done");

    for(;;);

    }
 }

That seems to work perfectly, now to the next step. creating an array from the string. or search the string for a word? something like this

string = bob:in-game|paul:in-game|;2 if string contains paul then varb=true end if

Example of using indexOf string function to check for a substring in readString.

if(readString.indexOf("on") >0)//checks for on
           {
             digitalWrite(4, HIGH);    // set pin 4 high
             Serial.println("Led On");
             }
if(readString.indexOf("off") >0)//checks for off
           {
             digitalWrite(4, LOW);    // set pin 4 low
             Serial.println("Led Off");
             }

So that basically checks for the "on" or "off" in the string ?

At some point you gotta just dig in, read the tutorials, read the docs, and read auxiliary articles pertaining to your issue. If your issue is that you don't know how to program in C/C++, read beginners C/C++ articles.

Here's the documentation for the String object: http://arduino.cc/en/Reference/StringObject

At some point you gotta just dig in, read the tutorials, read the docs, and read auxiliary articles pertaining to your issue. If your issue is that you don't know how to program in C/C++, read beginners C/C++ articles.

At some point, you have to understand that you can not destroy the hardware by loading a sketch on it and trying to run it. Personal observation is much more useful than asking questions like that last one on the forum.