getting public ip- It works "steadily"!!

hi guys,

I had this challenge to get my own IP address so that I can connect to my arduino over public-internet.

As I had a dynamic IP and also live in a place of frequent power cut, I needed to monitor the IP often.
I tried the dyndns services, but frequent pings used to timeout, even if it did I d hate the adds on the free service. After may be couple of years, had seen another member trying to do the same, and on mad searching, came across ipify.org, which is simple and awesome!

Hope the below code helps someone. Its a modified version of zoomkat’s code I found over the forum.

//zoomkat 9-22-12
//saiko 26-2-15 using ipify.com
//simple client test
//for use with IDE 1.0.1
//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[] = "api.ipify.org"; // zoomkat's 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("Get ip from ipify"); // 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: api.ipify.org");
    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

}

.

serial port result as expected

Send an e in serial monitor to test
connected
HTTP/1.1 200 OK
Server: CCCCC
Connection: close
Access-Control-Allow-Origin: *
Content-Type: text/plain
Date: Thu, 26 Feb 2015 16:58:50 GMT
Content-Length: 15
Via: 1.1 yyyy

xxx.xxx.ccc.xxx
disconnecting.
==================

The best thing about www.ipify.org I liked

  • You can use it without limit (even if you’re doing millions of requests per minute).
  • No visitor information is ever logged. Period.

It is very nice!

Thank you!

And how did you get only the ip from this?