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
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.
Well thats really the part i dont get, i can not get the external ip (thanks) so so far ive got:
//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 )
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?
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.
//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
}
Why would you need the External IP to access the internet?
I think you may need to read up on how TCP/IP works.
Your arduino will broadcast that it is on the network. It will look for a DHCP server. It will then be given all the details it needs to connect to the internet. (Internal IP address, DNS servers, Router Address) You NEVER need to know your external IP to access the internet. That will be handled by the router. You cannot even think of getting the external IP before you get all the details from your router. As soon as you have these if it supports UPNP you can just ask it for the external IP.
Your Router is given an IP address by your ISP. Then your router will give all the devices it is connected to an IP address. Your internal devices will be given the following over DHCP;
Internal IP address
Router IP address
DNS Server IP address
Subnet Mask
Your device is never given an external IP address. In order to get that you must either request it from your router OR connect to an external server which will then tell you the IP address that the request came from. Your entire network at home will have ONE external IP address.