Global variable String isn't working [solved]

Hello,

I have a program with a void function that gets 3 strings from “http request”. I want to send them to the main function, void loop. I tried to make global strings by declaring them in the beginning, but the compiler doesn’t recognize them in the void function. What can I do, how can I send them to the loop function?

Thanks,
Itay

@itayd100, can you post your code please.

Hey @peekay123,

This is the code:

// This #include statement was automatically added by the Spark IDE.
#include "HttpClient/HttpClient.h"


//button
int button = D6;
int i=0;

//LEDs
int RedLED = A4;
int GreenLED = A5;
int BlueLED = A6;

//parameters
String num1;
String lat;
String lon;
String city;


HttpClient http;
http_header_t headers[] = {
    //  { "Content-Type", "application/json" },
    //  { "Accept" , "application/json" },
    { "Accept" , "*/*"},
    { NULL, NULL }
};

 http_request_t request;
 http_response_t response;
 

void setup() {
  Serial.begin(9600);
  //while(!Serial.available()) SPARK_WLAN_Loop();
  delay(5*1000);
  Serial.println("Hello!");
  
  
  
  //PINs SET
  pinMode(RedLED, OUTPUT);
  pinMode(GreenLED, OUTPUT);
  pinMode(BlueLED, OUTPUT);
  pinMode(button, INPUT_PULLDOWN);
  

  

}



void loop() {

    if (!digitalRead(button))
    { 
    Serial.println(i);
    IP_LOC();
    i++;
    Serial.print(lat);
    Serial.print(lon);
    Serial.print(city);
      }

 
}

void IP_LOC(){
    
    //reset the LEDS
    digitalWrite(RedLED, LOW);
    digitalWrite(GreenLED, LOW);
    digitalWrite(BlueLED, LOW);
    Serial.println("Asking IP LOC ");
    
    
    
    delay(500);
    
    
    request.hostname = "ip-api.com";
    request.port = 80;
    
    request.path = "/json/?callback=yourfunction"  ;
    
    // Get request
    http.get(request, response, headers);
    String raw_response(response.body);
    
    String str=raw_response;
   
    int temp = str.indexOf("lat");
     lat = raw_response.substring(temp+5,temp+5+6); //the place of the number.
    Serial.println(lat);
    temp = str.indexOf("lon");
     lon = raw_response.substring(temp+5,temp+5+6); //the place of the number.
    Serial.println(lon);
    
    temp = str.indexOf("\"city\"");
    int temp2 = str.indexOf(",\"country\":");
    //Serial.println(temp);
     city = raw_response.substring(temp+8,temp2-1); //the place of the number.
    Serial.println(city);
    
   
}

Can you also post the error message the compiler throws.
I guess it’s not that the compiler doesn’t know your variables, but it doesn’t like how you use them.
But we can only be certain when seeing the compiler messages :wink:

Hey @ScruffR,

Yes, I know it’s me (I hear it from my girlfriend too ;-))
This is the error:

  undefined reference to `String::operator=(String&&)'

As I guessed :wink:

Try this

  lat = "" + raw_response.substring(temp+5,temp+5+6);
  // or this
  lat = raw_response.substring(temp+5,temp+5+6).c_str();

It’s not nice, but it should work. It’s a known bug of the String class :stuck_out_tongue_closed_eyes:

The message tells you that the var was recognized as String, but there is no assignment operator that can take a String as r-value (source).


So it’s not you - we are just never understood right :wink:

2 Likes

Working!

Thanks @ScruffR