Pages: [1] 2   Go Down
Author Topic: How to process information from Ethercard  (Read 3233 times)
0 Members and 1 Guest are viewing this topic.
Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50123
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
// 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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50123
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This:
Code:
  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()?

Quote
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:
Code:
byte Ethernet::buffer[700];

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

Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50123
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9675
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

search the forum for textfinder.h
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Quote
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
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9675
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9675
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Below is a textfinder example.

Code:
//
// 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);
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9675
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
//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(;;);

    }
 }
 
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Netherlands
Offline Offline
Newbie
*
Karma: 1
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
          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:
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.
Logged

Pages: [1] 2   Go Up
Jump to: