Skip to content

Instantly share code, notes, and snippets.

@vlna
Created May 24, 2018 20:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vlna/2713196a23174b3256a09e927eddc5b5 to your computer and use it in GitHub Desktop.
Save vlna/2713196a23174b3256a09e927eddc5b5 to your computer and use it in GitHub Desktop.
//License:GPLv3
#ifdef ARDUINO_ARCH_SAMD
#include <WiFi101.h>
#elif defined ARDUINO_ARCH_ESP8266
#include <ESP8266WiFi.h>
#elif defined ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#error Wrong platform
#endif
#include <WifiLocation.h>
const char* googleApiKey = "";
WifiLocation location(googleApiKey);
#include <ESP8266HTTPClient.h>
HTTPClient http;
#include <WiFiClientSecure.h>
#include <TelegramBot.h>
// Initialize Telegram BOT
const char BotToken[] = "";
const char ChatID[] = ""; //use @my_id_bot to get this number
WiFiClientSecure net_ssl;
TelegramBot bot (BotToken, net_ssl);
char message[500];
char seznamurl[500];
void setup() {
delay(100);
Serial.begin(9600);
// Serial.setDebugOutput(true);
// Connect to WPA/WPA2 network
#ifdef ARDUINO_ARCH_ESP32
WiFi.mode(WIFI_MODE_STA);
#endif
#ifdef ARDUINO_ARCH_ESP8266
WiFi.mode(WIFI_STA);
#endif
}
void loop() {
if (connectFreeWifi()) {
location_t loc = location.getGeoFromWiFi();
Serial.println("Location request data");
Serial.println(location.getSurroundingWiFiJson());
Serial.println("Latitude: " + String(loc.lat, 7));
Serial.println("Longitude: " + String(loc.lon, 7));
Serial.println("Accuracy: " + String(loc.accuracy));
Serial.println("Reading WiFi SSID");
String at = WiFi.SSID();
Serial.println("Constructing Telegram message");
sprintf(message, "%s%f%s%f +- %d @ %s", loc.lat<0?"S":"N", loc.lat<0?-loc.lat:loc.lat, loc.lon<0?"W":"E",loc.lon<0?-loc.lon:loc.lon, loc.accuracy, at.c_str());
Serial.println("Constructing Telegram message - seznamurl");
sprintf(seznamurl, "%s%s%f%s%f", "https://mapy.cz/zakladni?q=", loc.lat<0?"S":"N", loc.lat<0?-loc.lat:loc.lat, loc.lon<0?"W":"E",loc.lon<0?-loc.lon:loc.lon);
Serial.println(message);
Serial.println(seznamurl);
bot.begin();
// bot.sendMessage(ChatID, String(message) + " @ " + at);
bot.sendMessage(ChatID, message);
bot.sendMessage(ChatID, seznamurl);
// bot.sendMessage(ChatID, googleurl);
}
delay(15000);
WiFi.disconnect();
delay(15000);
}
boolean connectFreeWifi() {
// 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");
return false;
} else {
Serial.print(n);
Serial.println(" networks found");
//find strongest
int32_t bestRSSI = -1000;
int best = -1;
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
if (WiFi.encryptionType(i) == ENC_TYPE_NONE && WiFi.RSSI(i) > bestRSSI ) {
best = i;
bestRSSI = WiFi.RSSI(i);
}
//delay(10);
}
if (best >= 0) {
Serial.print("Connecting to: ");
Serial.println(WiFi.SSID(best));
WiFi.begin(WiFi.SSID(best).c_str(), "");
int timer = 60;
while (WiFi.status() != WL_CONNECTED && timer > 0) {
delay(1000);
Serial.print(".");
timer--;
}
Serial.println();
if (WiFi.status() == WL_CONNECTED) {
return true;
} else {
return false;
}
} else {
Serial.println("No free WiFi!");
return false;
}
}
}
//test access to internet (check captive portal)
// NCSI performs a DNS lookup on www.msftncsi.com, then requests http://www.msftncsi.com/ncsi.txt. This file is a plain-text file and contains only the text Microsoft NCSI.
// NCSI sends a DNS lookup request for dns.msftncsi.com. This DNS address should resolve to 131.107.255.255. If the address does not match, then it is assumed that the internet connection is not functioning correctly.
boolean internetAccess() {
http.begin("http://www.msftncsi.com/ncsi.txt");
int httpCode = http.GET();
// file found at server
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
//check payload content here
if (payload == "Microsoft NCSI") {
return true;
} else {
return false;
}
} else {
return false;
}
}
boolean sendMessage(String location) {
// bot.begin();
// bot.sendMessage(chat_id, location);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment