Reverse Geocoding with Google API and ArduinoJson

Ive been trying to get google API to take my request and parse it with my ESP8266. The serialization works (I know this b/c I can copy and paste it to my browser and get a response) but the deserialization has been giving me real problems. I have spend a lot of time on the ArduinoJson documentation and have yet to find an answer that works. If anyone has any experience with Arduinojson I would appreciate any help.

(And before you ask, I cannot upgrade to Json V6)

#include <ESP8266HTTPClient.h>
#include "ESP8266WiFi.h"
WiFiServer server(80); //default server port is 80
char myssid[] = "(my said)";         // your network SSID (name)
char mypass[] = "(my pass)";          // your network password
#include <ArduinoJson.h>
String key = "(my key)";
const char* Host = "www.googleapis.com";
String thisPage = "/maps/api/geocode/json?latlng=";


 void getGeocode(double lat, double lng) {
    DynamicJsonBuffer jsonBuffer;
  Serial.println("Geocode start");
  String geocodeJson;
  geocodeJson += "https://maps.googleapis.com/maps/api/geocode/json?latlng=";
  geocodeJson += String(lat,6) + "," + String(lng,6);
  geocodeJson += "&key=" + key;

  
  Serial.println("");
  WiFiClientSecure client;
  client.setInsecure();  
  
  Serial.print("Requesting URL: ");
  Serial.println(geocodeJson);
  Serial.println(" ");
  if (client.connect(Host, 443)) {
    Serial.println("Connected");
    client.println("POST " + thisPage + key + " 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(geocodeJson.length());
    client.println();
    client.print(geocodeJson);
    delay(500);
  } else {
      Serial.println("Cannot Connect");
  }

  while (client.available()) {
    String line = client.readStringUntil('\r');
    JsonArray& root = jsonBuffer.parse(line);
    if (root.success()) {
      const char*  formatted_address = root["results"]["formatted_address"];     
 Serial.println(formatted_address);
    } else {
      Serial.println ("root !success");
    }
  }


  Serial.println("closing connection");
  Serial.println();
  client.stop();



}







void setup(){
    Serial.begin(9600);
  //********************WIFI SETUP AND CONNECTION******************
  Serial.println("Start");
  // Set WiFi to station mode and disconnect from an AP if it was previously connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);
  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(".");
  server.begin();

}

void loop(){
  getGeocode(40.714224,-73.961452);
  delay (20000);
}


The Json file from google

What can we do with the json as an image….

It would be odd to put the 'key' both in the URL and in the POST arguments. Maybe those should be:

String thisPage = "/maps/api/geocode/json";

    client.println("POST " + thisPage + " HTTP/1.1");

Well it comes to my ESP as a file but I posted it so you would understand the nesting of it.

Do you have any sources on how to use headers? I don't really understand them.

But we can’t help with an image…

Im confused what you want because the point of the image was to show you how the json file is nested and the path that I take to reach the desired data. Ive included the entire file below if that's what you want but im still confused why the image docent help you understand the nesting. (This is a different lat/long input)

{
   "plus_code" : {
      "compound_code" : "VXJW+P24 Teaneck, NJ, USA",
      "global_code" : "87G7VXJW+P24"
   },
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "758",
               "short_name" : "758",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Stelton Street",
               "short_name" : "Stelton St",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Teaneck",
               "short_name" : "Teaneck",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Bergen County",
               "short_name" : "Bergen County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New Jersey",
               "short_name" : "NJ",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "07666",
               "short_name" : "07666",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "5344",
               "short_name" : "5344",
               "types" : [ "postal_code_suffix" ]
            }
         ],
         "formatted_address" : "758 Stelton St, Teaneck, NJ 07666, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 40.8818866,
                  "lng" : -74.0048163
               },
               "southwest" : {
                  "lat" : 40.8817377,
                  "lng" : -74.00499400000001
               }
            },
            "location" : {
               "lat" : 40.8817994,
               "lng" : -74.0049265
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.8831611302915,
                  "lng" : -74.0035561697085
               },
               "southwest" : {
                  "lat" : 40.8804631697085,
                  "lng" : -74.00625413029151
               }
            }
         },
         "place_id" : "ChIJI6QSi8nwwokR_kY7urWzR8I",
         "types" : [ "premise" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "776",
               "short_name" : "776",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Stelton Street",
               "short_name" : "Stelton St",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Teaneck",
               "short_name" : "Teaneck",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Bergen County",
               "short_name" : "Bergen County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New Jersey",
               "short_name" : "NJ",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "07666",
               "short_name" : "07666",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "5344",
               "short_name" : "5344",
               "types" : [ "postal_code_suffix" ]
            }
         ],
         "formatted_address" : "776 Stelton St, Teaneck, NJ 07666, USA",
         "geometry" : {
            "location" : {
               "lat" : 40.8823797,
               "lng" : -74.0046541
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.8837286802915,
                  "lng" : -74.00330511970849
               },
               "southwest" : {
                  "lat" : 40.88103071970851,
                  "lng" : -74.00600308029149
               }
            }
         },
         "place_id" : "ChIJzzpykcnwwokRScEYrVOLGIU",
         "plus_code" : {
            "compound_code" : "VXJW+X4 Teaneck, NJ, USA",
            "global_code" : "87G7VXJW+X4"
         },
         "types" : [ "establishment", "finance", "point_of_interest" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "VXJW+P2",
               "short_name" : "VXJW+P2",
               "types" : [ "plus_code" ]
            },
            {
               "long_name" : "Teaneck",
               "short_name" : "Teaneck",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Bergen County",
               "short_name" : "Bergen County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New Jersey",
               "short_name" : "NJ",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "VXJW+P2 Teaneck, NJ, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 40.88187500000001,
                  "lng" : -74.004875
               },
               "southwest" : {
                  "lat" : 40.88175,
                  "lng" : -74.005
               }
            },
            "location" : {
               "lat" : 40.88177,
               "lng" : -74.00493
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.88316148029151,
                  "lng" : -74.00358851970849
               },
               "southwest" : {
                  "lat" : 40.88046351970851,
                  "lng" : -74.00628648029151
               }
            }
         },
         "place_id" : "GhIJBUzg1t1wREARPDHrxVCAUsA",
         "plus_code" : {
            "compound_code" : "VXJW+P2 Teaneck, NJ, USA",
            "global_code" : "87G7VXJW+P2"
         },
         "types" : [ "plus_code" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "808-738",
               "short_name" : "808-738",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Stelton Street",
               "short_name" : "Stelton St",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Teaneck",
               "short_name" : "Teaneck",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Bergen County",
               "short_name" : "Bergen County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New Jersey",
               "short_name" : "NJ",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "07666",
               "short_name" : "07666",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "5344",
               "short_name" : "5344",
               "types" : [ "postal_code_suffix" ]
            }
         ],
         "formatted_address" : "808-738 Stelton St, Teaneck, NJ 07666, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 40.8832178,
                  "lng" : -74.00381019999999
               },
               "southwest" : {
                  "lat" : 40.8811928,
                  "lng" : -74.0049734
               }
            },
            "location" : {
               "lat" : 40.8822053,
               "lng" : -74.00439179999999
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.8835542802915,
                  "lng" : -74.00304281970848
               },
               "southwest" : {
                  "lat" : 40.8808563197085,
                  "lng" : -74.0057407802915
               }
            }
         },
         "place_id" : "ChIJFytskMnwwokRpi-dTPGlvXI",
         "types" : [ "route" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "07666",
               "short_name" : "07666",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Teaneck",
               "short_name" : "Teaneck",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Bergen County",
               "short_name" : "Bergen County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New Jersey",
               "short_name" : "NJ",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Teaneck, NJ 07666, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 40.917079,
                  "lng" : -73.9873898
               },
               "southwest" : {
                  "lat" : 40.860683,
                  "lng" : -74.0373879
               }
            },
            "location" : {
               "lat" : 40.8924932,
               "lng" : -74.0123851
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.917079,
                  "lng" : -73.9873898
               },
               "southwest" : {
                  "lat" : 40.860683,
                  "lng" : -74.0373879
               }
            }
         },
         "place_id" : "ChIJkbn9D7nwwokRzyY4Xym6VXY",
         "types" : [ "postal_code" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Teaneck",
               "short_name" : "Teaneck",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Bergen County",
               "short_name" : "Bergen County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New Jersey",
               "short_name" : "NJ",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Teaneck, NJ, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 40.917079,
                  "lng" : -73.9873898
               },
               "southwest" : {
                  "lat" : 40.8598989,
                  "lng" : -74.0373879
               }
            },
            "location" : {
               "lat" : 40.8932469,
               "lng" : -74.0116536
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.917079,
                  "lng" : -73.9873898
               },
               "southwest" : {
                  "lat" : 40.8598989,
                  "lng" : -74.0373879
               }
            }
         },
         "place_id" : "ChIJd-L_HbrwwokRzzONycC4Qmc",
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Bergen County",
               "short_name" : "Bergen County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New Jersey",
               "short_name" : "NJ",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Bergen County, NJ, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 41.1332421,
                  "lng" : -73.89397889999999
               },
               "southwest" : {
                  "lat" : 40.76159,
                  "lng" : -74.272226
               }
            },
            "location" : {
               "lat" : 40.9262762,
               "lng" : -74.07701
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 41.1332421,
                  "lng" : -73.89397889999999
               },
               "southwest" : {
                  "lat" : 40.76159,
                  "lng" : -74.272226
               }
            }
         },
         "place_id" : "ChIJi82Epg0Aw4kRBYbf88CH42w",
         "types" : [ "administrative_area_level_2", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "New Jersey",
               "short_name" : "NJ",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "New Jersey, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 41.357423,
                  "lng" : -73.88506
               },
               "southwest" : {
                  "lat" : 38.788657,
                  "lng" : -75.56358609999999
               }
            },
            "location" : {
               "lat" : 40.0583238,
               "lng" : -74.4056612
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 41.357423,
                  "lng" : -73.88506
               },
               "southwest" : {
                  "lat" : 38.788657,
                  "lng" : -75.56358609999999
               }
            }
         },
         "place_id" : "ChIJn0AAnpX7wIkRjW0_-Ad70iw",
         "types" : [ "administrative_area_level_1", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "United States",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 74.071038,
                  "lng" : -66.885417
               },
               "southwest" : {
                  "lat" : 18.7763,
                  "lng" : 166.9999999
               }
            },
            "location" : {
               "lat" : 37.09024,
               "lng" : -95.712891
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 74.071038,
                  "lng" : -66.885417
               },
               "southwest" : {
                  "lat" : 18.7763,
                  "lng" : 166.9999999
               }
            }
         },
         "place_id" : "ChIJCzYy5IS16lQRQrfeQ5K5Oxw",
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}

