· 6 years ago · Nov 22, 2019, 06:10 AM
1/*********************************************************************
2 This is the code for Fetching your location from Google Geolocation API
3
4 This code is provided by
5 techiesms
6*********************************************************************/
7
8#include <ESP8266HTTPClient.h>
9#include <ArduinoJson.h>
10#include "ESP8266WiFi.h"
11
12char myssid[] = "SSID"; // your network SSID (name)
13char mypass[] = "PASS"; // your network password
14
15
16//Credentials for Google GeoLocation API...
17const char* Host = "www.googleapis.com";
18String thisPage = "/geolocation/v1/geolocate?key=";
19String key = "YOUR_API_KEY";
20
21int status = WL_IDLE_STATUS;
22String jsonString = "{\n";
23
24double latitude = 0.0;
25double longitude = 0.0;
26double accuracy = 0.0;
27int more_text = 1; // set to 1 for more debug output
28
29
30
31
32void setup() {
33 Serial.begin(9600);
34
35
36 Serial.println("Start");
37 // Set WiFi to station mode and disconnect from an AP if it was previously connected
38 WiFi.mode(WIFI_STA);
39 WiFi.disconnect();
40 delay(100);
41 Serial.println("Setup done");
42 // We start by connecting to a WiFi network
43 Serial.print("Connecting to ");
44 Serial.println(myssid);
45 WiFi.begin(myssid, mypass);
46
47 while (WiFi.status() != WL_CONNECTED) {
48 delay(500);
49 Serial.print(".");
50 }
51 Serial.println(".");
52
53
54}
55
56
57void loop() {
58
59 char bssid[6];
60 DynamicJsonBuffer jsonBuffer;
61 Serial.println("scan start");
62 // WiFi.scanNetworks will return the number of networks found
63 int n = WiFi.scanNetworks();
64 Serial.println("scan done");
65 if (n == 0)
66 Serial.println("no networks found");
67 else
68 {
69 Serial.print(n);
70 Serial.println(" networks found...");
71
72 if (more_text) {
73 // Print out the formatted json...
74 Serial.println("{");
75 Serial.println("\"homeMobileCountryCode\": 234,"); // this is a real UK MCC
76 Serial.println("\"homeMobileNetworkCode\": 27,"); // and a real UK MNC
77 Serial.println("\"radioType\": \"gsm\","); // for gsm
78 Serial.println("\"carrier\": \"Vodafone\","); // associated with Vodafone
79 //Serial.println("\"cellTowers\": ["); // I'm not reporting any cell towers
80 //Serial.println("],");
81 Serial.println("\"wifiAccessPoints\": [");
82 for (int i = 0; i < n; ++i)
83 {
84 Serial.println("{");
85 Serial.print("\"macAddress\" : \"");
86 Serial.print(WiFi.BSSIDstr(i));
87 Serial.println("\",");
88 Serial.print("\"signalStrength\": ");
89 Serial.println(WiFi.RSSI(i));
90 if (i < n - 1)
91 {
92 Serial.println("},");
93 }
94 else
95 {
96 Serial.println("}");
97 }
98 }
99 Serial.println("]");
100 Serial.println("}");
101 }
102 Serial.println(" ");
103 }
104 // now build the jsonString...
105 jsonString = "{\n";
106 jsonString += "\"homeMobileCountryCode\": 234,\n"; // this is a real UK MCC
107 jsonString += "\"homeMobileNetworkCode\": 27,\n"; // and a real UK MNC
108 jsonString += "\"radioType\": \"gsm\",\n"; // for gsm
109 jsonString += "\"carrier\": \"Vodafone\",\n"; // associated with Vodafone
110 jsonString += "\"wifiAccessPoints\": [\n";
111 for (int j = 0; j < n; ++j)
112 {
113 jsonString += "{\n";
114 jsonString += "\"macAddress\" : \"";
115 jsonString += (WiFi.BSSIDstr(j));
116 jsonString += "\",\n";
117 jsonString += "\"signalStrength\": ";
118 jsonString += WiFi.RSSI(j);
119 jsonString += "\n";
120 if (j < n - 1)
121 {
122 jsonString += "},\n";
123 }
124 else
125 {
126 jsonString += "}\n";
127 }
128 }
129 jsonString += ("]\n");
130 jsonString += ("}\n");
131 //--------------------------------------------------------------------
132
133 Serial.println("");
134
135 WiFiClientSecure client;
136
137 //Connect to the client and make the api call
138 Serial.print("Requesting URL: ");
139 Serial.println("https://" + (String)Host + thisPage + "AIzaSyCYNXIYINPmTNIdusMjJloS4_BXSOff1_g");
140 Serial.println(" ");
141 if (client.connect(Host, 443)) {
142 Serial.println("Connected");
143 client.println("POST " + thisPage + key + " HTTP/1.1");
144 client.println("Host: " + (String)Host);
145 client.println("Connection: close");
146 client.println("Content-Type: application/json");
147 client.println("User-Agent: Arduino/1.0");
148 client.print("Content-Length: ");
149 client.println(jsonString.length());
150 client.println();
151 client.print(jsonString);
152 delay(500);
153 }
154
155 //Read and parse all the lines of the reply from server
156 while (client.available()) {
157 String line = client.readStringUntil('\r');
158 if (more_text) {
159 Serial.print(line);
160 }
161 JsonObject& root = jsonBuffer.parseObject(line);
162 if (root.success()) {
163 latitude = root["location"]["lat"];
164 longitude = root["location"]["lng"];
165 accuracy = root["accuracy"];
166 }
167 }
168
169 Serial.println("closing connection");
170 Serial.println();
171 client.stop();
172
173 Serial.print("Latitude = ");
174 Serial.println(latitude, 6);
175 Serial.print("Longitude = ");
176 Serial.println(longitude, 6);
177 Serial.print("Accuracy = ");
178 Serial.println(accuracy);
179
180
181
182
183}