Using a char from another scope

Hello, I’m having troubles with my code;
I’m trying to get my public ip via ipify, and upload it to my mysql server.

I found the ipify arduino code here so I changed the code an added a function to send the data to my server:

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "api.ipify.org"; // zoomkat's test web page server
char sqlServer[] = "MY REAL 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("Get ip from ipify"); // so I can keep track of what is loaded
}

void loop(){
  // check for serial input


      sendGET(); // call sendGET function below when byte is an e
      delay(1000);
      sendSQL();
      
  delay(9000);
}

//////////////////////////

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();
  }

  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

}

void sendSQL() {
   if (client.connect(sqlServer, 80)>0) {  // Conexion con el servidor
    client.print("GET /ardget.php?ip="); // Enviamos los datos por GET
    client.print(c);
    client.println(" HTTP/1.0");
    client.println("User-Agent: Arduino 1.0");
    client.println();
    Serial.println("Conectado");
  } else {
    Serial.println("Fallo en la conexion");
  }
  if (!client.connected()) {
    Serial.println("Disconnected!");
  }
  client.stop();
}

The problem is that the char c (the ip from ipify), is in another scope and I can’t use it, I tried to make another string like this:

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "api.ipify.org"; // zoomkat's test web page server
char sqlServer[] = "MY REAL SERVER";
String extip = "";
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("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


      sendGET(); // call sendGET function below when byte is an e
      delay(1000);
      Serial.println(extip);
      sendSQL();
      
  delay(9000);
}

//////////////////////////

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();
  }

  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
    extip = c;
  }

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

}

void sendSQL() {
   if (client.connect(sqlServer, 80)>0) {  // Conexion con el servidor
    client.print("GET /ardget.php?ip="); // Enviamos los datos por GET
    client.print(extip);
    client.println(" HTTP/1.0");
    client.println("User-Agent: Arduino 1.0");
    client.println();
    Serial.println("Conectado");
  } else {
    Serial.println("Fallo en la conexion");
  }
  if (!client.connected()) {
    Serial.println("Disconnected!");
  }
  client.stop();
}

But ‘extip’(my string) only shows me the last number of the ip. Can anyone tell me what am I doing wrong? Thank you in advance

(I hope you understand what I refered, if not, ask me)

extip = c;

Your String only has the last character read because you keep overwriting only the last character to it.

Maybe you meant:

extip += c;

It didn't work, it prints me the same info repeteadly without any sense:

Before:

Now:

Your code is in loop() which is called repeatedly. If you want it to only happen once, put it in setup().

Can you copy and paste the text instead of making screen grabs.

johnwasser: Your code is in loop() which is called repeatedly. If you want it to only happen once, put it in setup().

No, i want it to refresh the data, so that's why i put delays in the loop.

Delta_G: Can you copy and paste the text instead of making screen grabs.

This is the serial with 'extip = c' (every loop)

connected
HTTP/1.1 200 OK
Server: Cowboy
Connection: close
Content-Type: text/plain
Date: Wed, 03 Aug 2016 19:38:10 GMT
Content-Length: 13
Via: 1.1 vegur

2.138. (...) .207
disconnecting.
==================

7
Conectado

And that's the serial with 'extip += c'

connected
HTTP/1.1 200 OK
Server: Cowboy
Connection: close
Content-Type: text/plain
Date: Wed, 03 Aug 2016 19:42:44 GMT
Content-Length: 13
Via: 1.1 vegur

2.138. ... .207
disconnecting.
==================

HTTP/1.1 200 OK
Server: Cowboy
Connection: close
Content-Type: text/plain
Date: Wed, 03 Aug 2016 19:41:46 GMT
Content-Length: 13
Via: 1.1 vegur

2.138.169.207HTTP/1.1 200 OK
Server: Cowboy
Connection: close
Content-Type: text/plain
Date: Wed, 03 Aug 2016 19:41:57 GMT
Content-Length: 13
Via: 1.1 vegur

2.138.169.207HTTP/1.1 200 OK
Server: Cowboy
Connection: close
Content-Type: text/plain
Date: Wed, 03 Aug 2016 19:42:09 GMT
Content-Length: 13
Via: 1.1 vegur

2.138.169.207HTTP/1.1 200 OK
Server: Cowboy
Connection: close
Content-Type: text/plain
Date: Wed, 03 Aug 2016 19:42:20 GMT
Content-Length: 13
Via: 1.1 vegur

2.138.169.207HTTP/1.1 200 OK
Server: Cowboy
Connection: close
Content-Type: text/plain
Date: Wed, 03 Aug 2016 19:42:32 GMT
Content-Length: 13
Via: 1.1 vegur

2.138.169.207HTTP/1.1 200 OK
Server: Cowboy
Connection: close
Content-Type: text/plain
Date: Wed, 03 Aug 2016 19:42:44 GMT
Content-Length: 13
Via: 1.1 vegur

2.138. ... .207
Conectado