I’m trying to understand what you meant with that

What do you want to extract?
And wondering if a piece of code would be what you need. But for that having the json as text would be needed

The API documentation طلب الاستجابة العكسية للموقع الجغرافي (البحث عن العنوان) والردّ عليه  |  Geocoding API  |  Google Developers shows this URL:

https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY

This would be a "GET" request, not a "POST" request. Try changing "POST" to "GET" and putting the full URL in there.

  if (client.connect(Host, 443)) {
    Serial.println("Connected");
    client.println("GET " + geocodeJson + " HTTP/1.1");
    client.println("Host: " + (String)Host);
    client.println("Connection: close");
    client.println("Content-Type: application/json");
    client.println();

Are you using ArduinJson V5 or ArduinoJson V6? The ArduinoJson "Assistant" will help you by writing example code for deserializing:

// Stream& client;

DynamicJsonDocument doc(2048);

DeserializationError error = deserializeJson(doc, client);

if (error) {
  Serial.print(F("deserializeJson() failed: "));
  Serial.println(error.f_str());
  return;
}

const char* plus_code_compound_code = doc["plus_code"]["compound_code"]; // "P27Q+MCM New York, NY, USA"
const char* plus_code_global_code = doc["plus_code"]["global_code"]; // "87G8P27Q+MCM"

JsonArray results = doc["results"];

const char* results_0_formatted_address = results[0]["formatted_address"]; // "277 Bedford Ave, ...

const char* results_0_types_0 = results[0]["types"][0]; // "street_address"

const char* results_1_formatted_address = results[1]["formatted_address"]; // "279 Bedford Ave, ...

const char* results_1_types_0 = results[1]["types"][0]; // "premise"

const char* results_2_formatted_address = results[2]["formatted_address"]; // "277 Bedford Ave, ...

const char* results_2_types_0 = results[2]["types"][0]; // "establishment"
const char* results_2_types_1 = results[2]["types"][1]; // "point_of_interest"

const char* results_3_formatted_address = results[3]["formatted_address"]; // "291-275 Bedford Ave, ...

const char* results_3_types_0 = results[3]["types"][0]; // "route"

const char* results_4_formatted_address = results[4]["formatted_address"]; // "P27Q+MC New York, NY, ...

const char* results_4_types_0 = results[4]["types"][0]; // "plus_code"

const char* results_5_formatted_address = results[5]["formatted_address"]; // "South Williamsburg, ...

const char* results_5_types_0 = results[5]["types"][0]; // "neighborhood"
const char* results_5_types_1 = results[5]["types"][1]; // "political"

const char* results_6_formatted_address = results[6]["formatted_address"]; // "Brooklyn, NY 11211, USA"

const char* results_6_types_0 = results[6]["types"][0]; // "postal_code"

const char* results_7_formatted_address = results[7]["formatted_address"]; // "Williamsburg, Brooklyn, ...

const char* results_7_types_0 = results[7]["types"][0]; // "neighborhood"
const char* results_7_types_1 = results[7]["types"][1]; // "political"

const char* results_8_formatted_address = results[8]["formatted_address"]; // "Kings County, Brooklyn, ...

const char* results_8_types_0 = results[8]["types"][0]; // "administrative_area_level_2"
const char* results_8_types_1 = results[8]["types"][1]; // "political"

const char* results_9_formatted_address = results[9]["formatted_address"]; // "Brooklyn, NY, USA"

JsonArray results_9_types = results[9]["types"];
const char* results_9_types_0 = results_9_types[0]; // "political"
const char* results_9_types_1 = results_9_types[1]; // "sublocality"
const char* results_9_types_2 = results_9_types[2]; // "sublocality_level_1"

const char* results_10_formatted_address = results[10]["formatted_address"]; // "New York, NY, USA"

const char* results_10_types_0 = results[10]["types"][0]; // "locality"
const char* results_10_types_1 = results[10]["types"][1]; // "political"

const char* results_11_formatted_address = results[11]["formatted_address"]; // "New York, USA"

const char* results_11_types_0 = results[11]["types"][0]; // "administrative_area_level_1"
const char* results_11_types_1 = results[11]["types"][1]; // "political"

const char* results_12_formatted_address = results[12]["formatted_address"]; // "United States"

const char* results_12_types_0 = results[12]["types"][0]; // "country"
const char* results_12_types_1 = results[12]["types"][1]; // "political"

const char* status = doc["status"]; // "OK"

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.