// Source: https://www.hackster.io/edison0215/mini-wifi-scanner-using-oled-42a7a7
// with Modifications by MM
//#include "WiFi.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
// #include <ESP8266WebServer.h>
// Pin definitions (ESP-01 / ESP8266):
const uint8_t SDA_PIN = 0; // I2C SDA connected to GPIO0
const uint8_t SCL_PIN = 2; // I2C SCL connected to GPIO2
const uint8_t BUTTON_PIN = 3; // Button on GPIO3 (RX pin)
#elif defined(ESP32)
#include <WiFi.h>
// #include <WebServer.h>
// MM:
// Pin definitions (ESP32):
const uint8_t SDA_PIN = 21; // I2C SDA connected to GPIO0
const uint8_t SCL_PIN = 22; // I2C SCL connected to GPIO2
const uint8_t BUTTON_PIN = 16; // Button on GPIO3 (RX pin)
#endif
//Adafruit_SSD1306 display(-1);
Adafruit_SSD1306 display(128, 64, &Wire, -1);
// anschauen: https://robojax.com/learn/arduino/?vid=robojax_ESP8266_wifi_scan&srsltid=AfmBOopcXQ_1hl7aT2WaeIrP_TXjjseQpsNVfaHBbiChRkehD-V8dNtv
/*
* @brief returns the encryption type as String
* @param "id" network ID integer
* @return String text of network type
* Usage: encType(i)
* //where i is id of scanned network
* Written by Ahmad Shamshiri on August 10, 2019 at 17:33
* in Ajax, Ontario, Canada
* www.Robojax.com
*/
/*
String encType(int id){
String type;
if(WiFi.encryptionType(id) == ENC_TYPE_WEP){
type=" WEP";
}else if(WiFi.encryptionType(id) == ENC_TYPE_TKIP){
type="WPA / PSK";
}else if(WiFi.encryptionType(id) == ENC_TYPE_CCMP){
type="WPA2 / PSK";
}else if(WiFi.encryptionType(id) == ENC_TYPE_AUTO){
type="WPA / WPA2 / PSK";
}else if(WiFi.encryptionType(id) == ENC_TYPE_NONE){
type="<<OPEN>>";
}
return type;
//1: ENC_TYPE_WEP – WEP
//2 : ENC_TYPE_TKIP – WPA / PSK
//4 : ENC_TYPE_CCMP – WPA2 / PSK
//7 : ENC_TYPE_NONE – open network
//8 : ENC_TYPE_AUTO – WPA / WPA2 / PSK
}
*/
/*
* Written by Ahmad Shamshiri
* with lots of research, this sources was used:
* https://support.randomsolutions.nl/827069-Best-dBm-Values-for-Wifi
* This is approximate percentage calculation of RSSI
* Wifi Signal Strength Calculation
* Written Aug 08, 2019 at 21:45 in Ajax, Ontario, Canada
*/
/*
int dBmtoPercentage(int dBm)
{
int quality;
if(dBm <= RSSI_MIN)
{
quality = 0;
}
else if(dBm >= RSSI_MAX)
{
quality = 100;
}
else
{
quality = 2 * (dBm + 100);
}
return quality;
}//dBmtoPercentage
*/
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println(F("Booting..."));
// Initialize display
Wire.begin(SDA_PIN, SCL_PIN);
if (display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
// displayInitialized = true;
display.setRotation(2);
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.print("Booting...");
display.display();
// displayReady = true;
} else {
Serial.println(F("SSD1306 allocation failed"));
// Leave displayInitialized as false
}
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
// initialize with the I2C addr 0x3C
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// Clear the buffer.
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,12);
display.println(" WIFI SCAN");
display.display();
delay(2000);
}
void loop()
{
// WiFi.scanNetworks will return the number of networks found
//MatchState ms;
int n = WiFi.scanNetworks();
String ssid[n];
String rssi[n];
int channel[n];
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
// display.clearDisplay();
ssid[i] = WiFi.SSID(i); // String("MyOwnSSIDNetworkNo") + i; //
rssi[i] = WiFi.RSSI(i); // -10* i - i; //
channel[i] = WiFi.channel(i); // i; //
// Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
}
}
display.setTextSize(1);
display.setTextColor(WHITE);
// 8 rows per screen.. 8 pixel each...
for (int j = 0; j <= (n/8); j++) { // pages per 8 entries.
display.clearDisplay();
for (int i = j*8; i < min(n, ((j+1)*8)); i++) {
display.setCursor(0, 8*(i%8)); // 8 rows per page modulo
String ssid2 = ssid[i];
ssid2=ssid2.substring(0,14); //replace("unifi", "u"); //I customize so that it the display format is in single line
display.print(channel[i]);
display.println("" + ssid2+":" + rssi[i]);
}
display.display();
delay(5000);
}
}
// 15 -> 2 pages
// 16 =? 2 pages
// 1 -> 1 page
//17 -> 3 pagesPush during startup
for setup mode
(skipped in emulator)