How to process information from Ethercard

Hi,

I setup the Ethernet card with the Ethercard library. It prints the page content including the header too my serial screen. However I need to capture a piece of the page and compare it to a code inside the arduino. I don't know how to process a part of the page result.

I've done the google and arduino search but maybe I'm looking the wrong way.

Someone here who can help me?

Someone here who can help me?

We need to see your code, to see how the data to be parsed is stored.

We need to see the output, to see how the data is presented.

We need to have a clue what part of the output is of interest.

// Includes and defines

#include <EtherCard.h>

#define REQUEST_RATE 5000 // milliseconds

static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

char website[] PROGMEM = "jecom.nl";

byte Ethernet::buffer[700];
static long timer;

// called when the client request is complete
static void my_result_cb (byte status, word off, word len) {
  Serial.print("<<< reply ");
  Serial.print(millis() - timer);
  Serial.println(" ms");
  Serial.println((const char*) Ethernet::buffer + off);
}

#include <SoftwareSerial.h>

int  val = 0; 
char code[10]; 
char route[] = "020083741B";
int bytesread = 0; 

#define rxPin 7
#define txPin 6



void setup () {
  Serial.begin(57600);
  Serial.println("\n[getDHCPandDNS]");
  
  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
    Serial.println( "Failed to access Ethernet controller");

  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");
  
  ether.printIp("My IP: ", ether.myip);
  // ether.printIp("Netmask: ", ether.mymask);
  ether.printIp("GW IP: ", ether.gwip);
  ether.printIp("DNS IP: ", ether.dnsip);

  if (!ether.dnsLookup(website))
    Serial.println("DNS failed");
  ether.printIp("Server: ", ether.hisip);
  
  timer = - REQUEST_RATE; // start timing out right away
}

void loop()
{
  ether.packetLoop(ether.packetReceive());
  
  SoftwareSerial RFID = SoftwareSerial(rxPin,txPin); 
  RFID.begin(2400);

  if((val = RFID.read()) == 10)
  {   // check for header 
    bytesread = 0; 
    while(bytesread<10)
    {  // read 10 digit code 
      val = RFID.read(); 
      if((val == 10)||(val == 13))
      {  // if header or stop bytes before the 10 digit reading 
        break;                       // stop reading 
      } 
      code[bytesread] = val;         // add the digit       
      bytesread++;                   // ready to read next digit  
    } 

    if(bytesread == 10)
    {  // if 10 digit read is complete 
      Serial.print("TAG code is: ");   // possibly a good TAG 
      Serial.println(code);            // print the TAG code 
      
      if (millis() > timer + REQUEST_RATE) {
    timer = millis();
    Serial.println("\n>>> REQ");
    ether.browseUrl(PSTR("/"), "frizzbar.php", website, my_result_cb);
  }
    }
    bytesread = 0; 
    delay(5000);                       // wait for a second
  } 
}

The contents of the page is only 1234 and I would like to put it into an variable which I can compare.

This:

  SoftwareSerial RFID = SoftwareSerial(rxPin,txPin); 
  RFID.begin(2400);

does not belong in loop(). Why create a new instance of SoftwareSerial on every pass through loop()?

The contents of the page is only 1234 and I would like to put it into an variable which I can compare.

1234 what? Did you mean "1234"?

If so, explain this:

byte Ethernet::buffer[700];

buffer is a variable, and you can compare the contents to other stuff, using the appropriate function.

Hmm yep, sounds logical.

Yes, I am sorry if I was unclear. The content of this page is a numerical code so indeed it's "1234" (or whatever I make it spit out). However the buffer contains first the header and then the content of this page and I need only that numerical code.

However, as I think of it I could just compare the complete buffer but it contains a date and time stamp.

However, as I think of it I could just compare the complete buffer but it contains a date and time stamp.

It would be helpful to see the actual output, but, generally web site data is separated by carriage returns and line feeds into logical blocks. You could discard any data that does not contain data of interest. If you control the output, make it identify the interesting data in some easy to parse way.

I need to capture a piece of the page and compare it to a code inside the arduino

search the forum for textfinder.h

PaulS:

However, as I think of it I could just compare the complete buffer but it contains a date and time stamp.

It would be helpful to see the actual output, but, generally web site data is separated by carriage returns and line feeds into logical blocks. You could discard any data that does not contain data of interest. If you control the output, make it identify the interesting data in some easy to parse way.

<<< reply 5074 ms

