This will give the real time latitude and longitude
“ Have you ever thought of getting your device’s location without using any GPS hardware? ”
“ Are you curious about how your NodeMCU can track your location? ”
I have a solution and it is possible to get the live location coordinates just using NodeMCU ESP8266 board without any extra hardware. The output will be coordinates where you are located and you will see them in your serial monitor. This becomes possible because of Google’s Geolocation API. So let’s get start how this Geolocation API works and how we are able to get the location using this.
How Google Geolocation API works?
As you know Google takes the input of our nearby WiFi routers and in response gives us the coordinates. Smartphones have GPS and Wi-Fi enabled all the time so, Google provides an API which not only tracks our location, but it also tracks the nearby Wi-Fi networks. For example: If a person is walking in an open area with Wi-Fi and GPS enabled so his smartphone is constantly scanning the nearby Wi-Fi networks and Google continuously saves the MAC addresses and the name of this Wi-Fi networks along with the location of his phone. So based on the signal strengths received by phone, Google estimates the location of that particular Wi-Fi network, and it tries to save that data into the database. Therefore, next time if anybody will pass through that same Wi-Fi networks and does not have GPS enabled in his phone he/she can still get the location of the phone based on the location of that particular Wi-Fi network. To create your own Google Geolocation API key and to study about how Google’s Geolocation API works, go to this page.
Getting API key from Google Cloud
To use Google's Geolocation service, we need a Google account and API key. Follow below steps to get the API key.
Step 1:-Go to Google developers page, login with your Google account and click on Get API key and then click on Google Cloud Platform Console.
Step 3:-Then you need to create a project. By clicking on “NEW PROJECT”. As you can see, I have created a project by name “esp8266”.
Selecting the created project
Creating a new project
After entering all the details, click on CREATE. The page will come as shown below:
Step 4 :- On left side menu bar and click on “APIs & Services” and click on ‘Credentials’ as shown below:
The below screen will come and click on “+ CREATE CREDENTIALS”. After that, click on “API key”:
In this way, You will get your API key:
Step 5:-Now, click on “APIs & Services” which is on the left side of the panel menu bar as show below:
The page will appear as shown below:
Step 6:-Click on “+ ENABLE APIs AND SERVICES” and enable the services mentioned below:
Click on the above two tabs and ENABLE the service:
Step 7:To check whether your API Key is working or not, you can follow the link: https://yougapi.com/tools/google-api-key-checker.php Just copy and paste your API Key to see the result.
Programming NodeMCU for getting GPS data
Here, I used Arduino IDE to program my ESP8266. So, make sure you have downloaded ESP8266 board files. For the coding part, we need three libraries ESP8266HTTPClient, ArduinoJson, ESP8266WiFi.
Step 1:- We need to initialize the variables for Wi-Fi name, Wi-Fi password, API key and initialize other variables used in the code.
char ssid[] = "********";// your network SSID name char pass[] = "********";// your network password String key = "your api key";
Step 2:-In setup function, we need to set the Wi-Fi to station mode and disconnect from an AP if it was previously connected.
Step 3:-Inloop function, we will scan all the nearby Wi-Fi networks and extract the MAC address and strength of these networks. After that, we make the json string from the extracted information and send it to the Google cloud and in return of it we get the co-ordinates of the location in terms of latitude, longitude and accuracy.
Note:- Accuracy will depend on number of Wi-fi networks available nearby.
int n = WiFi.scanNetworks(); Serial.println("scan done"); if (n == 0) Serial.println("no networks found"); else { Serial.print(n); Serial.println(" networks found…"); if (more_text) { Serial.println("\"wifiAccessPoints\": ["); for (int i = 0; i < n; ++i) { Serial.println("{"); Serial.print("\"macAddress\" : \""); Serial.print(WiFi.BSSIDstr(i)); Serial.println("\","); Serial.print("\"signalStrength\": ");
Note: I used Arduino JSON6 version. So, modify your code according to your Arduino JSON version.
Step 5:-Upload the full code given at the end to the board and open the Serial monitor. You will see the co-ordinates and accuracy of your location as shown below:
You can also check & test the HTTPS URL i.e. “https://www.googleapis.com/geolocation/v1/geolocate?key=<YOURAPIKEY>” by GET request in POSTMAN, whether its working or not.
Testing the HTTPS URL in postman
Step 6:- Now, open your Google Maps in your smartphone or laptop and enter the latitude and longitude separated by comma (,). You will see your location on the map as shown.
Liked what you just read? Share it with a friend.
Have questions? Get in touch!
Request submitted. Our team will get in touch with you shortly!
Oops! Something went wrong while submitting the form.