I was trying to get the location of NodeMcu using unwiredlabs API obtained from www.unwiredlabs.com .
I referred this example: https://www.instructables.com/id/Location-Tracker-With-NodeMCU-ESP8266/ which is in ArduinoJson v5. I changed it to version v6. I compiled the below code, but it returned an error stated below :
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include "ESP8266WiFi.h"
// your network SSID (name) & network password
char myssid[] = "your id";
char mypass[] = "your pass";
// 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 = "your api";
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];
DynamicJsonDocument doc(1024); //V6
// 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);
const char* sslFingerprint = "c3 3e 2f 21 cb 15 4e 02 d5 27 e5 f6 ef fb 31 ae 91 51 a3 5d";
client.setFingerprint(sslFingerprint);
if (client.connect(Host, 443)) { //ERROR IS HERE, NO CONNECTS
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(1000);
}
else Serial.println("Client not connected");
//Read and parse all the lines of the reply from server
while (client.available()) {
String line = client.readStringUntil('\r');
DeserializationError error = deserializeJson(doc, line); //V6
if (error)
{
Serial.println("ERROR!");
return;
}
latitude = doc["lat"];
longitude = doc["lon"];
accuracy = doc["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(4000);
}
But after running the code, it shows "Client not connected"
What may be the problem ?
blh64
June 14, 2021, 4:20pm
2
That is just example code. Did you fill in the proper credentials for YOUR network?
// your network SSID (name) & network password
char myssid[] = "your id";
char mypass[] = "your pass";
and did your obtain and use a proper API Token?
String token = "your api";
Yes, I have tested many times, even I tried with Google Maps Geolocation API, but still shows the same error.
orasion:
www.unwiredlabs.com
This says to use "us1.unwiredlabs.com " or "us2.unwiredlabs.com " depending on your location.
After updating the fingerprint, client connected. But still there's error !
Again, you are connecting to 'www' and not one of the API addresses they provide.
I have modified the code by adding the SSL fingerprint, present below
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include "ESP8266WiFi.h"
const char *sslFingerprint = "2e cd e9 ee ff 45 5c 61 b4 a9 ca 53 47 39 d0 6e 97 46 51 7e";
// your network SSID (name) & network password
char myssid[] = " ";
char mypass[] = " ";
// unwiredlabs Hostname & Geolocation Endpoint url
const char* Host = "ap1.unwiredlabs.com";
String endpoint = "/v2/process.php";
// UnwiredLabs API_Token. Signup here to get a free token https://unwiredlabs.com/trial
String token = " ";
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];
DynamicJsonDocument doc(1024); //V6
// 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);
client.setFingerprint(sslFingerprint);
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');
auto error = deserializeJson(doc, line); //V6
if (error)
{
Serial.print(F("deserializeJson() failed with code "));
Serial.println(error.c_str());
return;
}
latitude = doc["lat"];
longitude = doc["lon"];
accuracy = doc["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);
}
Now the client "unwiredlabs" connects but cannot receive the location
Is the entire JSON document in one line?
No sir, below is the format
{
"token": " ",
"radio": "gsm",
"mcc": 310,
"mnc": 404,
"cells": [{
"lac": 7033,
"cid": 17811
}],
"wifi": [{
"bssid": "00:17:c5:cd:ca:aa",
"channel": 11,
"frequency": 2412,
"signal": -51
}, {
"bssid": "d8:97:ba:c2:f0:5a"
}],
"address": 1
}
My guess is that the deserialization won't work on a single line and you will have to read all of the characters before you deserialize. There may be an option in the library to do a line at a time. I have not used the library.
There are many reasons for which DeserializationJson Invalid Input error occurs, but I can't understand what is the exact problem
Try printing out 'line' before you pass it to deserializeJson(doc, line);. I expect you will see something like:
Connected
line="{"
deserializeJson() failed with code InvalidInput
Try changing:
String line = client.readStringUntil('\r');
to
String line = client.readString();
system
Closed
November 10, 2021, 5:04am
16
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.