TextFinder and Ethernet Client troubles

Hey guys,

After having my Arduino and Ethernet board delivered the other day I decided to attempt to make a stock ticker (show the prices of some stocks and update them at an "as live as possible rate"). Well, the first part I haven't had to much trouble with, I wanted to make this as autonomous as possible so I decided to use the TextFinder library to scrape the prices and display them, after a little bit of tinkering I managed to get this to work. Although it only returns the value once, I wish for it to do it maybe once a minute or such. This being my first project with Arduino and the programming language I fear I may have made a mistake in the coding for starting and stopping the client.

I would greatly appreciate it if someone could look over my code and double check what i have done :slight_smile:
As far as I am aware, i have programmed it so that the client connects, and if it makes a connection it then goes to the Google finance page for BP.L and then searches for the field ID of " ref_13539391_l", it then takes the next float and prints it to serial (eventually a display panel as well).

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,177 };
byte gateway[] = { 192,168,1,254 };
byte server[] = { 209,85,147,104 }; // Google

float price;
Client client(server, 80);
TextFinder finder( client );

void setup() {
Ethernet.begin(mac, ip, gateway);
Serial.begin(9600);
delay(1000);
Serial.println("connecting...");

if (client.connect()) {
Serial.println("connected");
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
}

void loop()
{
client.println("GET /finance?q=BP.L HTTP/1.0");
client.println();
if (client.available()){
if (finder.find("ref_13539391_l")){
price = finder.getFloat();
Serial.println(price);
}
}
delay(500);
}

You need to decide whether you leave the loop after each attempt to read - then the client.println is done far too often - or if you process a whole reply inside loop(). In this case you're going to need some while or for loop inside the loop() function.

Once you have fixed this problem, you might discover that the server won't allow you to send a second request after the first was processed. In this case, you need to disconnect and reconnect before redoing the client.println.

Korman

Thanks for the quick reply Korman, If I follow you suggest I either create a separate function outside of loop, or I move everything into loop? Admittedly I'm a little confused how to go about it, but I would assume processing everything in the loop would be the most useful solution as it would reconnect and disconnect every time, would it not; however I could be missing the point entirely.
I will have an attempt and see if I can sort it.
Thanks again

I don't have an Ethernet-shield on hand to test it, but I guess your program should look roughly like this:

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

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,177 };
byte gateway[] = { 192,168,1,254 };
byte server[] = { 209,85,147,104 }; // Google

float price;
Client client(server, 80);
TextFinder finder( client );

void setup() {
  Ethernet.begin(mac, ip, gateway);
  Serial.begin(9600);
  delay(1000);

}

void loop()
{
  Serial.println("connecting...");
  if (client.connect()) {
    Serial.println("connected");
  
  client.println("GET /finance?q=BP.L HTTP/1.0");
  client.println();
  if (finder.find("ref_13539391_l")){
     price = finder.getFloat();
     Serial.println(price);
  }

  // Close connection
  client.stop ();    
 } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }

  // Wait a minute for next round
  delay(60000L);
}

Korman