Hi everyone, as usual when i'm able to solve a problem I also like to share the solution and hope that maybe someone can improve it and shorten the code making it use less memory.
Here below a perfectly working code which retreives you pubblic ip and sends it by tweeter every X milliseconds specified in the sketch.
I have added to the tweet also the milliseconds that have passed since Arduino has powered on, this is needed to avoid the 403 error that Twitter shoots when you're trying to post a duplicate data already posted. Since time passes and milliseconds will always increase, adding them to the post will result in different posts always avoiding the Error 403 of Twitter.
Below is the code, i would like someone to help me make it better and especially make it send the IP only when it has changed since the first reading.
// reads IP address output from the www.realip.info website
// Thanks to ZoomKat for his precious help
#include <SPI.h>
#include <Ethernet.h>
String readString, tempstring;
#include <Twitter.h>
Twitter twitter("your token"); //token di tweeter per inviare messaggi
char tweetText[100];
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
const unsigned long requestInterval = 120000; //delay between requests 300.000 = 5 min / 60.000 = 1 minuto / 120.000 = 2 minuti
unsigned long lastAttemptTime = 0 ; //var for lastattemptTime for requesting new ip reading
int lastStringLength = readString.length(); //var for reading length of ipaddress string output from website
int octet1,octet2,octet3,octet4 ; //integers for ip address
char serverName[] = "www.realip.info"; //myIP server 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);
sendGET(); //asks for the ip address
}
void loop()
{
if (millis() - lastAttemptTime > requestInterval) // se sono passati almeno x minuti dall'ultimo tweet d'allarme allora invia di nuovo se necessario
{
sendGET();
lastAttemptTime = millis(); // note the time of this connect attempt:
}
//occorre attendere prima di poter eseguire un nuovo controllo dell'ip
}
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 /api/p/realip.php HTTP/1.1"); //download text
client.println("Host: www.realip.info");
client.println("Connection: close"); //close 1.1 persistent connection
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
readString += c; //places captured byte in readString
}
client.stop(); //stop client
Serial.print("IP = ");
int primaricerca = readString.indexOf('{'); //inserisce in variabile la posizione della prima parentesi graffa all'interno della quale c'è l'IP
lastStringLength = readString.length(); //assegna alla variabile lastringlength la lunghezza della stringa letta contenente l'IP
readString.remove(0, primaricerca+7); //rimuove tutto quello che c'è dall'inizio della stringa fino alla prima parentesi graffa + 7 caratteri per arrivare all'ip
lastStringLength = readString.length(); //rilegge la lunghezza della stringa ora che abbiamo rimosso parte delle cose inutili
readString.remove(lastStringLength-2, lastStringLength); //rimuove tutto il resto partendo dalla fine -2 verso la fine in modo da ottenere solo l'IP
Serial.println(readString); //stampa sul monitor seriale l'IP
//trasforma l'ip letto in numeri interi
int punto1 = readString.indexOf('.'); //legge la posizione del primo punto dell'IP
tempstring = readString.substring(0,punto1); //inserisce in variabile tempstring il primo otteto
octet1 = tempstring.toInt (); //assegna a variabile octet1 il valore numerico della stringa tempstring
readString.remove(0, punto1+1); //rimuove dalla stringa readString tutto quello che c'è dall'inizio fino al punto
int punto2 = readString.indexOf('.'); //legge la posizione del primo punto dell'IP
tempstring = readString.substring(0,punto2); //inserisce in variabile tempstring il secondo otteto
octet2 = tempstring.toInt (); //assegna a variabile octet2 il valore numerico della stringa tempstring
readString.remove(0, punto2+1); //rimuove dalla stringa readString tutto quello che c'è dall'inizio fino al punto
int punto3 = readString.indexOf('.'); //legge la posizione del primo punto dell'IP
tempstring = readString.substring(0,punto3); //inserisce in variabile tempstring il terzo otteto
octet3 = tempstring.toInt (); //assegna a variabile octet3 il valore numerico della stringa tempstring
readString.remove(0, punto3+1); //rimuove dalla stringa readString tutto quello che c'è dall'inizio fino al punto
lastStringLength = readString.length(); //legge la lunghezza stringa dell'IP
tempstring = readString.substring(0,lastStringLength+1); //inserisce in variabile tempstring il quarto otteto
octet4 = tempstring.toInt (); //assegna a variabile octet4 il valore numerico della stringa tempstring
readString.remove(0, lastStringLength+1); //rimuove dalla stringa readString tutto quello che c'è dall'inizio fino alla fine
Serial.print("otteto 1 = ");
Serial.println(octet1);
Serial.print("otteto 2 = ");
Serial.println(octet2);
Serial.print("otteto 3 = ");
Serial.println(octet3);
Serial.print("otteto 4 = ");
Serial.println(octet4);
sprintf(tweetText, "http://%d.%d.%d.%d/\nTempo trascorso ms : %d", octet1,octet2,octet3,octet4, millis()); //prepara la stringa da inviare con tweeter
tweet(tweetText); //invia il tweet
readString=""; //svuota la variabile readString
tempstring=""; //svuota la variabile tempstring
}
void tweet(char msg[])
{
Serial.println("connecting ...");
if (twitter.post(msg))
{
// Specify &Serial to output received response to Serial.
// If no output is required, you can just omit the argument, e.g.
// int status = twitter.wait();
// int status = twitter.wait(&Serial);
int status = twitter.wait();
if (status == 200)
{
Serial.println("OK.");
}
else
{
Serial.print("failed : code ");
Serial.println(status);
}
}
else
{
Serial.println("connection failed.");
}
}