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 :
#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(;;)
;
}
}
//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
}
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.
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...
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.
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.