Last active
November 5, 2017 13:13
-
-
Save misternay/3bd7e0a012f68491ff794a3ad731162f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ESP8266WiFi.h" | |
#include <ArduinoJson.h> | |
const char* Host = "www.googleapis.com"; | |
String thisPage = "/geolocation/v1/geolocate?key="; | |
String key = "AIzaSyDeUxorNncriqxB3c2jVz0GrLfqpFG1A4c"; | |
double latitude = 0.0; | |
double longitude = 0.0; | |
double accuracy = 30.0; | |
int check = 0; | |
void setup() { | |
Serial.begin(115200);//Serial | |
WiFi.mode(WIFI_STA);//Mode STATION | |
WiFi.disconnect(); | |
delay(100); | |
Serial.println("Setup done"); | |
WiFi.begin ("DONTCRY", "n123321456");//WIFI CONNECT | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(); | |
Serial.println("WIFI CONNECTED"); | |
} | |
void loop() { | |
if (check == 0) { | |
int countOFnetwork = getNumberOfnetwork(); | |
callRestAPI(setJsonString(countOFnetwork) , Host, thisPage, key); | |
check = 1; | |
} else { | |
Serial.println(latitude); | |
Serial.println(longitude); | |
} | |
} | |
int getNumberOfnetwork() { | |
Serial.println("scan start"); | |
int numbers = WiFi.scanNetworks();//จำนวนที่เจอนะครับ | |
Serial.println("scan done"); | |
if (numbers == 0) { | |
Serial.println("no networks found"); | |
} else | |
{ | |
Serial.println(numbers); | |
Serial.println(" networks found..."); | |
return numbers; | |
} | |
} | |
String setJsonString(int counts) { | |
String jsonS = "{\n"; | |
jsonS += "\"homeMobileCountryCode\": 234,\n"; // this is a real UK MCC | |
jsonS += "\"homeMobileNetworkCode\": 27,\n"; // and a real UK MNC | |
jsonS += "\"radioType\": \"gsm\",\n"; // for gsm | |
jsonS += "\"carrier\": \"Vodafone\",\n"; // associated with Vodafone | |
jsonS += "\"wifiAccessPoints\": [\n"; | |
for (int j = 0; j < counts; ++j) | |
{ | |
jsonS += "{\n"; | |
jsonS += "\"macAddress\" : \""; | |
jsonS += (WiFi.BSSIDstr(j)); | |
jsonS += "\",\n"; | |
jsonS += "\"signalStrength\": "; | |
jsonS += WiFi.RSSI(j); | |
jsonS += "\n"; | |
if (j < counts - 1) | |
{ | |
jsonS += "},\n"; | |
} | |
else | |
{ | |
jsonS += "}\n"; | |
} | |
} | |
jsonS += ("]\n"); | |
jsonS += ("}\n"); | |
return jsonS; | |
} | |
void callRestAPI(String jsonString, String host, String url, String key) { | |
WiFiClientSecure client; | |
DynamicJsonBuffer jsonBuffer; | |
Serial.print("Requesting URL: "); | |
Serial.println("https://" + (String)Host + thisPage + "AIzaSyCYNXIYINPmTNIdusMjJloS4_BXSOff1_g"); | |
Serial.println(" "); | |
if (client.connect(Host, 443)) { //443 | |
Serial.println("Connected"); | |
client.println("POST " + url + 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'); | |
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment