-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathGeolocation.ino
286 lines (243 loc) · 7.49 KB
/
Geolocation.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*********************************************************************
This is the code for Fetching your location from Google Geolocation API
This code is provided by
techiesms
*********************************************************************/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include "ESP8266WiFi.h"
char myssid[] = "SSID"; // your network SSID (name)
char mypass[] = "PASS"; // your network password
//Credentials for Google GeoLocation API...
const char* Host = "www.googleapis.com";
String thisPage = "/geolocation/v1/geolocate?key=";
String key = "YOUR_API_KEY";
int status = WL_IDLE_STATUS;
String jsonString = "{\n";
double latitude = 0.0;
double longitude = 0.0;
double accuracy = 0.0;
int more_text = 1; // set to 1 for more debug output
#define OLED_RESET LED_BUILTIN //4
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000
};
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
void setup() {
Serial.begin(9600);
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(10,25);
display.println("techiesms");
display.setTextColor(WHITE); // 'inverted' text
display.display();
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(".");
}
void loop() {
char bssid[6];
DynamicJsonBuffer jsonBuffer;
Serial.println("scan start");
// WiFi.scanNetworks will return the number of networks found
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) {
// Print out the formatted json...
Serial.println("{");
Serial.println("\"homeMobileCountryCode\": 234,"); // this is a real UK MCC
Serial.println("\"homeMobileNetworkCode\": 27,"); // and a real UK MNC
Serial.println("\"radioType\": \"gsm\","); // for gsm
Serial.println("\"carrier\": \"Vodafone\","); // associated with Vodafone
//Serial.println("\"cellTowers\": ["); // I'm not reporting any cell towers
//Serial.println("],");
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\": ");
Serial.println(WiFi.RSSI(i));
if (i < n - 1)
{
Serial.println("},");
}
else
{
Serial.println("}");
}
}
Serial.println("]");
Serial.println("}");
}
Serial.println(" ");
}
// now build the jsonString...
jsonString = "{\n";
jsonString += "\"homeMobileCountryCode\": 234,\n"; // this is a real UK MCC
jsonString += "\"homeMobileNetworkCode\": 27,\n"; // and a real UK MNC
jsonString += "\"radioType\": \"gsm\",\n"; // for gsm
jsonString += "\"carrier\": \"Vodafone\",\n"; // associated with Vodafone
jsonString += "\"wifiAccessPoints\": [\n";
for (int j = 0; j < n; ++j)
{
jsonString += "{\n";
jsonString += "\"macAddress\" : \"";
jsonString += (WiFi.BSSIDstr(j));
jsonString += "\",\n";
jsonString += "\"signalStrength\": ";
jsonString += WiFi.RSSI(j);
jsonString += "\n";
if (j < n - 1)
{
jsonString += "},\n";
}
else
{
jsonString += "}\n";
}
}
jsonString += ("]\n");
jsonString += ("}\n");
//--------------------------------------------------------------------
Serial.println("");
WiFiClientSecure client;
//Connect to the client and make the api call
Serial.print("Requesting URL: ");
Serial.println("https://" + (String)Host + thisPage + "AIzaSyCYNXIYINPmTNIdusMjJloS4_BXSOff1_g");
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(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');
if (more_text) {
Serial.print(line);
}
JsonObject& root = jsonBuffer.parseObject(line);
if (root.success()) {
latitude = root["location"]["lat"];
longitude = root["location"]["lng"];
accuracy = root["accuracy"];
}
}
Serial.println("closing connection");
Serial.println();
client.stop();
Serial.print("Latitude = ");
Serial.println(latitude, 6);
Serial.print("Longitude = ");
Serial.println(longitude, 6);
Serial.print("Accuracy = ");
Serial.println(accuracy);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(15, 0);
display.println("Routers");
display.setTextColor(WHITE); // 'inverted' text
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(40, 30);
display.println(n);
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(15, 0);
display.println("Latitude");
display.setTextColor(WHITE); // 'inverted' text
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(15, 30);
display.println(latitude, 6);
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(15, 0);
display.println("Longitude");
display.setTextColor(WHITE); // 'inverted' text
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(15, 30);
display.println(longitude, 6);
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(15, 0);
display.println("Accuracy");
display.setTextColor(WHITE); // 'inverted' text
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(20, 30);
display.println(accuracy);
display.display();
delay(2000);
}