Can't parse GPS output from ublox neo 6m into openweathermap API

Hi all,
I want to be able to get a weather forecast when I'm having my holidays on board of my caravan. GPS reception works fine and also connection with openweathermap is OK. I'm using a NodeMCU 1.2 ESP-12. Typical GPS output is e.g. lat 52.302945 and lon 4.968453 (Amsterdam). Typical input needed for the openweathermap API looks the same, but when I parse the coordinates into the weatheronline API, the output on the serial monitor is nan and null. I have tried to convert the GPS output to several formats (double, float, char, string) but up to now nothing helped. Any suggestions how to solve this?

My code:

/*
Sketch aimed at parsing GPS data from NMEA sentence
into openweather API for 8-day's weather forecast

part of sketch concerning GPS from:
https://lastminuteengineers.com/neo6m-gps-arduino-tutorial/

part of sketch concerning weather forecast from:
https://openweathermap.org/api/one-call-3#current

GPS output in displayInfo() section, rules 1110 & 111 (typically 52.303012 & 4.968256),
for use in loop, line 65, where manual input of the coordinates works fine, but copy & paste of GPS output does not 

NodeMCU 1.0 (ESP-12E) on Com3
*/

#include <TinyGPS++.h>
#include <SoftwareSerial.h>

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <Arduino_JSON.h>
#include <ESP32Time.h>

TinyGPSPlus gps;

SoftwareSerial gpsSerial(4, 5);

ESP32Time rtc1(-3600);  // offset GMT-1

const char* ssid = "***";
const char* password = "***";
String openWeatherMapApiKey = "***";

unsigned long lastTime = 600000;    //om een beginronde te starten, daarna elke 600000 milliseconden
unsigned long timerDelay = 600000;  //10 minuten 600000

String jsonBuffer;
String description[8];
JSONVar datum;
JSONVar temperatuur;

void setup() {
  Serial.begin(9600);
  gpsSerial.begin(9600);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

void loop() {
  while (gpsSerial.available() > 0)
    if (gps.encode(gpsSerial.read()))
      displayInfo();

  if (millis() > 5000 && gps.charsProcessed() < 10) {
    Serial.println("No GPS detected");
    while (true)
      ;
  }
  If((millis() - lastTime) > timerDelay) {
    if (WiFi.status() == WL_CONNECTED) {
      String serverPath = "http://api.openweathermap.org/data/3.0/onecall?lat=latitude&lon=longitude&exclude=current,minutely,hourly&lang=NL&appid=***";
      jsonBuffer = httpGETRequest(serverPath.c_str());
      JSONVar myObject = JSON.parse(jsonBuffer);

      if (JSON.typeof(myObject) == "undefined") {
        return;
      }

      for (int i = 0; i <= 7; i++) {
        datum = myObject["daily"][i]["dt"];
        rtc1.setTime(datum);
        Serial.print(rtc1.getTime("%a %d     "));
        temperatuur = myObject["daily"][i]["temp"]["day"];
        Serial.print(double(temperatuur) - 272.15, 1);
        Serial.print("     ");
        Serial.println(myObject["daily"][i]["weather"][0]["description"]);
      }
    }
    lastTime = millis();
  }
}
String httpGETRequest(const char* serverName) {
  WiFiClient client;
  HTTPClient http;

  http.begin(client, serverName);

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

  return payload;
}
void displayInfo() {
  if (gps.location.isValid()) {
    latitude = Serial.println(gps.location.lat(), 6);   // latitude
    longitude = Serial.println(gps.location.lng(), 6);  // longitude
  } else {
    Serial.println("Location: Not Available");
  }
  Serial.println();
  delay(1000);
}
type or paste code here

You are not embedding your variables latitude and longitude into that string. You are just putting the ASCII characters there. If you want the value of your variables, you need to build up your String

      String serverPath = "http://api.openweathermap.org/data/3.0/onecall?lat=" + String(latitude,6) + "&lon=" + String(longitude,6) + "&exclude=current,minutely,hourly&lang=NL&appid=***";

Thank you so much! An elegant solution! The code works fine now!

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