Created
July 9, 2019 12:54
-
-
Save espresso3389/34d6262ea2f11a229aacbc1bb697b73f 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> // or WiFi.h for ESP32 | |
#include <WiFiClientSecure.h> | |
#include <map> | |
const char* const GOOGLE_APIKEY = "gggggggggggggggggggggggggggggg"; | |
const char* const SLACK_TOKEN = "ssssssssssssssssssssssssssssssssssss"; | |
void setup() | |
{ | |
pinMode(14,OUTPUT); | |
digitalWrite(14,HIGH); | |
Serial.begin(115200); | |
Serial.println(); | |
WiFi.mode(WIFI_STA); | |
WiFi.disconnect(); | |
delay(100); | |
std::map<String, String> knownSsids = { | |
{ "wifiap1", "password1" }, | |
{ "wifiap2", "password2" } | |
}; | |
String ssidToConnect; | |
while (ssidToConnect.length() == 0) | |
{ | |
Serial.println("Scanning for known SSIDs..."); | |
auto n = WiFi.scanNetworks(false, false); | |
for (int i = 0; i < n; i++) | |
{ | |
auto ssid = WiFi.SSID(i); | |
auto dbm = WiFi.RSSI(i); | |
auto it = knownSsids.find(ssid); | |
if (it != knownSsids.end() && dbm > -73) | |
{ | |
ssidToConnect = it->first; | |
break; | |
} | |
} | |
WiFi.scanDelete(); | |
delay(2000); | |
} | |
Serial.printf("Connecting to %s.", ssidToConnect.c_str()); | |
WiFi.begin(ssidToConnect.c_str(), knownSsids[ssidToConnect].c_str()); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(1000); | |
Serial.print("."); | |
} | |
Serial.printf("Wi-Fi Connected.\nIP address: %s\n", WiFi.localIP().toString().c_str()); | |
} | |
bool getDouble(const String& json, const String& name, double& val) | |
{ | |
auto idx = json.indexOf("\"" + name + "\":"); | |
if (idx < 0) | |
return false; | |
val = atof(json.c_str() + idx + 3 + name.length()); | |
return true; | |
} | |
bool getCurrentGeoLocation(double& lat, double& lng, double& accuracy) | |
{ | |
WiFiClientSecure client; | |
Serial.printf("Scanning Wi-Fi APs around me...\n"); | |
String json = "{\"wifiAccessPoints\": [\n"; | |
auto n = WiFi.scanNetworks(false, false); | |
for (int i = 0; i < n; i++) | |
{ | |
Serial.printf(" %s (%s)\n", WiFi.SSID(i).c_str(), WiFi.BSSIDstr(i).c_str()); | |
char buf[256]; | |
sprintf(buf, " { \"macAddress\": \"%s\", \"signalStrength\": %d }%s", | |
WiFi.BSSIDstr(i).c_str(), | |
WiFi.RSSI(i), | |
i + 1 < n ? ",\n" : "\n]}\n"); | |
json += buf; | |
} | |
WiFi.scanDelete(); | |
Serial.printf("Estimating current location...\n"); | |
if (!client.connect("www.googleapis.com", 443)) | |
{ | |
Serial.println("HTTPS Connection to www.googleapis.com failed."); | |
return false; | |
} | |
client.printf( | |
"POST /geolocation/v1/geolocate?key=%s HTTP/1.1\r\n" | |
"Host: www.googleapis.com\r\n" | |
"Connection: close\r\n" | |
"Content-Length: %u\r\n" | |
"Content-Type: application/json\r\n\r\n", GOOGLE_APIKEY, json.length()); | |
client.print(json); | |
client.find("\r\n\r\n"); // skip header | |
json = client.readString(); | |
Serial.println(json); | |
if (!getDouble(json, "lat", lat) || | |
!getDouble(json, "lng", lng) || | |
!getDouble(json, "accuracy", accuracy)) | |
{ | |
Serial.println("\n\nInvalid response."); | |
return false; | |
} | |
Serial.printf("\n\nLocation: https://maps.google.com/maps/?q=%lf,%lf\n", lat, lng); | |
return true; | |
} | |
bool reportToSlackChannel(double lat, double lng, double accuracy) | |
{ | |
WiFiClientSecure client; | |
Serial.printf("Saying hello to slack...\n"); | |
if (!client.connect("hooks.slack.com", 443)) | |
{ | |
Serial.println("HTTPS Connection to hooks.slack.com failed."); | |
return false; | |
} | |
char body[256]; | |
sprintf(body, "payload={\"text\": \"Hello, I'm <https://maps.google.com/maps/?q=%lf,%lf|here>.\"}", lat, lng); | |
Serial.println(body); | |
client.printf( | |
"POST /services/%s HTTP/1.1\r\n" | |
"Host: hooks.slack.com\r\n" | |
"Connection: close\r\n" | |
"Content-Length: %u\r\n" | |
"Content-Type: application/x-www-form-urlencoded\r\n\r\n", SLACK_TOKEN, strlen(body)); | |
client.print(body); | |
Serial.println(client.readString()); | |
return true; | |
} | |
void loop() | |
{ | |
double lat, lng, accuracy; | |
if (!getCurrentGeoLocation(lat, lng, accuracy)) | |
{ | |
delay(1000 * 60 * 2); | |
return; | |
} | |
reportToSlackChannel(lat, lng, accuracy); | |
delay(1000 * 60 * 10); // 10 minutes. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment