Use value from setup

So I'm writing a program which sets a value to a string variable in the setup function. Next I try to use the value in a other function which is executed in setup and loop. The function is in another ino file.
My problem is that the value can't be accessed.

post the code using </>

is the variable defined globally or in setup()? needs to be global is used outside of setup()

The variable is defined gloablly. Do I need to import the global variable? Because I use multiple ino files?

Is it define globally in the sketch .ino file or in a .ino file on a tab ?

It is defined in the main ino file.

What is a MAIN INO FILE?

The one whose name is used when you save the sketch in a folder with the same name. It is the .ino file on the leftmost tab in the IDE

I assume that it is declared outside of a function, thus making it a global variable. It may, however, have its value defined within a function

Please post an example sketch showing the problem. It should be easy to show the problem in a minimal but complete sketch

Are we talking about a String variable or a string variable ? An example will tell use which it is

#include <HTTPClient.h>
#include <Arduino_JSON.h>
#include <WiFiManager.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
int RXPin = 16; // Schaltplan schauen
int TXPin = 17;
int GPSBaud = 9600;

TinyGPSPlus gps;
SoftwareSerial gpsSerial(RXPin, TXPin);


// Zeitintervalle für jede Funktion aufstellen (Timer)

const unsigned long updateweather = 420000;     // alle 7min wird die Temperatur laut OWM auf TFT gebracht; 10s für Testzwecke
unsigned long lastTimeweather = 0;


String lg;
String bg;
String jsonBuffer;

void setup() {
  gpsSerial.begin(GPSBaud);
  Serial.begin(9600);
  WiFi.mode(WIFI_STA);                         // WLAN-Verbindung aufbauen
  WiFiManager wm;
  bool res;
  res = wm.autoConnect("Wetterstation", "WetterArmin"); // AP mit Passwort
  if (!res) {
    tft.setCursor(0, 50);
    tft.print("Es konnte keine Verbindung aufgbeaut werden!");
  }
  else {
    //if you get here you have connected to the WiFi
    tft.setCursor(30, 75);
    tft.print("Verbunden. Viel Spaß!");
    delay(5000);
  }
  Serial.begin(9600);
  delay(5000);

  while (gpsSerial.available() > 0)
    if (gps.encode(gpsSerial.read()))
      if (gps.location.isValid())
      {
        Serial.print("Breitengrad: ");
        Serial.print(gps.location.lat());
        Serial.println("Längengrad: ");
        Serial.print(gps.location.lng());
      }
      else
      {
        Serial.println("Location: Not Available");
      }

  if (millis() > 5000 && gps.charsProcessed() < 10)
  {
    Serial.println("No GPS detected");
    while (true);
  }

  String bg = String(gps.location.lat());
  String lg = String(gps.location.lng());

  delay(1000);

  weathersetup();

}

void loop() {

}


// für Datenempfang von OpenWeather API

String httpGETRequest(const char* serverName) {
  HTTPClient http;

  // Your IP address with path or Domain name with URL path
  http.begin(serverName);

  // Send HTTP POST request
  int httpResponseCode = http.GET();

  String payload = "{}";

  if (httpResponseCode > 0) {
    //Serial.print("HTTP Response code: ");
    //Serial.println(httpResponseCode);
    payload = http.getString();
  }
  else {
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }
  // Free resources
  http.end();

  return payload;
}

// tft_output

bool tft_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t* bitmap)
{
  if ( y >= tft.height() ) return 0;
  tft.pushImage(x, y, w, h, bitmap);
  return 1;
}
void wettersetup () {
  // Check WiFi connection status
  if (WiFi.status() == WL_CONNECTED) {
    String serverPath = "http://api.openweathermap.org/data/2.5/onecall?lat=" + lg + "&lon=" + bg + "&exclude=alerts,hourly,minutely&appid=XXX&units=metric";
    jsonBuffer = httpGETRequest(serverPath.c_str());
    //Serial.println(jsonBuffer);
    JSONVar myObject = JSON.parse(jsonBuffer);

    // JSON.typeof(jsonVar) can be used to get the type of the var
    if (JSON.typeof(myObject) == "undefined") {
      Serial.println("Parsing input failed!");
      return;
    }

    
    Serial.print(round(myObject["current"] ["temp"]));
   
    
}

I was hoping for a simple but complete example. As it is I have no idea which is the main .ino file, which is the subsidiary .ino file or which variable you are having a problem with

Here you are creating new local variables with the same name as your global variables. Your setup() function is setting the local variables! Try:

  bg = String(gps.location.lat());
  lg = String(gps.location.lng());

The first quoted code is the main ino file. The second code is one ino file. My problem is with the variables lg and bg.

See reply #11

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.