#include <WiFi.h> // WiFi functions for ESP32
#include <Wire.h> // I2C communication for LCD
#include <LiquidCrystal_I2C.h> // LCD control library
// LCD setup: I2C address 0x27, 20 columns, 4 rows
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
Serial.begin(115200); // Start Serial Monitor for debugging
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on backlight
lcd.setCursor(0, 0);
lcd.print("WiFi Scanner...");
WiFi.mode(WIFI_STA); // Set ESP32 to Station mode
WiFi.disconnect(); // Ensure it disconnects from any network
delay(100); // Allow time for settings to apply
}
void loop() {
Serial.println("Scanning WiFi networks...");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Scanning...");
int n = WiFi.scanNetworks(); // Perform WiFi scan
Serial.println("Scan complete.");
if (n == 0) {
Serial.println("No networks found.");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No networks found");
} else {
Serial.printf("%d networks found:\n", n);
lcd.clear();
int maxNetworks = (n > 4) ? 4 : n; // Limit display to 4 networks
for (int i = 0; i < maxNetworks; i++) {
String ssid = WiFi.SSID(i);
int rssi = WiFi.RSSI(i);
Serial.printf("%d: %s (%d dBm)\n", i + 1, ssid.c_str(), rssi);
// Format line for LCD: truncate long SSIDs
String line = ssid;
if (line.length() > 10)
line = line.substring(0, 10);
line += " ";
line += String(rssi) + "dBm";
lcd.setCursor(0, i); // Set line on LCD (0–3)
lcd.print(line);
}
}
delay(10000); // Wait 10 seconds before next scan
}