Pages: [1]   Go Down
Author Topic: How do I read the data from a simple single page website (ethernet) (SOLVED)  (Read 1293 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I can't figure out how to  read data from a very simple website. I connect to    http://myip.dnsdynamic.org/   all this page does is  show your current IP address....  xxx.xxx.xxx.xxx   i've successfully connecting to the page, but I can't figure out what my get statement would be ?

client.connect(server, 80)
client.println("GET /mylip.dnsdynamic.org HTTP/1.0");

this code almost works for me unfortunately it gets me the data on their main page /www.dnsdynamic.org/
does anyone have an idea, thanks
« Last Edit: July 06, 2012, 09:24:27 pm by ej012345 » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 633
Posts: 50210
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

client.println("GET /mylip.dnsdynamic.org HTTP/1.0");

Posting your real code is a good idea.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

typo !
client.println("GET /mylip.dnsdynamic.org HTTP/1.0");

should have been
client.println("GET /myip.dnsdynamic.org HTTP/1.0");

Unfortunately the "real code" is at home and I'm at work.
I believe it was basically....
Code:
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char serverName[] = "/myip.dnsdynamic.org" ;

// Initialize the Ethernet client library
EthernetClient client;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(serverName, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    // client.println("GET / HTTP/1.0"); // didnt work
    client.println("GET /myip.dnsdynamic.org HTTP/1.0");
    client.println();
  }
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    for(;;)
      ;
  }
}


Thanks, Eric

« Last Edit: July 05, 2012, 11:16:20 am by ej012345 » Logged

Finland
Offline Offline
Sr. Member
****
Karma: 1
Posts: 270
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Add client.println("Host: myip.dnsdynamic.org"); after the line that prints the GET header.

Edit: before -> after
« Last Edit: July 05, 2012, 12:02:34 pm by pekkaa » Logged

Finland
Offline Offline
Sr. Member
****
Karma: 1
Posts: 270
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And this looks a bit funny:
Code:
client.println("GET /myip.dnsdynamic.org HTTP/1.0");
It  means that you want to request the resource "myip.dnsdynamic.org" on the server.

If you meant to access the root resource of your server, it should be:
Code:
client.println("GET / HTTP/1.0");

Edit: typos
« Last Edit: July 05, 2012, 12:27:01 pm by pekkaa » Logged

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

Some simple client test code you can try. Sometimes the server adds a "ETag:" line to the returned header which throws the cr counting off by 1.

Code:
//zoomkat 7-05-12
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan assigned to arduino
//byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
//byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
//byte myserver[] = { 208, 104, 2, 86 }; // zoomkat web page server IP address
//Client client(myserver, 80);  // connect to web server using port 80
char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;

String readString, readString1;
int x=0;
char lf=10;
//////////////////////

void setup(){

  Ethernet.begin(mac, ip);
  //Ethernet.begin(mac,ip,gateway,gateway,subnet);
  Serial.begin(9600);
  Serial.println("Better client test 12/16/11"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  } 
}

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    client.println(); //end of get request
  }
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor
    if (c==lf) x=(x+1);
    if (x==9) readString += c;
  }

    Serial.println(); 
    Serial.println();
    Serial.print("Current data row:" );
    Serial.print(readString);
    Serial.println();
    readString1 = (readString.substring(0,8));
    Serial.println();
    Serial.print("How we feeling?: ");
    Serial.println(readString1);
    Serial.println();     
    Serial.println("done");
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Pekkaa,
Thanks so much.
Adding your line of code...
Code:
client.println("Host: myip.dnsdynamic.org");
after the line client.println("GET / HTTP/1.0");
worked perfectly...  I don't understand it yet, but it worked.
Logged

Finland
Offline Offline
Sr. Member
****
Karma: 1
Posts: 270
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The server uses name based virtual hosts, i.e., several host names share the same IP address. You need to provide the  Host-header so that the server can decide which virtual host it needs to connect you.

You can easily test the request from your PC before implementing it on the Arduino. For example on Windows start the command prompt and type "telnet myip.dnsdynamic.org 80". When the telnet session is established type the same lines as you would print in the Arduino, e.g:

Code:
GET / HTTP/1.0
Host: myip.dnsdynamic.org

Logged

Pages: [1]   Go Up
Jump to: