Go Down

Topic: Removing HTTP Headers (Read 1 time) previous topic - next topic

edan_gelo

I'm using the DFRobot Ethernet Shield as a client to retrieve data from a webserver(my website) 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. This what is printed to myserial:

Code: [Select]

connecting...
connected
HTTP/1.1 200 OK
Date: Sun, 16 Jun 2013 12:35:35 GMT
Server: Apache
Last-Modified: Sun, 16 Jun 2013 07:44:05 GMT
Accept-Ranges: bytes
Content-Length: 27
Connection: close
Content-Type: text/plain

aduh biyung aing gak ngerti


and this is my Arduino Sketch :

Code: [Select]

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,10,105);
char server[] = "webme.net10.net";

EthernetClient client;

void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip);
delay(1000);
Serial.println("connecting...");
if (client.connect(server, 80)) {
Serial.println("connected");
client.println("GET /infonya.log HTTP/1.1");
client.println("Host: webme.net10.net");
client.println("Connection: close");
client.println();
} else {
Serial.println("connection failed");
}
}

void loop() {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
}



So what should i do with my sketch if i just want aduh biyung aing gak ngerti printed in my serial monitor???

sonnyyu

#1
Jun 16, 2013, 03:01 pm Last Edit: Jun 16, 2013, 03:06 pm by sonnyyu Reason: 1
Use Arduino HttpClient;-

In normal usage, handles the outgoing request and Host header.  The returned status code is parsed for you, as is the Content-Length header (if present).

check SimpleHttpExample come with it for parse body.

https://github.com/amcewen/HttpClient

sonnyyu

Plan B;-

Wrote yourself Arduino TCP-Client and a Java Server for PC, no more http no more header. 100% control by yourself.

simple Arduino TCP-Client using the Ethernetshield, DHCP and a Java Server


zoomkat

Maybe a little clunky, but it looks like the header and body are separated by a pair of cr/lf bytes, so you probably could use this to start the capture of the body.
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

edan_gelo

thank you sonnyyu,,, i fix my problem, this is very help me. But one more question. What should i do with httpclient code, if i want the data loop, receive forever if there are nomore data that i input???

sonnyyu


But one more question. What should i do with httpclient code, if i want the data loop, receive forever if there are nomore data that i input???

What do you try to do? code please.

edan_gelo

#6
Jun 17, 2013, 04:36 am Last Edit: Jun 17, 2013, 04:37 am by edan_gelo Reason: 1
This is my picture when i still using my code before (not using httpclient)



and this is my code

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>



String inputString = "";         // string buat tampung data yg dateng
boolean stringComplete = false;  // inisialisasi kalo kumpulan karakter dari komunikasi serial dah jadi satu kesatuan string


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,10,105);
char server[] = "myweb.net";

EthernetClient client;

void setup() {
 
 // inisialisasi serial:
 Serial.begin(9600);
 // reserve/menyediakan 200 bytes untuk inputString:
 inputString.reserve(200);
 
 Ethernet.begin(mac, ip);
 delay(1000);

 Serial.println("connecting...");

 if (client.connect(server, 80)) {
   Serial.println("connected");
   client.println("GET /infonya.log HTTP/1.1");
   client.println("Host: myweb.net");
   client.println("Connection: close");
   client.println();
 } else {
   Serial.println("connection failed");
 }
}

void loop() {
 // print string saat data ada:
 while (client.available()) {
   // tangkap byte baru:
   char inChar = client.read();
   // inChar yg berbentuk char di rubah ke bentuk string
   inputString += inChar;
   // if the incoming character is a newline, set a flag
   // so the main loop can do something about it:
   if (inChar == '\n') {
     stringComplete = true;
   }
 }
   
 if (stringComplete) {
   Serial.println(inputString);
     }
       

  delay(5000);


  if(client.available()){
    inputString = "";
     char inChar = client.read();
   inputString += inChar;
   if (inChar == '\n') {
     stringComplete = true;
   }
  }

 }




Now after you give me solution about show content from my website without http header, this is my picture




and this is my new code

Code: [Select]
// (c) Copyright 2010-2012 MCQN Ltd.
// Released under Apache License, version 2.0
//
// Simple example to show how to use the HttpClient library
// Get's the web page given at http://<kHostname><kPath> and
// outputs the content to the serial port

#include <SPI.h>
#include <HttpClient.h>
#include <Ethernet.h>
#include <EthernetClient.h>

// This example downloads the URL "http://arduino.cc/"

// Name of the server we want to connect to
const char kHostname[] = "myweb.net";
// Path to download (this is the bit after the hostname in the URL
// that you want to download
const char kPath[] = "/infonya.log";

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Number of milliseconds to wait without receiving any data before we give up
const int kNetworkTimeout = 30*1000;
// Number of milliseconds to wait if no data is available before trying again
const int kNetworkDelay = 1000;

void setup()
{
 // initialize serial communications at 9600 bps:
 Serial.begin(9600);

 while (Ethernet.begin(mac) != 1)
 {
   Serial.println("Error getting IP address via DHCP, trying again...");
   delay(15000);
 }  
}

void loop()
{
 int err =0;
 
 EthernetClient c;
 HttpClient http(c);
 
 err = http.get(kHostname, kPath);
 if (err == 0)
 {
   Serial.println("startedRequest ok");

   err = http.responseStatusCode();
   if (err >= 0)
   {
     Serial.print("Got status code: ");
     Serial.println(err);

     // Usually you'd check that the response code is 200 or a
     // similar "success" code (200-299) before carrying on,
     // but we'll print out whatever response we get

     err = http.skipResponseHeaders();
     if (err >= 0)
     {
       int bodyLen = http.contentLength();
       Serial.print("Content length is: ");
       Serial.println(bodyLen);
       Serial.println();
       Serial.println("Body returned follows:");
     
       // Now we've got to the body, so we can print it out
       unsigned long timeoutStart = millis();
       char c;
       // Whilst we haven't timed out & haven't reached the end of the body
       while ( (http.connected() || http.available()) &&
              ((millis() - timeoutStart) < kNetworkTimeout) )
       {
           if (http.available())
           {
               c = http.read();
               // Print out this character
               Serial.print(c);
             
               bodyLen--;
               // We read something, reset the timeout counter
               timeoutStart = millis();
           }
           else
           {
               // We haven't got any data, so let's pause to allow some to
               // arrive
               delay(kNetworkDelay);
           }
       }
     }
     else
     {
       Serial.print("Failed to skip response headers: ");
       Serial.println(err);
     }
   }
   else
   {    
     Serial.print("Getting response failed: ");
     Serial.println(err);
   }
 }
 else
 {
   Serial.print("Connect failed: ");
   Serial.println(err);
 }
 http.stop();

 // And just stop, now that we've tried a download
 while(1);
}




My Question is, how to make my second code looping forever like my first code???

sonnyyu

I will try first comment out "while(1);" at end.

edan_gelo

many thanks with your help, now i can loop data until i get a new data in my serial. I want ask a question again but it's not relation with ethernet shield. It's about save a serial data. i have data from serial with arduino uno usb cable pluged in my pc, but when i give arduino external power and there is not plugged in my pc, there is no data in my arduino. My question is how can i buffer data from my previous serial??

sonnyyu

SD cards, the SD library allows for reading from and writing to SD cards.

http://arduino.cc/en/Reference/SD/

In future if I were you,  will ask this type question in new thread, It will get answer from every one of forum. You will get better and quicker answers. To bet on multiple horses is far better than single one.

edan_gelo

thank you bro,, i will follow your suggest, i will post in new thread... once again, thank you for your help :)

Go Up