Pages: [1]   Go Down
Author Topic: Getting simple data from a website  (Read 601 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to get some data from a website : myip.dnsdynamic.org
just want the IP address.

i have found a thread but it doesn't work

the only thing i'm getting in my serial monitor is :

connecting...
connected

disconnecting.


so there is a connection, but it fails to give me  the IP-address.

DHCP worked, for testing ik tried it with a manual IP, gateway, subnet.... but still no result...

tnx in advance


the code i'm using is :

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("Host: myip.dnsdynamic.org");
   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(;;)
      ;
  }
}
« Last Edit: March 01, 2013, 05:28:19 am by tomvo » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 149
Posts: 6119
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This client code works for me.
http://playground.arduino.cc/Code/WebClient
Logged

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

Some modified client code using the below link.

http://checkip.dyndns.com/

Code:
//zoomkat 3-1-13
//simple client checkip test
//for use with IDE 1.0.1 or later
//with DNS, DHCP, and Host
//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

char serverName[] = "checkip.dyndns.com"; // test web page server
EthernetClient client;

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

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600);
  Serial.println("Better client ip test 3/1/13"); // 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 / HTTP/1.0"); //download text
    client.println("Host: checkip.dyndns.com");
    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
  }

  Serial.println();
  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: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

tnx zoomkat & SurferTim

this works, my serial output looks like this


connected
HTTP/1.1 200 OK
Content-Type: text/html
Server: DynDNS-CheckIP/1.0
Connection: close
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 105

<html><head><title>Current IP Check</title></head><body>Current IP Address: 209.121.24.86</body></html>

disconnecting.
==================


do you know if its possible ( i'm sure it is, but don't know how  ) to only get the Ip address out this output?

tnx Tom
Logged

N. Illinois
Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is a sample that I wrote to read the current temperature of my thermostat.  It has the same basic functionality you are looking for.
Code:
void loop()
{


  if (Get.check() ==1){

    if (!Client.connected())
    {
      Tstatconnect();
    }

    if (Client.connected())
    {
     
      gtemp();
      Get.interval(60000);
    }
  }



  if (Client.available() > 0)
  {
    ValidDataFound = true;
    ValidDataFound &= Client.find("{");
    ValidDataFound &= Client.find(":");
    DataValueFloat = Client.parseFloat();
    ValidDataFound &= Client.find("}");
    while (Client.available() > 0) Client.read();        //flush the input buffer manually, since the flush() function now only waits for the write buffer to clear.
    if (ValidDataFound)
    {
      Serial.print("Valid Data Found. Value = ");
      Serial.println(DataValueFloat);
            Tstatstop();
    }
    else
    {
      Serial.println("Invalid Data");
      Tstatstop();
    }
  }

} // END OF LOOP

The basic parsing is found in here...

Code:
if (Client.available() > 0)
  {
    ValidDataFound = true;
    ValidDataFound &= Client.find("{");
    ValidDataFound &= Client.find(":");
    DataValueFloat = Client.parseFloat();
    ValidDataFound &= Client.find("}");
    while (Client.available() > 0) Client.read();        //flush the input buffer manually, since the flush() function now only waits for the write buffer to clear.
Logged

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

Quote
do you know if its possible ( i'm sure it is, but don't know how  ) to only get the Ip address out this output?

For a small returned string you can capture the string and parse it. You could find the : location in the string and the following < location, and then capture the the IP address that is between these two characters. For large returns there is a library call ed textfinder.h that can be used.
Logged

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

Pages: [1]   Go Up
Jump to: