Wifi Analyzer |
Skrip dibawah ini berfungsi untuk scan WiFi berdekatan dan memaparkan maklumatnya pada screen OLED. Fungsi utamanya ialah:
- Scan WiFi setiap 5 saat.
- Tunjuk maklumat rangkaian seperti:
- Nama (SSID)
- Kekuatan signal (RSSI dalam dBm)
- Nombor channel (Channel)
- Jenis (WPA2, WPA, Open, dll.)
- Bar kekuatan isyarat.
Disini saya lampirkan skripnya.
-------------------------------------------------------------------------------------------------------------------
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#define OLED_MOSI 21
#define OLED_CLK 22
#define OLED_DC 15
#define OLED_CS 13
#define OLED_RESET 14
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define BUTTON_NEXT 0 // Button for scrolling networks
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
#define MAX_NETWORKS 20
int networkCount = 0;
int currentNetwork = 0;
const int scanTime = 5; // In seconds
unsigned long lastScan = 0;
struct WiFiNetwork {
String ssid;
int32_t rssi;
wifi_auth_mode_t encryptionType;
int channel;
};
WiFiNetwork networks[MAX_NETWORKS];
void setup() {
Serial.begin(115200);
pinMode(BUTTON_NEXT, INPUT_PULLUP);
// Setup OLED
if (!display.begin(SSD1306_SWITCHCAPVCC)) {
Serial.println(F("OLED initialization failed"));
while (1);
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
// Show initial message
display.setTextSize(1);
display.setCursor(0,0);
display.println("WiFi Analyzer");
display.println("Initializing...");
display.display();
// Setup WiFi
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
}
void loop() {
// Scan networks every 5 seconds
if (millis() - lastScan >= scanTime * 1000) {
scanNetworks();
lastScan = millis();
}
// Change network when button is pressed
if (digitalRead(BUTTON_NEXT) == LOW && networkCount > 0) {
currentNetwork = (currentNetwork + 1) % networkCount;
delay(200); // Debounce
displayNetworks();
}
}
void scanNetworks() {
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.println("Scanning...");
display.display();
networkCount = WiFi.scanNetworks();
if (networkCount == 0) {
display.clearDisplay();
display.setCursor(0,0);
display.println("No networks found!");
display.display();
} else {
networkCount = min(networkCount, MAX_NETWORKS);
for (int i = 0; i < networkCount; i++) {
networks[i].ssid = WiFi.SSID(i);
networks[i].rssi = WiFi.RSSI(i);
networks[i].encryptionType = WiFi.encryptionType(i);
networks[i].channel = WiFi.channel(i);
}
displayNetworks();
}
// Clear scan results
WiFi.scanDelete();
}
String getEncryptionTypeString(wifi_auth_mode_t encryptionType) {
switch (encryptionType) {
case WIFI_AUTH_OPEN:
return "Open";
case WIFI_AUTH_WEP:
return "WEP";
case WIFI_AUTH_WPA_PSK:
return "WPA-PSK";
case WIFI_AUTH_WPA2_PSK:
return "WPA2-PSK";
case WIFI_AUTH_WPA_WPA2_PSK:
return "WPA/WPA2";
case WIFI_AUTH_WPA2_ENTERPRISE:
return "WPA2-ENT";
default:
return "Unknown";
}
}
void displayNetworks() {
display.clearDisplay();
display.setTextSize(1);
// Header
display.setCursor(0,0);
display.print(networkCount);
display.print(" Networks | ");
display.print(currentNetwork + 1);
display.print("/");
display.println(networkCount);
display.drawLine(0, 9, SCREEN_WIDTH-1, 9, SSD1306_WHITE);
if (networkCount > 0) {
// Network SSID
display.setCursor(0,12);
String ssid = networks[currentNetwork].ssid;
if (ssid.length() > 20) {
ssid = ssid.substring(0, 17) + "...";
}
display.println(ssid);
// Signal strength
display.print("RSSI: ");
display.print(networks[currentNetwork].rssi);
display.println(" dBm");
// Channel
display.print("CH: ");
display.println(networks[currentNetwork].channel);
// Encryption type
display.print("Security: ");
display.println(getEncryptionTypeString(networks[currentNetwork].encryptionType));
// Signal strength bar
int strength = map(networks[currentNetwork].rssi, -100, -40, 0, SCREEN_WIDTH);
display.drawRect(0, 54, SCREEN_WIDTH, 10, SSD1306_WHITE);
display.fillRect(0, 54, strength, 10, SSD1306_WHITE);
}
display.display();
}
---------------------------------------------------------------------------------------------------------------------
semoga bermanfaat, 73 9m2zak