Read word from website

I am trying to find a word on a webpage, example

Read "twitter.com"

if the word "hello" is found
{

do something

}

if not
{

do something else

}

I've tried using client.find("wordimlookingfor")
however that just finds the next integer and passes the value to a variable

Any suggestions

Any suggestions

Post some real code.

We can't guess whether you are saving the data, and then trying to parse the data, or if you expect various undefined classes to have methods that will perform magic for you.

I am reading data from http://magicseaweed.com/syndicate/rss/index.php?id=701&unit=uk

I want to find the word "swell", if it exists do something if not do something else.

At the moment I can find the word "swell" then pass the next numerical value into 'waveheight'

if (client.connected()) {
if(client.find("Swell"))
{//look for the wave height and pass the value to variable
waveheight = client.parseFloat();
}

At the moment I can find the word "swell" then pass the next numerical value into 'waveheight'

So, the problem is?

At the moment I have this, however if I change "Swell" to a word that doesn't exist. Pin 13 does not go high

if(client.find("Swell"))
{//look for the wave height and pass the value to variable
waveheight = client.parseFloat();
}
else{
digitalWrite(13,HIGH);
}

I am trying to find a way to check if "swell" exists

cammoore:
At the moment I have this, however if I change "Swell" to a word that doesn't exist. Pin 13 does not go high

  • Arduino board used?
  • other hardware used?
  • circuit schematics?
  • full sketch code?

If, for example, I'm acting as a mind reader and make the guess that you are using an Arduino UNO board and an Ethernet Shield with Wiznet W5100, then the Ethernet hardware is controlled by SPI bus, and pin-13 is the SPI clock pin of the UNO board.

You cannot use the SPI clock pin as a general output pin, while using pin-13 for any connected SPI device at the same time.

But I'm really bad in mind reading, so possibly you are doing much different bullshit than what I described.

You're a pretty good mind reader, I am using the UNO with the W5100. Here is the code I am using. It has LED's hooked up to pins 7,8 and 9. I've tried changing using pin 13 to another LED I have hooked up to the board with no luck.

#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>
EthernetClient client;
TextFinder finder( client );
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char serverName[] = "ndbc.noaa.gov";

int red=7,green=8,blue=9; // PWM pins initialized to pass values of RGB
float waveheight; // float value that holds wave height
void setup()
{
Serial.begin(9600);
pinMode(red ,OUTPUT);
pinMode(green ,OUTPUT);
pinMode(blue ,OUTPUT);
if (Ethernet.begin(mac) == 0)
{ // start ethernet using mac & IP address
while(true) ;
}
delay(2000); // give the Ethernet shield 2 second to initialize
}
void loop()
{
if (client.connect(serverName, 80)>0) {
client.println("GET NDBC - Station 62081 - K2 Buoy Observations");
}
if (client.connected()) {
if(client.find("Swell"))
{//look for the wave height and pass the value to variable
waveheight = client.parseFloat();
}
else{
analogWrite(red ,255 );
analogWrite(green ,255 );
analogWrite(blue ,255);
}
//The code below lights up RGB module light depending on the wave height
if ((waveheight>=0)&&(waveheight<3))
{
analogWrite(red ,0 );
analogWrite(green ,0 );
analogWrite(blue ,255);
}
if ((waveheight>=3)&&(waveheight<8))
{
analogWrite(red ,0);
analogWrite(green ,255 );
analogWrite(blue ,0);
};
if ((waveheight>=8)&&(waveheight<100))
{
analogWrite(red ,255 );
analogWrite(green ,0);
analogWrite(blue ,0);
}

client.stop();//Disconnect
}
}

cammoore:
I am using the UNO with the W5100.
...
Here is the code I am using. It has LED's hooked up to pins 7,8 and 9.

On an UNO the pins 7 and 8 are NOT PWM pins, so you cannot use pins 7 and 8 with the analogWrite() function.

Use digitalWrite with pin-7 and pin-8 instead!

BTW: When posting code into this forum

  • do some nice code formatting using the Arduino IDE "Tools - Auto Format" function
  • put the code into "code tags" (see "How to use this forum", chapter 7)

Thats not the issue I'm having, the rest of the code works fine. When the word "swell" exists it reads the number next to it and turns the corresponding LED on. When the word swell doesn't exist, the code doesn't work, it treats the variable 'waveheight' as 0 and turns on the corresponding LED. Where it should turn ALL of the LED's on.

I am trying to find a way where I can search for a word off a webpage, if that word exists return some value, if it doesn't resist, return a different value.

cammoore:
Thats not the issue I'm having, the rest of the code works fine. When the word "swell" exists it reads the number next to it and turns the corresponding LED on.

Really? You actually get a valid HTTP response to the malformed HTTP request you are sending?

This is a malformed HTTP GET request:

 client.println("GET http://www.ndbc.noaa.gov/data/latest_obs/62081.rss");

If I'd make guess you possibly would get a "404 - Not Found" or "503 - Server Error" or something like that back as a response only. But your Arduino will receive a valid HTTP response?

BTW: On the actual response in my webbrowser I don't see the word "swell", but I see "Significant Wave Height:" as text.

So sometimes the same page shows ""Significant Wave Height:" and you want to ignore that, but at different times the same page shows "swell" and you want to find that?

  if (client.connect(serverName, 80)>0) {
    client.println("GET http://www.ndbc.noaa.gov/data/latest_obs/62081.rss");
  }

A GET request does NOT involve a protocol (http://) or a server name (www.ndbc.noaa.gov). You connected to the server already, so the get request should simply be:

   client.println("GET data/latest_obs/62081.rss");

When the word swell doesn't exist, the code doesn't work,

You need to look at how the find() method works. It reads and discards data UNTIL IT FINDS THE SEARCH PHRASE. Only then does it return.

You REALLY want to read and store all the data from the server and THEN parse the data. Only that way can you take action on a phrase not being in the response (because you know what the whole response is).

So sometimes the same page shows ""Significant Wave Height:" and you want to ignore that, but at different times the same page shows "swell" and you want to find that?

Yes that is correct, but I also want to be able to tell if "Significant Wave Height:" isn't there.

I have fixed the request. Thank you

but I also want to be able to tell if "Significant Wave Height:" isn't there.

The find() method really should be called the waitForStringToAppearInStream() function. It can NOT determine if the string it is supposed to find will never appear. Get over it.