Components and supplies
Wemos D1 Mini
SD Card & RTC shield
Project description
Code
Geolocation Code
c_cpp
repo: https://gitlab.com/MrDIYca/a-car-trip-tracker-without-a-gps-module/-/blob/master/esp8266_geolocation.ino
1/*========================================================== 2 3 MrDIY Geolocation - Find Me ( without GPS) 4 5 Getting the API: https://developers.google.com/maps/documentation/geolocation/intro 6 7 Watch the video - https://youtu.be/nHaEYJzduZM 8 9 ========================================================== */ 10 11#include <ESP8266WiFi.h> 12#include <TimeLib.h> 13#include "cert.h" 14 15#define WIFI_NAME "ssid" 16#define WIFI_PASS "password" 17#define API_KEY "api_key" 18 19const char* host = "www.googleapis.com"; 20const String url = "/geolocation/v1/geolocate?key="; 21 22 23/* ##################### setup ################################ */ 24 25void setup() { 26 27 Serial.begin(115200); 28 Serial.println(); Serial.println(); Serial.println(); 29 Serial.println("=================================================="); 30 Serial.println(" MrDIY Geolocation"); 31 Serial.println("================================================\ 32"); 33 34 get_my_location(); 35 36} 37 38 39void loop() {} 40 41 42void get_my_location() { 43 44 45 // Searching for Wifis ------------------------------------------ 46 int n = WiFi.scanNetworks(); 47 48 Serial.print("------ Scan ("); Serial.print(n); Serial.println(")-------"); 49 50 // Listing Wifis found ------------------------------------------ 51 for (int j = 0; j < n; ++j) { 52 Serial.println( WiFi.BSSIDstr(j) ); 53 } 54 55 56 57 // Preparing Json needed for API call ----------------------------- 58 59 String jsonString = "{ \\"considerIp\\": \\"false\\", \\"wifiAccessPoints\\": ["; 60 for (int j = 0; j < n; ++j) { 61 jsonString += "{"; 62 jsonString += "\\"macAddress\\":\\""; 63 jsonString += (WiFi.BSSIDstr(j)); 64 jsonString += "\\","; 65 jsonString += "\\"signalStrength\\":"; 66 jsonString += WiFi.RSSI(j); 67 jsonString += ","; 68 jsonString += "\\"channel\\":"; 69 jsonString += WiFi.channel(j); 70 if (j < n - 1) jsonString += "},"; 71 else jsonString += "}"; 72 73 } 74 jsonString += ("]}"); 75 WiFi.scanDelete(); 76 77 // Connecting to the API to get a location ----------------------- 78 79 WiFi.begin(WIFI_NAME, WIFI_PASS); 80 81 while (WiFi.status() != WL_CONNECTED) { 82 delay(500); 83 } 84 85 Serial.println(""); 86 Serial.println("-------- CERT & TIME--------"); 87 setClock(); 88 WiFiClientSecure client; 89 BearSSL::X509List cert (GlobalSignCA); 90 client.setTrustAnchors (&cert); 91 92 Serial.println(""); 93 Serial.println("-------- API --------"); 94 Serial.println("https://" + (String) host + ":443"); 95 96 97 98 if (int s = client.connect(host, 443)) { 99 String request = String("POST ") + String(url) + API_KEY; 100 request += " HTTP/1.1\ \ 101"; 102 request += "Host: " + (String)host + "\ \ 103"; 104 request += "User-Agent: ESP8266\ \ 105"; 106 request += "Content-Type: application/json\ \ 107"; 108 request += "Content-Length: " + String(jsonString.length()) + "\ \ 109"; 110 request += "Connection: close\ \ 111\ \ 112"; 113 request += jsonString; 114 Serial.println(request); 115 client.println(request); 116 } 117 118 // Parse reply from API call ------------------------------------ 119 String response; 120 while (client.connected() && !client.available()) delay(1); 121 while (client.available()) { 122 response += client.readString(); 123 } 124 client.stop(); 125 126 Serial.println(""); 127 Serial.println("-------- Response --------+"); 128 Serial.println(response); 129 Serial.println("+-------------------------"); 130 131} 132 133 134void setClock () { 135 136 configTime (3600, 0, "pool.ntp.org", "time.nist.gov"); 137 time_t ntp_now = time (nullptr); 138 while (ntp_now < 8 * 3600 * 2) { 139 delay (100); 140 ntp_now = time (nullptr); 141 } 142 setTime(ntp_now); 143 struct tm timeinfo; 144 gmtime_r (&ntp_now, &timeinfo); 145 146}
Downloadable files
Schematic
Schematic
Schematic
Schematic
Comments
Only logged in users can leave comments
mrdiyca
0 Followers
•0 Projects
Table of contents
Intro
0
0