Author Topic: Equivalent of client.find and client.parseFloat  (Read 4803 times)

olman011

  • Newbie
  • *
  • Posts: 4
Equivalent of client.find and client.parseFloat
« on: December 02, 2013, 08:11:27 pm »
I'm rewriting some code that previously used the ethernet shield and library to read a .txt file from the web and search it for values. The text file had data in it and looks like this:

<pit>234<fan>98<temp>195.......

So the following code would find the <pit> tag and return the value 234.
I'm not sure how to carry this out with the DigiX libraries.
Any suggestions would be greatly appreciated.

Code: [Select]
if (client.available()) {


    if(client.find("<pit"))
    {
      client.find(">");  // seek past the next '>'
      value = client.parseFloat();
      Serial.print("pit is set at ");
      Serial.println(value);  // value is printed

    }

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: Equivalent of client.find and client.parseFloat
« Reply #1 on: December 02, 2013, 10:26:04 pm »
Interesting - I haven't come across these before

I will put support for these on my list - but for now DigiFi gets the whole response where as Ethernet feeds it a bit at a time - so we can't use these same functions

But standard string manipulation functions will work

Untested off the top of my head - but should get you in the right direction:
Code: [Select]

if(!wifi.get("digistump.com","/test.txt")){
    Serial.println("error");
    while(1){delay(1000);} //stop running
}

//if we are here it worked and we are ready to get the body of the returned data
String body = wifi.body();

//body is the string output body from digifi
String t;
t = body.substring(body.indexOf("<pit>")); //make t everything after <pit> http://arduino.cc/en/Tutorial/StringSubstring http://arduino.cc/en/Tutorial/StringIndexOf
t = t.substring(0,t.indexOf("<")); //make t everything before the next <
float pit = t.toFloat(); //convert to float - pit is now a float equal to 234 - repeat for other numbers - if int is all that is needed you could use .toInt() instead
« Last Edit: December 02, 2013, 10:28:08 pm by digistump »

olman011

  • Newbie
  • *
  • Posts: 4
Re: Equivalent of client.find and client.parseFloat
« Reply #2 on: December 02, 2013, 10:49:43 pm »
Thanks, that totally worked!
I'm really liking this DigiX.

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: Equivalent of client.find and client.parseFloat
« Reply #3 on: December 02, 2013, 11:47:52 pm »
Glad that helped -

I'm thinking this might be better than find()

Code: [Select]

String parseBetween(String start, String end, String text){
    text = text.substring(text.indexOf(start));
    text = text.substring(0,text.indexOf(end));
    return text;
}


If you put that outside of loop and setup in your sketch then you can just do (again untested)

Code: [Select]

wifi.get("digistump.com","/test.txt");
String body = wifi.body();
float pit = parseBetween("<pit>","<",body).toFloat();