Go Down

Topic: How do I read the data from a simple single page website (ethernet) (SOLVED) (Read 1 time) previous topic - next topic

ej012345

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

PaulS

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

Posting your real code is a good idea.

ej012345

#2
Jul 05, 2012, 05:50 pm Last Edit: Jul 05, 2012, 06:16 pm by ej012345 Reason: 1
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: [Select]
#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


pekkaa

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

Edit: before -> after

pekkaa

And this looks a bit funny:
Code: [Select]
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: [Select]
client.println("GET / HTTP/1.0");

Edit: typos

zoomkat

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: [Select]

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

}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

ej012345

Pekkaa,
Thanks so much.
Adding your line of code...
Code: [Select]
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.

pekkaa

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: [Select]

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



Go Up