#include <WiFi.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
// If display initialization fails, enter an infinite loop
for (;;)
;
}
// Clear display, set text size and color
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
// Set WiFi mode to station mode and disconnect from any existing WiFi network
WiFi.mode(WIFI_STA);
WiFi.disconnect();
}
void loop() {
// Clear display and print scanning message
display.clearDisplay();
display.setCursor(0, 0);
display.println("Scanning WiFi...");
// Scan for nearby networks
int n = WiFi.scanNetworks();
// Check if no networks found
if (n == 0) {
display.println("No networks found.");
} else {
// Display found networks
display.setCursor(0, 0);
display.clearDisplay();
display.println("WiFi networks:");
for (int i = 0; i < n; ++i) {
// Print network details
display.print(i + 1); // Print network index
display.print(": ");
display.print(WiFi.SSID(i)); // Get SSID of the network
display.print(" (");
// Map RSSI to 0-100 scale for better readability
int signalStrength = map(WiFi.RSSI(i), -100, 0, 0, 100);
display.print(signalStrength); // Print signal strength
display.println(")");
// Limit displayed networks to 6 to fit on the OLED display
if (i >= 6) {
break;
}
}
}
// Update display
display.display();
}