Hello,
I was trying Saiko's sketch in hope to finally have a mean to be continuesely informed of my Arduino's public IP. Unfortunately, after having returned the (correct) IP few times, now it returns a long preamble before the IP (see below) or it closes the connection write away. I there a sure mean to get the IP and only the IP? The code is almost identical to Saiko's
HTTP/1.1 200 OK
Server: Cowboy
Connection: close
Content-Type: text/plain
Vary: Origin
Date: Thu, 24 May 2018 15:37:37 GMT
Content-Length: 12
Via: 1.1 vegur
//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
IPAddress myIP (10, 0, 0, 4);
IPAddress netDNS (10, 0, 0, 138);
IPAddress gateway(10, 0, 0, 138);
IPAddress subnet (255, 255, 255, 000);
char serverName[] = "api.ipify.org"; // zoomkat's test web page server
EthernetClient client;
//////////////////////
void setup(){
Serial.begin(38400);
Ethernet.begin(mac, myIP, netDNS, gateway, subnet);
Serial.println(F("My ip is: ")); Serial.println(Ethernet.localIP());
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
{
Serial.println("calling sendGET");
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();
}
bool clientConnected,clientAvailable;
while((clientConnected=client.connected()) && !(clientAvailable=client.available())) delay(1); //waits for data
Serial.print("client connected ");Serial.print(clientConnected); Serial.print(" clientAvailable "); Serial.println(clientAvailable);
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
}