Somebody else's code that uses a utility called textfinder to extract needed data from a data stream.
/*
* Based on SimpleClientGoogleWeatherDHCP - This one comes with High Temp and Current conditions data.
* gets xml data from http://www.google.com/ig/api?weather=milwaukee,wi
* reads temperature from field: <temp_f data="66" />
* writes temperature to 20x4 LCD. Using Ardiuno Mega 2640
*/
#if ARDUINO > 18
#include <SPI.h> // needed for Arduino versions later than 0018
#endif
#include <Ethernet.h>
#include <EthernetDHCP.h> // uses DHCP code from: http://blog.jordanterrell.com/post/Arduino-DHCP-Library-Version-04.aspx
#include <TextFinder.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(26, 24, 22, 23, 25, 27, 29);
int backLight = 28; // pin 28 will control the backlight
byte mac[] = {
0x90, 0xA2, 0xDA, 0x00, 0x8F, 0xD3 };
byte server[] = {
209,85,229,104 }; // Google
Client client(server, 80);
TextFinder finder( client );
char cond[40];
void setup()
{
pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
lcd.begin(20,4);
lcd.setCursor(0,0);
lcd.print("I'm alive");
if(EthernetDHCP.begin(mac)) {
// begin method returns 1 if successful
lcd.setCursor (0,1);
lcd.print("got IP address, connecting...");
lcd.clear();
delay(500);
}
else {
lcd.clear();
lcd.print("unable to acquire ip address!");
while(true)
; // do nothing
}
}
void loop()
{
if (client.connect()) {
client.println("GET http://www.google.com/ig/api?weather=milwaukee,wi"); // google weather for Milwaukee
client.println();
}
else {
lcd.println("Can not connect to Google Weather");
}
if (client.connected()) {
finder.find("<temp_f data=");
int temperature = finder.getValue();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temperature = ");
lcd.print(temperature);
delay(3000);
lcd.setCursor(0,1);
lcd.print("High Temp ");
finder.find("<high data=");
int temp = finder.getValue();
lcd.print(temp);
delay(3000);
lcd.setCursor(0,2);
lcd.print("Current Conditions ");
finder.find("<condition data");
finder.getString("=","/",cond,(40));
lcd.setCursor(0,3);
lcd.print(cond);
}
else {
lcd.clear();
lcd.print("Can not find xml tag");
}
client.stop();
client.flush();
delay(4000); // wait a minute before next update
}