Advertisement
MrLunk

MrLunk_s_AnyThingTracker_v1_FINAL

Jan 14th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 KB | None | 0 0
  1. #include <ESP8266HTTPClient.h>
  2. #include <ArduinoJson.h>
  3. #include "ESP8266WiFi.h"
  4.  
  5. //Credentials for Google GeoLocation API...
  6. const char* Host = "www.googleapis.com";
  7. String thisPage = "/geolocation/v1/geolocate?key=";
  8. String key = "YourAPIKEY";
  9.  
  10. int status = WL_IDLE_STATUS;
  11. String jsonString = "{\n";
  12.  
  13. double latitude = 0.0;
  14. double longitude = 0.0;
  15. double accuracy = 0.0;
  16. int more_text = 1; // set to 1 for more debug output
  17.  
  18. #define SERIAL_BAUD 115200
  19. #define WIFI_DELAY 5000
  20. #define MAX_SSID_LEN 32
  21. #define MAX_CONNECT_TIME 30000
  22.  
  23. char ssid[MAX_SSID_LEN] = "";
  24.  
  25. // ------------------------------------------------------------------------------------
  26.  
  27. void setup() {
  28. Serial.begin(115200);
  29.  
  30. Serial.println("Start");
  31. // Set WiFi to station mode and disconnect from an AP if it was previously connected
  32. WiFi.mode(WIFI_STA);
  33. WiFi.disconnect();
  34. delay(100);
  35. Serial.println("Setup done");
  36. // We start by connecting to a WiFi network
  37. ConnectOpenWifi();
  38. }
  39. // --------------------------------------------------------------------------------
  40. void loop() {
  41.  
  42. char bssid[6];
  43. DynamicJsonBuffer jsonBuffer;
  44. Serial.println("scan start");
  45. // WiFi.scanNetworks will return the number of networks found
  46. int n = WiFi.scanNetworks();
  47. Serial.println("scan done");
  48. if (n == 0)
  49. Serial.println("no networks found");
  50. else
  51. {
  52. Serial.print(n);
  53. Serial.println(" networks found...");
  54.  
  55. if (more_text) {
  56. // Print out the formatted json...
  57. Serial.println("{");
  58. Serial.println("\"homeMobileCountryCode\": 234,"); // this is a real UK MCC
  59. Serial.println("\"homeMobileNetworkCode\": 27,"); // and a real UK MNC
  60. Serial.println("\"radioType\": \"gsm\","); // for gsm
  61. Serial.println("\"carrier\": \"Vodafone\","); // associated with Vodafone
  62. //Serial.println("\"cellTowers\": ["); // I'm not reporting any cell towers
  63. //Serial.println("],");
  64. Serial.println("\"wifiAccessPoints\": [");
  65. for (int i = 0; i < n; ++i)
  66. {
  67. Serial.println("{");
  68. Serial.print("\"macAddress\" : \"");
  69. Serial.print(WiFi.BSSIDstr(i));
  70. Serial.println("\",");
  71. Serial.print("\"signalStrength\": ");
  72. Serial.println(WiFi.RSSI(i));
  73. if (i < n - 1)
  74. {
  75. Serial.println("},");
  76. }
  77. else
  78. {
  79. Serial.println("}");
  80. }
  81. }
  82. Serial.println("]");
  83. Serial.println("}");
  84. }
  85. Serial.println(" ");
  86. }
  87. // now build the jsonString...
  88. jsonString = "{\n";
  89. jsonString += "\"homeMobileCountryCode\": 234,\n"; // this is a real UK MCC
  90. jsonString += "\"homeMobileNetworkCode\": 27,\n"; // and a real UK MNC
  91. jsonString += "\"radioType\": \"gsm\",\n"; // for gsm
  92. jsonString += "\"carrier\": \"Vodafone\",\n"; // associated with Vodafone
  93. jsonString += "\"wifiAccessPoints\": [\n";
  94. for (int j = 0; j < n; ++j)
  95. {
  96. jsonString += "{\n";
  97. jsonString += "\"macAddress\" : \"";
  98. jsonString += (WiFi.BSSIDstr(j));
  99. jsonString += "\",\n";
  100. jsonString += "\"signalStrength\": ";
  101. jsonString += WiFi.RSSI(j);
  102. jsonString += "\n";
  103. if (j < n - 1)
  104. {
  105. jsonString += "},\n";
  106. }
  107. else
  108. {
  109. jsonString += "}\n";
  110. }
  111. }
  112. jsonString += ("]\n");
  113. jsonString += ("}\n");
  114. //--------------------------------------------------------------------
  115.  
  116. Serial.println("");
  117.  
  118. WiFiClientSecure client;
  119.  
  120. //Connect to the client and make the api call
  121. Serial.print("Requesting URL: ");
  122.  
  123. // ---- Get Google Maps Api Key here, Link: https://developers.google.com/maps/documentation/geolocation/intro
  124.  
  125. Serial.println("https://" + (String)Host + thisPage + key);
  126.  
  127. Serial.println(" ");
  128. if (client.connect(Host, 443)) {
  129. Serial.println("Connected");
  130. client.println("POST " + thisPage + key + " HTTP/1.1");
  131. client.println("Host: " + (String)Host);
  132. client.println("Connection: close");
  133. client.println("Content-Type: application/json");
  134. client.println("User-Agent: Arduino/1.0");
  135. client.print("Content-Length: ");
  136. client.println(jsonString.length());
  137. client.println();
  138. client.print(jsonString);
  139. delay(500);
  140. }
  141.  
  142. //Read and parse all the lines of the reply from server
  143. while (client.available()) {
  144. String line = client.readStringUntil('\r');
  145. if (more_text) {
  146. // Serial.print(line);
  147. }
  148. JsonObject& root = jsonBuffer.parseObject(line);
  149. if (root.success()) {
  150. latitude = root["location"]["lat"];
  151. longitude = root["location"]["lng"];
  152. accuracy = root["accuracy"];
  153. }
  154. }
  155.  
  156. Serial.println("closing connection");
  157. Serial.println();
  158. client.stop();
  159.  
  160. Serial.print("Latitude = ");
  161. Serial.println(latitude, 8);
  162. Serial.print("Longitude = ");
  163. Serial.println(longitude, 8);
  164. Serial.print("Accuracy = ");
  165. Serial.println(accuracy);
  166. Serial.println();
  167.  
  168. // START send Lat * long data to php server / MySQL -----
  169.  
  170. if (WiFi.status() == WL_CONNECTED) {
  171.  
  172. // Construct (local) php server URL to call with data to send ----
  173. String URLtoCall = String("http://192.168.2.27/index.php?latitude=" + String(latitude, 8) + "&longitude=" + String(longitude, 8));
  174. Serial.println(URLtoCall);
  175.  
  176. HTTPClient http;
  177. http.begin(URLtoCall);
  178. int httpCode = http.GET();
  179.  
  180. if (httpCode > 0) {
  181. String payload = http.getString();
  182. Serial.println(payload);
  183. }
  184. http.end();
  185. }
  186. // END send Lat * long data to php server / MySQL -----
  187.  
  188. Serial.println("Waiting 30 Seconds before next Scan...");
  189. Serial.println();
  190.  
  191. for (int i = 0; i < 30; ++i) {
  192. Serial.println(i);
  193. delay(1000);
  194. }
  195. }
  196.  
  197. // --------------------------------------------------------------------------------------------------------
  198.  
  199. void scanAndSort() {
  200. memset(ssid, 0, MAX_SSID_LEN);
  201. int n = WiFi.scanNetworks();
  202. Serial.println("Scan done!");
  203. if (n == 0) {
  204. Serial.println("No networks found!");
  205. } else {
  206. Serial.print(n);
  207. Serial.println(" networks found.");
  208. int indices[n];
  209. for (int i = 0; i < n; i++) {
  210. indices[i] = i;
  211. }
  212. for (int i = 0; i < n; i++) {
  213. for (int j = i + 1; j < n; j++) {
  214. if (WiFi.RSSI(indices[j]) > WiFi.RSSI(indices[i])) {
  215. std::swap(indices[i], indices[j]);
  216. }
  217. }
  218. }
  219. for (int i = 0; i < n; ++i) {
  220. Serial.print(WiFi.SSID(indices[i]));
  221. Serial.print(" ");
  222. Serial.print(WiFi.RSSI(indices[i]));
  223. Serial.print(" ");
  224. Serial.print(WiFi.encryptionType(indices[i]));
  225. Serial.println();
  226. if (WiFi.encryptionType(indices[i]) == ENC_TYPE_NONE) {
  227. Serial.println("Found non-encrypted network. Store it and exit to connect.");
  228. memset(ssid, 0, MAX_SSID_LEN);
  229. strncpy(ssid, WiFi.SSID(indices[i]).c_str(), MAX_SSID_LEN);
  230. break;
  231. }
  232. }
  233. }
  234. }
  235.  
  236. //-----------------------------------------------------------------------
  237.  
  238. void ConnectOpenWifi() {
  239. Serial.println("Started.");
  240. Serial.println();
  241. if (WiFi.status() != WL_CONNECTED) {
  242.  
  243. WiFi.softAPdisconnect();
  244. WiFi.disconnect();
  245. WiFi.mode(WIFI_STA);
  246. delay(WIFI_DELAY);
  247.  
  248. scanAndSort();
  249. delay(WIFI_DELAY);
  250.  
  251. if (strlen(ssid) > 0) {
  252. Serial.print("Going to connect for : ");
  253. Serial.println(ssid);
  254.  
  255. WiFi.begin(ssid);
  256. unsigned short try_cnt = 0;
  257.  
  258. while (WiFi.status() != WL_CONNECTED && try_cnt < MAX_CONNECT_TIME / WIFI_DELAY) {
  259. delay(WIFI_DELAY);
  260. Serial.print(".");
  261. try_cnt++;
  262. }
  263. if (WiFi.status() == WL_CONNECTED) {
  264.  
  265. Serial.println("");
  266. Serial.println("WiFi connected");
  267. Serial.println("IP address: ");
  268. Serial.println(WiFi.localIP());
  269.  
  270.  
  271. } else {
  272. Serial.println("Cannot established connection on given time.");
  273. }
  274. } else {
  275. Serial.println("No non-encrypted WiFi found.");
  276. }
  277. }
  278.  
  279. Serial.println("End Loop break...");
  280. delay(10000);
  281. }
  282. // -------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement