BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Google Geolocation API

#1

Hello Everyone!! I’m new in blynk. Could you please tell me how to store latitude & longitude values on the variables from this code? I’ve used a valid API, ssid & pass for WiFi, but it won’t show the lat & long value in the serial monitor. I,ve been waited for a long. But nothing is displayed on the serial monitor after connected with the network. If I able to get the values then I’ll modify the code for blynk map by my won. For now, please tell me where is the problem in this code? Why the values are not shown on the serial monitor??

#ifdef ARDUINO_ARCH_SAMD
#include <WiFi101.h>
#elif defined ARDUINO_ARCH_ESP8266
#include <ESP8266WiFi.h>
#elif defined ARDUINO_ARCH_ESP32
#include <WiFi.h>
#include <ESP8266HTTPClient.h>
#else
#error Wrong platform
#endif 

#include <WifiLocation.h>

const char* googleApiKey = "#########################";
const char ssid[] = "#######";
const char pass[] = "######";

WifiLocation location(googleApiKey);

void setup() {
    Serial.begin(115200);
    // Connect to WPA/WPA2 network
#ifdef ARDUINO_ARCH_ESP32
    WiFi.mode(WIFI_MODE_STA);
#endif
#ifdef ARDUINO_ARCH_ESP8266
    WiFi.mode(WIFI_STA);
#endif
    WiFi.begin(ssid, pass);
    while (WiFi.status() != WL_CONNECTED) {
        Serial.print("Attempting to connect to WPA SSID: ");
        Serial.println(ssid);
        // wait 5 seconds for connection:
        Serial.print("Status = ");
        Serial.println(WiFi.status());
        delay(500);
    }
    location_t loc = location.getGeoFromWiFi();

    Serial.println("Location request data");
    Serial.println(location.getSurroundingWiFiJson());
    Serial.println("Latitude: " + String(loc.lat, 7));
    Serial.println("Longitude: " + String(loc.lon, 7));
    Serial.println("Accuracy: " + String(loc.accuracy));


}

void loop() {

}
0 Likes

#2

Please edit your post to add triple backticks at the beginning and end of your code, so that it displays correctly.
Triple backticks look like this:
```

Pete.

1 Like

#3

hello dear, I’ve edited the post. Please help. This is the code from github: https://github.com/gmag11/WifiLocation

0 Likes

#4

How is this related to Blynk?
What is it that you’re trying to achieve?

Pete.

0 Likes

#5

I just want to make this work for blynk map. That is, I want to give the lat & lon value of the blynk map via geolocation api, not manually stated on the code.

0 Likes

#6

If I can display it, then I’ll use the values here.

WidgetMap myMap(V1);
...
int index = 1;
float lat = 51.5074;
float lon = 0.1278;
myMap.location(index, lat, lon, "value");

0 Likes

#7

Waiting won’t help. The code to get the location is in void setup, so runs once on boot, 500ms after the Wi-Fi Status is printed to the serial monitor.

You should probably raise an issue on the github page for this library and let the author sort-out the issue.

Pete.

1 Like

#8

Also, I assume that you’ve done all the things that it says on the github page - assigning a billing method, then logging in to the Developer Console and enabling the geolocation API?

Pete.

0 Likes

#9

is that okay? I have flashed the board 3times & it shows that I’ve used the key 3times… Is that mean my API that I’ve been created on the google is okay?

0 Likes

#10

I have no idea, I’ve never used this service.

Pete.

1 Like

#11

okay, thanks

0 Likes

#12

Have your tried to put this in your void loop? - @PeteKnight has already explained that with this code in your void setup() that it will only run once and won’t update constantly.

    location_t loc = location.getGeoFromWiFi();

    Serial.println("Location request data");
    Serial.println(location.getSurroundingWiFiJson());
    Serial.println("Latitude: " + String(loc.lat, 7));
    Serial.println("Longitude: " + String(loc.lon, 7));
    Serial.println("Accuracy: " + String(loc.accuracy));
1 Like

#13

A timer controlled function would be better… no need to update the geolocation data thousands of times a second :stuck_out_tongue:

2 Likes

#14

I agree, just getting them started first :wink: there’s nothing related to Blynk in there at the moment so the beautifully crafted BlynkTimer isn’t at their disposal :laughing:

0 Likes

#15

Even for a snail ?

0 Likes

#16

:snail: :snail: :snail:

1 Like

#17

True :stuck_out_tongue: Looks like another example of someone joining the forum without doing any reading and asking for us to fix things… hopefully not, but we will see.

However, (for the OP’s info) Blynktimer is based on Simpletimer which will work the same, and allow a more reasonable reading rate with “normal” Arduino code.

2 Likes