GPS latitude and longitude in httpgetRequest

Hello there. I'm currently working on a weather station with a sensor, TFT-display and the NEO-6m GPS module. Over the internet I request the current weather with the OpenWeatherMap API. The request url needs coordinates there comes the GPS-module in. And this is my problem. How do I use the readings from the module for my url?

[code]
#include <TFT_eSPI.h>
#include <SPI.h>
#include "DHT.h"
#include "time.h"
#include <HTTPClient.h>
#include <Arduino_JSON.h>
#include <TJpg_Decoder.h>
#include <WiFiManager.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
//#include settings.h
int RXPin = 16; // Schaltplan schauen
int TXPin = 17;
int GPSBaud = 9600;
#define DHTPIN 22
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

// Bilder
#include "01d.h"
#include "02d.h"
#include "03d.h"
#include "04d.h"
#include "09d.h"
#include "10d.h"
#include "11d.h"
#include "13d.h"
#include "50d.h"


TFT_eSPI tft = TFT_eSPI();  // Invoke library, pins defined in User_Setup.h
TinyGPSPlus gps;
SoftwareSerial gpsSerial(RXPin, TXPin);

const char* ntpServer = "europe.pool.ntp.org";
const long  gmtOffset_sec = 3600;
const int   daylightOffset_sec = 3600;

// Zeitintervalle für jede Funktion aufstellen (Timer)
const unsigned long updateSensor = 30000;      // alle 30s werden die Sensordaten auf TFT gebracht
const unsigned long updatetime = 60000;        // alle 1min wird die Zeit auf TFT gebracht
const unsigned long updateweather = 420000;     // alle 7min wird die Temperatur laut OWM auf TFT gebracht; 10s für Testzwecke
const unsigned long updateicon = 420000;        // alle 7min wird das Icon der Wetterlage auf TFT gebracht; 10s für Testzwecke
unsigned long lastTimeSensor = 0;
unsigned long lastTimeTime = 0;
unsigned long lastTimeweather = 0;
unsigned long lastTimeicon = 0;

String bg;
String lg;

String jsonBuffer;

void setup() {
  gpsSerial.begin(GPSBaud);
  Serial.begin(9600);
  tft.init();                                   // TFT-Display konfigurieren
  tft.setRotation(1);                           // Displaydrehung
  tft.fillScreen(TFT_BLACK);                    // TFT-Display Hintergrund
  tft.setTextSize(1);                           // Textgröße
  tft.setTextColor(TFT_WHITE);                  // Textfarbe
  //tft.setSwapBytes(true);
  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);
  }
  dht.begin();                                  // Sensor bereit machen
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  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(), 6);
        Serial.println("Längengrad: ");
        Serial.print(gps.location.lng(), 6);
      }
      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);
  tft.fillRect(0, 0, 160, 128, TFT_BLACK);
  TJpgDec.setJpgScale(1);
  TJpgDec.setSwapBytes(true);
  TJpgDec.setCallback(tft_output);
  iconsetup();
  sensorsetup();
  wettersetup();
  zeitsetup();
}

void loop() {
  sensor();                                     // void sensor in schleife ausführen
  printLocalTime();                             // void printLocalTime in schleife ausführen
  wetter();                                     // void wetter in schleife ausführen
  iconshow();                                   // void iconshow in schleife ausführen
  
}


// 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;
}
[/code]
[code]
//extern String bg;
//extern String lg;

void wettersetup () {
    // Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
        String serverPath = "http://api.openweathermap.org/data/2.5/onecall?lat=" + bg + "&lon=" + lg + "&exclude=alerts,hourly,minutely&appid=2658a465232ebfb8f6747c2ce3232c47&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;
      }
    
      tft.setCursor(115, 103);    // JSON Daten auf TFT bringen
      tft.print(round(myObject["current"] ["temp"]));
      tft.drawString("`", 146, 98, 2); // °-Symbol
      tft.print(" C");
      tft.setCursor(85, 115);
      tft.print("Max: ");
      tft.print(round(myObject["daily"] [0] ["temp"] ["max"] ));
      tft.drawString("`", 146, 110, 2); // °-Symbol
      tft.print(" C");
      Serial.println("Wetter setup");
    }
  }
[/code]

Problem has been solved.

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