I have a project which is a led matrix clock with a gps module and weather informations. In order to have an independent solution form the location point of view I have the gps module for accurate geolocation and time.
The problem I have is I cant find a way to pass the coordinates received from gps module to the get api enquiry.
I have this
client.println("GET /data/2.5/weather?lat=" + lat + "&lon=" + lon + "&appid=token");
and
lat = gps.location.lat();
lon = gps.location.lng();
How can I put the gps values in the get enquiry.
I tried dtostrf(lat,4, 3, outstr1); and that did not work.
The api send a 400 error code "nothing to geocode" which is normal since did not have any values for latitude and longitude.
If I put values on the lat and long everything is working as is should be.
Anybody...
Thank you
Gabi
C/C++ doesn't just magically convert integers into strings and concatenate them with other strings to create a complete string to pass to your client.println(). You can easily break this up into several calls.
Breaking the client.print() in several calls help a lot (thank you blh64). I have now values for lat and lon, but… Both values are 0 (zero). I dont understand why. If I run this:
include <Pins_Arduino.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
// The serial connection to the GPS module
SoftwareSerial ss(D2, D3); // Here are the aliased pins
TinyGPSPlus gps;
static float lat;
static float lon;
String lat1;
String lon1;
void setup() {
Serial.begin(115200);
ss.begin(9600);
}
void loop() {
while (ss.available() > 0) {
gps.encode(ss.read());
if (gps.location.isUpdated()) {
lat = gps.location.lat();
lon = gps.location.lng();
Serial.println(lat,3);
Serial.println(lon,3);
}
}
}
lat and lon appear as they should be. If I put that piece of cod in my tabs sketch and call the api both of them (lat and lon) are zero. I use tinygps++ library and I don’t know what kid of numbers that library put out. I suspect they are double.Can somebody shed a little light on this?
Then I suggest that you put together a smaller example that illustrates the problem. I expect you’ve managed to declare two variables with the same name and one is masking the other, but without seeing any code, I’m really just speculating.
When you call getLocation, you open the software serial port and read. You may get a character, you may not. But the arduino is much much faster than the speed at which the serial port can deliver bytes.
It will check available and there will be nothing there before the gps has delivered enough characters for tinygps to parse. Therefore, gps.location.isUpdated will not be true and you never get a position.
You need to keep calling getlocation until it delivers some non-zero lat lon. Best not to call ss.begin in there either - put it in setup.
void getlocation()
{
while (1)
{
if (ss.available() > 0)
{
gps.encode(ss.read());
if (gps.location.isUpdated())
{
lat = gps.location.lat();
lon = gps.location.lng();
return;
}
}
}
}
You will need to declare ss and run begin on it elsewhere.
It's dirty, but it will prove out the problem I think. it's better to read characters as they become available and pass them to the gps and only talk to your weather service once you have a fix.