HTTP/1.1 200 OK
Date: Fri, 18 Jan 2013 07:47:11 GMT
Server: Apache/2
Vary: Accept-Encoding,User-Agent
Content-Length: 4
Connection: close
Content-Type: text/html

1234

So the info is contained by ether.packetLoop but documentation on ethercard is limited so I don't know how to use it in combination with TextFinder

If the below is what is returned, then you can count the carrage returns in the stream and capture the string between what looks to be 8 and 9.

HTTP/1.1 200 OK
Date: Fri, 18 Jan 2013 07:47:11 GMT
Server: Apache/2
Vary: Accept-Encoding,User-Agent
Content-Length: 4
Connection: close
Content-Type: text/html

1234

zoomkat:
If the below is what is returned, then you can count the carrage returns in the stream and capture the string between what looks to be 8 and 9.

That seems logical indeed. However I am unsure how to do this. The example is quite clear, but since EtherCard is different from normal Ethernet I don't know how to implement it. Or in other words I don't know how to put the output in a stream that I can use with TextFind

Below is a textfinder example.

//
// Read Yahoo Weather API XML
// 03.09.2012
// http://arduino-praxis.ch


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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAD };
byte ip[] = { 192, 168, 1, 102 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };

// Server Yahoo
IPAddress server(87,248,122,181);

EthernetClient client;
TextFinder  finder( client );  

char place[50];
char hum[30];


void setup()
{
  // Start Ehternet
  Ethernet.begin(mac, ip);
  // Start Serial Port
  Serial.begin(9600);
  Serial.println("Setup...");
}


void loop()
{
  if (client.connect(server, 80))
  {
    // Call Wetter-API
    // w: ID from your City
    // http://weather.yahooapis.com/forecastrss?w=12893459&u=c
    ///
    Serial.println("Connect to Yahoo Weather...");
    client.println("GET /forecastrss?w=12893459&u=c HTTP/1.0");
    client.println("HOST:weather.yahooapis.com\n\n");
    client.println();
    Serial.println("Connected...");
  } 
  else
  {
    Serial.println(" connection failed");
  } 
 

  if (client.connected())
  {
    
    // Humidity
   if ( (finder.getString("<yweather:atmosphere humidity=\"", "\"",hum,4)!=0) )
   {
     Serial.print("Humidity:  ");
     Serial.println(hum);
   } 
   else
   {
     Serial.print("No Humidity Data");
   }
    
    
    // Place/City
    if ( (finder.getString("<title>Conditions for ", " ",place,50)!=0) )
    {
      Serial.print("City:  ");
      Serial.println(place);
    }
    
    
    // Temperature
    if(finder.find("temp=") )
    {
     int temperature = finder.getValue();
     Serial.print("Temp C:  ");
     Serial.println(temperature);
   }
   else
   {
     Serial.print("No Temperature Data");
   }
   
         
  // END XML
  }
  else
  {
    Serial.println("Disconnected"); 
  }
 
  client.stop();
  client.flush();
  delay(60000); 
}

Yeah I found some examples but since I am not using the Ethernet library but the Ethercard one.

It's not TextFinder finder (ether / Ethernet), so what is?

Old code that counts line feeds in the data stream to capture the line with the wave hight from a NOAA weather site.

//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

EthernetClient 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(server, 80)) {
    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(;;);

    }
 }

The usage of Ethercard.h instead of Ethernet.h is what is causing the problems.

Client.read does not exist and I can't figure out how to put the buffer into a char instead of just getting printed.

After some more googling I found a webscraping example for Ethercard and this works great.

          Serial.print("Code ");Serial.println(displayBuff);
           char fCode[] = "1111";
          Serial.print("Fcode ");Serial.println(fCode);
         
          if (displayBuff ==fCode) 
          {
            Serial.print("Equal.");
          }

The result of this is

Code 1111
Fcode 1111

However the code does not return my print. I believed at first this was an issue because of the numerical string, but using ABCDE gives the same answer.

The usage of Ethercard.h instead of Ethernet.h is what is causing the problems.

Client.read does not exist and I can't figure out how to put the buffer into a char instead of just getting printed.

No, the use of Ethercard is NOT causing your problem. Your refusal to look at the code and understand what it is doing is.

In my_result_cb(), you have a statement that prints the whole packet returned by the GET request, as a NULL terminated array of char, There are string functions that let you find stuff in that array, just like TextFinder does. If you want to parse that data, strtok() could be used to get each line, delimited by CR or LF.

You still haven't told us of you have any control over the website you are accessing, or whether you could change the output to make parsing it easier.