
It is possible to get real-time latitude and longitude coordinates without any dedicated GPS hardware using the NodeMCU ESP8266 board. The solution relies on Google's Geolocation API, which works by scanning the MAC addresses and signal strengths of nearby Wi-Fi routers. The NodeMCU then sends this data to the API to receive the location coordinates, which are displayed in the Serial Monitor. This method provides an accuracy that depends on the number of available Wi-Fi networks nearby.
{{key-insights}}
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.
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.
To use Google's Geolocation service, we need a Google account and API key. Follow below steps to get the API key.



Selecting the created project

Creating a new project
After entering all the details, click on CREATE. The page will come 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:


The page will appear as shown below:


Click on the above two tabs and ENABLE the service:

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.
char ssid[] = "********";// your network SSID name
char pass[] = "********";// your network password
String key = "your api key";
void setup() {
Serial.begin(9600);
Serial.println("Start");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("Setup done");
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.

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
