Are you curious about how your NodeMCU can track your location? It's possible, even without a GPS module and without display. The output will be coordinates where you are located and you will see them in your serial monitor.
The following setup was used for NodeMCU 1.0 (ESP-12E Module) with Arduino IDE.
What you need in order to follow this tutorial are the following components:
Furthermore, you will need:
Geolocation comes in very handy because when your GPS is down, you can still use Geolocation to track your location. Our host that provides geolocation, will be https://www.unwiredlabs.com/. Go to that website and sign up (the orange button in the upper right corner).
Go to your email and you will see your API token. Copy the API token, because you need that for the code we will be using. This is how the email looks like:
Hello!
Thanks for signing up with Unwired Labs LocationAPI! Your API token is 'your API code is here' (without quotes). This will give 100 requests/ day for free - forever.
If you'd like to track 5 devices for free, please respond with the following details and we'll upgrade your account within 12 hours:
1. Deployment type (Hardware/ App/ Other):
2. About your project:
3. Website:
You can login to your dashboard here: https://unwiredlabs.com/dashboard. If you run into trouble or have questions, reply to this email and I'll help you out!
Happy Locating!
Sagar
Unwired Labs
Make a new sketch and add the following code in Arduino. Write your own wifi/hotspot name and your password. Paste the API token you received in the email. Upload your code to your NodeMCU.
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include "ESP8266WiFi.h"
// your network SSID (name) & network password char myssid[] = "Your wifi/hotspot name"; char mypass[] = "Your password";
// unwiredlabs Hostname & Geolocation Endpoint url const char* Host = "www.unwiredlabs.com"; String endpoint = "/v2/process.php";
// UnwiredLabs API_Token. Signup here to get a free token https://unwiredlabs.com/trial String token = "d99cccda52ec0b";
String jsonString = "{\n";
// Variables to store unwiredlabs response double latitude = 0.0; double longitude = 0.0; double accuracy = 0.0;
void setup(){ Serial.begin(115200);
// Set WiFi to station mode and disconnect from an AP if it was previously connected WiFi.mode(WIFI_STA); WiFi.disconnect(); Serial.println("Setup done");
// We start by connecting to a WiFi network Serial.print("Connecting to "); Serial.println(myssid); WiFi.begin(myssid, mypass);
while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("."); }
void loop() { char bssid[6]; DynamicJsonBuffer jsonBuffer;
// WiFi.scanNetworks will return the number of networks found int n = WiFi.scanNetworks(); Serial.println("scan done");
if (n == 0 ) { Serial.println("No networks available"); } else { Serial.print(n); Serial.println(" networks found"); }
// now build the jsonString... jsonString = "{\n"; jsonString += "\"token\" : \""; jsonString += token; jsonString += "\",\n"; jsonString += "\"id\" : \"saikirandevice01\",\n"; jsonString += "\"wifi\": [\n"; for (int j = 0; j < n; ++j) { jsonString += "{\n"; jsonString += "\"bssid\" : \""; jsonString += (WiFi.BSSIDstr(j)); jsonString += "\",\n"; jsonString += "\"signal\": "; jsonString += WiFi.RSSI(j); jsonString += "\n"; if (j < n - 1) { jsonString += "},\n"; } else { jsonString += "}\n"; } } jsonString += ("]\n"); jsonString += ("}\n"); Serial.println(jsonString);
WiFiClientSecure client;
//Connect to the client and make the api call Serial.println("Requesting URL: https://" + (String)Host + endpoint); if (client.connect(Host, 443)) { Serial.println("Connected"); client.println("POST " + endpoint + " HTTP/1.1"); client.println("Host: " + (String)Host); client.println("Connection: close"); client.println("Content-Type: application/json"); client.println("User-Agent: Arduino/1.0"); client.print("Content-Length: "); client.println(jsonString.length()); client.println(); client.print(jsonString); delay(500); }
//Read and parse all the lines of the reply from server while (client.available()) { String line = client.readStringUntil('\r'); JsonObject& root = jsonBuffer.parseObject(line); if (root.success()) { latitude = root["lat"]; longitude = root["lon"]; accuracy = root["accuracy"];
Serial.println(); Serial.print("Latitude = "); Serial.println(latitude, 6); Serial.print("Longitude = "); Serial.println(longitude, 6); Serial.print("Accuracy = "); Serial.println(accuracy); } }
Serial.println("closing connection"); Serial.println(); client.stop();
delay(5000); }
Go to tools in Arduino and open the serial monitor. To see if you're connected to the internet, you should see the following in the serial monitor:
Setup done Connecting to (your wifi name) ... scan done
If it worked succesfully, you should see under scan done a whole list of data. The only thing we need is the code under the requesting URL, so we will need the latitude and the longitude. These are the coordinates.
Requesting URL: https://www.unwiredlabs.com/v2/process.php
Connected
Latitude = 52.385259
Longitude = 5.196099
Accuracy = 41.00
closing connection
After 5 seconds the code will constantly update and you will probably see the latitude, longitude and accuracy change. That's because the API is trying it's best to track the location as precisely as possible.
Go to https://www.google.com/maps/ and type your coordinates in the search bar. The coordinates need to be written in the following way: 52.385259,5.196099. Google Maps should show where you're located on the map.