Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266HTTPClient.h>
- #include <ArduinoJson.h>
- #include "ESP8266WiFi.h"
- //Credentials for Google GeoLocation API...
- const char* Host = "www.googleapis.com";
- String thisPage = "/geolocation/v1/geolocate?key=";
- String key = "YourAPIKEY";
- 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 SERIAL_BAUD 115200
- #define WIFI_DELAY 5000
- #define MAX_SSID_LEN 32
- #define MAX_CONNECT_TIME 30000
- char ssid[MAX_SSID_LEN] = "";
- // ------------------------------------------------------------------------------------
- void setup() {
- Serial.begin(115200);
- 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
- ConnectOpenWifi();
- }
- // --------------------------------------------------------------------------------
- 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: ");
- // ---- Get Google Maps Api Key here, Link: https://developers.google.com/maps/documentation/geolocation/intro
- Serial.println("https://" + (String)Host + thisPage + key);
- 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, 8);
- Serial.print("Longitude = ");
- Serial.println(longitude, 8);
- Serial.print("Accuracy = ");
- Serial.println(accuracy);
- Serial.println();
- // START send Lat * long data to php server / MySQL -----
- if (WiFi.status() == WL_CONNECTED) {
- // Construct (local) php server URL to call with data to send ----
- String URLtoCall = String("http://192.168.2.27/index.php?latitude=" + String(latitude, 8) + "&longitude=" + String(longitude, 8));
- Serial.println(URLtoCall);
- HTTPClient http;
- http.begin(URLtoCall);
- int httpCode = http.GET();
- if (httpCode > 0) {
- String payload = http.getString();
- Serial.println(payload);
- }
- http.end();
- }
- // END send Lat * long data to php server / MySQL -----
- Serial.println("Waiting 30 Seconds before next Scan...");
- Serial.println();
- for (int i = 0; i < 30; ++i) {
- Serial.println(i);
- delay(1000);
- }
- }
- // --------------------------------------------------------------------------------------------------------
- void scanAndSort() {
- memset(ssid, 0, MAX_SSID_LEN);
- int n = WiFi.scanNetworks();
- Serial.println("Scan done!");
- if (n == 0) {
- Serial.println("No networks found!");
- } else {
- Serial.print(n);
- Serial.println(" networks found.");
- int indices[n];
- for (int i = 0; i < n; i++) {
- indices[i] = i;
- }
- for (int i = 0; i < n; i++) {
- for (int j = i + 1; j < n; j++) {
- if (WiFi.RSSI(indices[j]) > WiFi.RSSI(indices[i])) {
- std::swap(indices[i], indices[j]);
- }
- }
- }
- for (int i = 0; i < n; ++i) {
- Serial.print(WiFi.SSID(indices[i]));
- Serial.print(" ");
- Serial.print(WiFi.RSSI(indices[i]));
- Serial.print(" ");
- Serial.print(WiFi.encryptionType(indices[i]));
- Serial.println();
- if (WiFi.encryptionType(indices[i]) == ENC_TYPE_NONE) {
- Serial.println("Found non-encrypted network. Store it and exit to connect.");
- memset(ssid, 0, MAX_SSID_LEN);
- strncpy(ssid, WiFi.SSID(indices[i]).c_str(), MAX_SSID_LEN);
- break;
- }
- }
- }
- }
- //-----------------------------------------------------------------------
- void ConnectOpenWifi() {
- Serial.println("Started.");
- Serial.println();
- if (WiFi.status() != WL_CONNECTED) {
- WiFi.softAPdisconnect();
- WiFi.disconnect();
- WiFi.mode(WIFI_STA);
- delay(WIFI_DELAY);
- scanAndSort();
- delay(WIFI_DELAY);
- if (strlen(ssid) > 0) {
- Serial.print("Going to connect for : ");
- Serial.println(ssid);
- WiFi.begin(ssid);
- unsigned short try_cnt = 0;
- while (WiFi.status() != WL_CONNECTED && try_cnt < MAX_CONNECT_TIME / WIFI_DELAY) {
- delay(WIFI_DELAY);
- Serial.print(".");
- try_cnt++;
- }
- if (WiFi.status() == WL_CONNECTED) {
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- } else {
- Serial.println("Cannot established connection on given time.");
- }
- } else {
- Serial.println("No non-encrypted WiFi found.");
- }
- }
- Serial.println("End Loop break...");
- delay(10000);
- }
- // -------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement