Retreive External IP address and send it in a Tweet

Hi everyone,

i wanted to implent a new function in my sketch and get some sensor (temp) readings Tweeted to me. I found some Tweeting example on the web and managed to use it. Then i decided it would be great if i get a Tweet at every External IP address change and so looked for a function to retreive my IP. I also found that function which is the following, but i'm not able to store the IP ADDRESS in a string so that my Tweet function con send it to me. The tweet function only send strings.

What shall i do to be able to store the IP Address in a string?

Thanks in advance.

void sendGET() //client function to send/receive GET request data.
{
  EthernetClient client;
/*
 if (millis() - lastIPAttemptTime > requestIPInterval)  // se sono passati almeno x minuti dall'ultimo accesso lettura IP
        {
          
          lastIPAttemptTime = millis(); // note the time of this connect attempt:*/
          
                    if (client.connect(serverIPIFY, 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
                      //gets byte from ethernet buffer
                      //ip_ext = ip_ext + String(client.read());
                      char ip_pubblico = client.read();
                      Serial.print(ip_pubblico); //prints byte to serial monitor 
                      //ip_total.concat(String(client.read()));
                    }
                    Serial.println();
                    Serial.println("disconnecting.");
                    Serial.println("==================");
                    Serial.println();
                    client.stop();
/*}*/
}

You can see some comments in the sketch of what i tried to use attempting the conversion in a string.

char ip[20];
int count = 0;

while (client.connected() || client.available()) 
    ip[count++] = client.read();

ip[count] = '\0';
Serial.print(ip);

OR, using StaticString library

etk::StaticString<20> ip;
while (client.connected() || client.available()) 
    ip += client.read();
Serial.print(ip.c_str());

char ip[12];How many characters are there in an IP address held as a zero terminated array of chars ?

Ah, yes quite right. Can't believe I didn't think that through properly. The 12's should be more like 20.

Wow, thanks!

So the correct version is ? :

char ip[12];
int count = 0;

while (client.connected() || client.available()) 
    ip[count++] = client.read();

ip[count] = '\0';
Serial.print(ip);

No, I've edited my previous post. The first line is now char ip[20];

12 characters isn't enough to hold an IP address with decimals and a null terminator, that was my mistake.

Thanks for replying.

I'm very sorry but i still get get this thing working :-\

I need the IP Address in a String so that i can write a message and say for example :

message = ("IP Address is : ");
ip_address = ip;
Serial.print(message + ip);

I need that also to be able to compare last ip address reading string to new ip address reading and compare them and send a new message with the new IP if the IP has changed.

I just can't figure how to do that.

Sorry again for being so noob!

I need the IP Address in a String so that i can write a message and say for example :

The code puts the IP address in a string rather than a String. strings (lowercase s) are generally regarded as preferable on a processor with limited memory.

If you absolutely insist on using Strings then there are functions available to convert a string into a String but I am not familiar with them. What I am familiar with are the functions to manipulate strings such as strcat() to concatenate them and strcmp() to compare them. A little research on your part will reap benefits and show you how to use them (and other) functions effectively.

Here is the library documentation, featured but not very prominently on the Arduino reference page.

marcomaroso: I need the IP Address in a String so that i can write a message and say for example :

message = ("IP Address is : ");
ip_address = ip;
Serial.print(message + ip);

did you try:

Serial.print("IP Address is : ");
Serial.println(ip);

Hi, thanks to everyone trying to help me. Maybe it's because i've been awake till 6 am trying to figure this out, and now i'm just tired!

Anyway, i should post some more code to make understand my needs.

I have a big sketch doing alot of things, besides all this i would like to implent a feature that checks my External IP address and sends a tweet to me.

I therefore have a void for tweeting messages and a void for checking the IP address.

Here is the Tweet Void :

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);
    if (status == 200)
    {
      Serial.println("OK.");
    } 
    else
    {
      Serial.print("failed : code ");
      Serial.println(status);
    }
  } 
  else
  {
    Serial.println("connection failed.");
  }
}

And here the one for reading IP (a bit messed up in the various trying i did!)

void sendGET() //client function to send/receive GET request data.
{
  EthernetClient client;

 if (millis() - lastIPAttemptTime > requestIPInterval)  // se sono passati almeno x minuti dall'ultimo accesso lettura IP
        {
            lastIPAttemptTime = millis(); // note the time of this connect attempt:
          
                    if (client.connect(serverIPIFY, 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();
                    }
                      
                      int count = 0;

                  
                    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 */
                       
                       ip_address[count++] = client.read();
                       ip_address[count] = '\0';
                      
                      Serial.print(ip_address);

                    }
                    Serial.println();
                    Serial.println("disconnecting.");
                    Serial.println("==================");
                    Serial.println();
                    client.stop();

            
}
}

In my Loop Void i would just like to call a Void that checks my ip, but right now i have this non sensing code beside other code that works fine for other things

  // 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
    }
  }    
    
  
  check_ip();   // this should be the void that calls the IP checking function (now it's just empty)

Here is the Tweet Void

It isn't a "void". It's called a "function".

Yes, you're right ... i'm just new to C programming, i can program in other languages and would have already done, but here i have to use C so that's why i'm asking help.

I usually used to create prodcedures or functions and recall them when needed, i'm not so good here in this, still have to learn everything here!

i would just like to call a Void that checks my ip

The "my IP address" is somewhat confusing. If your arduino is acting as a server, you probably would have this IP address in your server code. Seems like you may be wanting the "remote IP address" of some client tweet. If so, search those words in the search box in the upper right of this page.

Thanks for your reply,

No, my sketch acts as a server and client. It has some html buttons (made by code in Arduino) that can give commands to arduino and it also has an IFRAME which updates nearly in realtime the readings of some sensors.

It's obvious i know my local ip address, but what i want is the internet connection IP so that if arduino is able to retreive it , he can then send it to me via tweet if IP changes (dynamic).

I don't want to keep a PC acting as DynDns updater , my router has OLD and not free DYNDNS hosts i can use but i don't want to rely on him and just want to do it from the arduino.

I thought it could be a very easy thing but i notice that no one has a real solution to this, everyone just saying i print it to the serial monitor ...yes but i need to read it in a tweet and not in the serial monitor because i need to access it remotely.

I'm stuck on this since 2 days barely sleeping and it's incredible i haven't found a solution yet.

Having trouble with char and chars* ??? and strings and conversions....what a mess!

Well, maybe i'm facing the problem in the wrong manner, so please, can someone suggest me a short and easy to understand solution.

Thanks.

Well, maybe i'm facing the problem in the wrong manner, so please, can someone suggest me a short and easy to understand solution.

So what did the forum google search produce for you?