Pages: [1]   Go Down
Author Topic: WiShield + HTTP Headers  (Read 2826 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm using the WiShield as a client to retrieve data from a webserver and print to the serial port. I am interested in what might be the best way to remove the http headers from the string being printed. Ex. of what is printed to serial:

HTTP/1.1 200 OK
Date: Sun, 30 Jan 2011 01:57:44 GMT
Server: Apache
Last-Modified: Sun, 30 Jan 2011 01:21:55 GMT
ETag: "5d0c-1-1ff372c0"
Accept-Ranges: bytes
Content-Length: 1
Connection: close
Content-Type: text/plain

variable = XxxX


I want to get variable only

thanks
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9541
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The below code does something similar to capture desired data from a very large ocean weather data file. The code counts line feeds in the received file and capture ~line 14 which contains the desired data. From what you posted your data would probably be somewhere near line 11.

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

Client 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()) {
    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

Van Alstyne, TX
Offline Offline
Full Member
***
Karma: 1
Posts: 154
Haven't smoked an Arduino... yet.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Instead of counting lines, the OP should be looking for a blank
line (i.e. two LF characters back to back).  Parsing of the variable
and value should commence after the second LF.  This is the proper
way to parse the HTTP server's output and will continue to work
should the server output a different number of header lines in the
future.

     -Rusty-
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Rusty, got any example code?
Logged

Pages: [1]   Go Up
Jump to: