Pages: [1] 2   Go Down
Author Topic: Changes to external ip  (Read 2172 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all
As you probably here all the time im quite new to all this and could really use some help in my project
I need my programing to:
1) get external IP
2) Wait for a while
3) Get new external IP
4) Compare the old and new to see if their are any changes

im really going nowhere any input would be appriciated
Thanks
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
1) get external IP
Of what?

Quote
2) Wait for a while
That's easy.

Quote
3) Get new external IP
Of?

Quote
4) Compare the old and new to see if their are any changes
And if they are?
Logged

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

Sorry, the external ip of the Ethernet board or rather the router/ the inetrnet connection it is using
Logged

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

Oh, if the external ip is different then it will hopefully send a get request to another server, i have actualy managed this bit and in my backwards thinking to start at the end and work my way back.
Thanks
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't know if it's even possible to get the address of the outward facing IP of router that the Arduino is connected to. I asked that this topic be moved here to Networking, because it is more appropriate here. Perhaps someone else will be able to answer your question.
Logged

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

Put this php script to a server on Internet with know ip:

Code:
<?php
print ('<'.$_SERVER['REMOTE_ADDR'].'>');
?>


And use something like this script to read the ip from there: http://bildr.org/2011/06/arduino-ethernet-client/

Now you just need to compare the new and old ip values.

Is this what you are looking for?
« Last Edit: January 10, 2013, 07:17:27 pm by llukkari » Logged

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

Is this what you are looking for?

Yeah Thank you that helps a lot, I dont suppoze you would know how i could then compare the old from the new?
Thanks again
Logged

Smithfield, Rhode Island
Offline Offline
God Member
*****
Karma: 3
Posts: 843
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The easiest way, although probably not the most efficient, is to use the String class. What version of the Arduino environment are you using?
 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I dont suppoze you would know how i could then compare the old from the new?
How have you written the code to get the two values?
Logged

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

Well thats really the part i dont get, i can not get the external ip (thanks) so so far ive got:
Code:

//ARDUINO 1.0+ ONLY
//ARDUINO 1.0+ ONLY
#include <Ethernet.h>
#include <SPI.h>

////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
byte server[] = { 192,168,0,31 }; //ip Address of the server you will connect to

//The location to go to on the server
//make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
String location = "/externalip.php/ HTTP/1.0";

// if need to change the MAC address (Very Rare)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
////////////////////////////////////////////////////////////////////////

EthernetClient client;

char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?

void setup(){
  Ethernet.begin(mac);
  Serial.begin(9600);
}

void loop(){
  String pageValue = connectAndRead(); //connect to the server and read the output

  Serial.println(pageValue); //print out the findings.

  delay(5000); //wait 5 seconds before connecting again
}

String connectAndRead(){
  //connect to the server

  Serial.println("connecting...");

  //port 80 is typical of a www page
  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.print("GET ");
    client.println(location);
    client.println();

    //Connected - Read the page
    return readPage(); //go and read the output

  }else{
    return "connection failed";
  }

}

String readPage(){
  //read the page, and capture & return everything between '<' and '>'

  stringPos = 0;
  memset( &inString, 0, 32 ); //clear inString memory

  while(true){

    if (client.available()) {
      char c = client.read();

      if (c == '<' ) { //'<' is our begining character
        startRead = true; //Ready to start reading the part
      }else if(startRead){

        if(c != '>'){ //'>' is our ending character
          inString[stringPos] = c;
          stringPos ++;
        }else{
          //got what we need here! We can disconnect now
          startRead = false;
          client.stop();
          client.flush();
          Serial.println("disconnecting.");
          return inString;

        }

      }
    }

  }

}


I setup a webserver localy on that ip for now which is printing the internal ip but shows alls working, so as of now i get a simple ip address.

So i think what i need to do is then save that numer as a string?
Then wait (thats the easy bit  smiley )
Then change the name of the now old ip sting to "old"
Check again and call it "new"
Check old against new
If old is the same as new then...
If not start again
But i have no idea how to get the read data and save it as a string?

Thanks again for your time
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Where did you get that crappy code?

There is no reason to be using the String class. There is, in particular, almost a crime in wrapping a global char array as a String just because you can't figure out what the correct return type for readPage() should be (hint: it's void).

Anyway, that code does something, and generates some serial output. The psychic we hired starts the 12th of never. If you need help before then, you'll tell us what that code actually does, and show the serial output.
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You might start with some code like below.

Code:
//zoomkat 9-22-12
//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[] = "checkip.dyndns.com"; // 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("Better client test 9/22/12"); // 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: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Excellent code Changes to external ip
How do I send the ip found for a PHP page or email?
Or how do I access a page as http://username:password@members.dyndns.org/nic/update?hostname=yourhostname&myip=ipaddress
Logged

London, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 189
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can use UPNP to get your external IP from your router.
This library can do it pretty easily.

https://github.com/deverick/Arduino-Upnp-PortMapping
Logged

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

Ok But I like to get my external ip (wan) to be able to access the Internet via arduino. In that case then I can not know the external IP.
Logged

Pages: [1] 2   Go Up
Jump to: