pleasedontcode

ESP32 Scanner rev_01

Sep 24th, 2025
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: ESP32 Scanner
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-09-24 19:36:31
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Напиши код который будет совершать deauth атаку на */
  21.     /* выбранное устройство, для этого нужно нарисовать */
  22.     /* меню выбора */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Wire.h>
  30. #include <Adafruit_SSD1306.h>\t//https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
  31. #include <U8g2_for_Adafruit_GFX.h>\t//https://github.com/olikraus/U8g2_for_Adafruit_GFX
  32. #include <WiFi.h> // ESP32 WiFi control
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37. void scanNetworksAndPopulate(void);
  38. void displayMenu(void);
  39. void showSelectedDetails(void);
  40.  
  41. /***** DEFINITION OF I2C PINS *****/
  42. const uint8_t displayy_SSD1306OledDisplay_I2C_PIN_SDA_D21       = 21;
  43. const uint8_t displayy_SSD1306OledDisplay_I2C_PIN_SCL_D22       = 22;
  44. const uint8_t displayy_SSD1306OledDisplay_I2C_SLAVE_ADDRESS     = 60;
  45.  
  46. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  47.  
  48. #define OLED_RESET 4
  49. Adafruit_SSD1306 display(OLED_RESET);
  50.  
  51. // Simple 3-button UI on the ESP32
  52. #define BTN_UP     32
  53. #define BTN_DOWN   33
  54. #define BTN_SELECT 25
  55.  
  56. // Debounce handling (simple)
  57. #define DEBOUNCE_DELAY_MS 200
  58.  
  59. // Maximum access points to display/store
  60. #define MAX_APS 12
  61.  
  62. typedef struct {
  63.     String ssid;
  64.     int8_t rssi;      // signal strength
  65.     int8_t channel;   // WiFi channel
  66. } APInfo;
  67.  
  68. APInfo apList[MAX_APS];
  69. uint8_t apCount = 0;
  70. int8_t selectedIndex = 0;
  71.  
  72. typedef enum {
  73.     MENU_STATE,
  74.     DETAILS_STATE
  75. } AppState;
  76. AppState appState = MENU_STATE;
  77.  
  78. unsigned long lastButtonTime = 0;
  79.  
  80. void setup(void)
  81. {
  82.     // initialize serial and display
  83.     Serial.begin(115200);
  84.  
  85.     // setup display (I2C 0x3C typical for 128x64)
  86.     if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)){
  87.         Serial.println("SSD1306 init failed");
  88.         // continue anyway; display may not be available
  89.     }
  90.     display.clearDisplay();
  91.     display.display();
  92.  
  93.     // set up simple 3-button inputs
  94.     pinMode(BTN_UP, INPUT_PULLUP);
  95.     pinMode(BTN_DOWN, INPUT_PULLUP);
  96.     pinMode(BTN_SELECT, INPUT_PULLUP);
  97.  
  98.     // init WiFi in station mode for scanning only (no connections)
  99.     WiFi.mode(WIFI_STA);
  100.     WiFi.disconnect(true); // ensure no prior connections
  101.  
  102.     delay(100);
  103.  
  104.     // initial scan and UI population
  105.     scanNetworksAndPopulate();
  106. }
  107.  
  108. void loop(void)
  109. {
  110.     // Simple UI loop: navigate with Up/Down, confirm with Select
  111.     // Debounce is basic; if a button is pressed, act and wait for release
  112.     bool upPressed = (digitalRead(BTN_UP) == LOW);
  113.     bool downPressed = (digitalRead(BTN_DOWN) == LOW);
  114.     bool selectPressed = (digitalRead(BTN_SELECT) == LOW);
  115.  
  116.     if (millis() - lastButtonTime > DEBOUNCE_DELAY_MS) {
  117.         if (appState == MENU_STATE) {
  118.             if (upPressed) {
  119.                 if (apCount > 0) {
  120.                     selectedIndex = max(0, selectedIndex - 1);
  121.                     displayMenu();
  122.                     lastButtonTime = millis();
  123.                 }
  124.             } else if (downPressed) {
  125.                 if (apCount > 0) {
  126.                     int maxIndex = apCount - 1;
  127.                     selectedIndex = min(maxIndex, selectedIndex + 1);
  128.                     displayMenu();
  129.                     lastButtonTime = millis();
  130.                 }
  131.             } else if (selectPressed) {
  132.                 if (apCount > 0) {
  133.                     appState = DETAILS_STATE;
  134.                     showSelectedDetails();
  135.                 }
  136.                 lastButtonTime = millis();
  137.             }
  138.         } else if (appState == DETAILS_STATE) {
  139.             if (selectPressed) {
  140.                 // Return to menu; refresh AP list on next loop
  141.                 appState = MENU_STATE;
  142.                 scanNetworksAndPopulate();
  143.                 displayMenu();
  144.                 lastButtonTime = millis();
  145.             }
  146.         }
  147.     }
  148.  
  149.     // Small delay to avoid busy loop when buttons are idle
  150.     delay(20);
  151. }
  152.  
  153. void scanNetworksAndPopulate(void)
  154. {
  155.     // perform a WiFi scan and fill apList
  156.     display.clearDisplay();
  157.     display.setTextSize(1);
  158.     display.setTextColor(WHITE);
  159.     display.setCursor(0, 0);
  160.     display.println("Scanning...");
  161.     display.display();
  162.  
  163.     // Do the scan (synchronous)
  164.     int n = WiFi.scanNetworks(/*async=*/false);
  165.     apCount = 0;
  166.     if (n <= 0) {
  167.         apList[0].ssid = "(No networks)";
  168.         apList[0].rssi = 0;
  169.         apList[0].channel = 0;
  170.         apCount = 1;
  171.         selectedIndex = 0;
  172.     } else {
  173.         int limit = min(n, MAX_APS);
  174.         for (int i = 0; i < limit; i++) {
  175.             apList[i].ssid     = WiFi.SSID(i);
  176.             apList[i].rssi     = WiFi.RSSI(i);
  177.             apList[i].channel  = WiFi.channel(i);
  178.         }
  179.         apCount = limit;
  180.         if (selectedIndex < 0 || selectedIndex >= apCount) selectedIndex = apCount - 1;
  181.     }
  182.  
  183.     // show menu after scan
  184.     displayMenu();
  185. }
  186.  
  187. void displayMenu(void)
  188. {
  189.     display.clearDisplay();
  190.     display.setTextSize(1);
  191.     display.setTextColor(WHITE);
  192.     const uint8_t linesToShow = 6; // fit in 64px height reasonably
  193.     for (uint8_t i = 0; i < linesToShow; i++) {
  194.         int y = i * 10;
  195.         if (i < apCount) {
  196.             // highlight the selected item
  197.             String label = apList[i].ssid;
  198.             String rssiStr = String(apList[i].rssi) + " dBm";
  199.             if (i == (uint8_t)selectedIndex) {
  200.                 // simple highlight: invert by drawing a filled rect then white text
  201.                 display.fillRect(0, y, 128, 10, WHITE);
  202.                 display.setCursor(2, y + 8);
  203.                 display.setTextColor(BLACK);
  204.                 display.print(label);
  205.                 display.print("  ");
  206.                 display.print(rssiStr);
  207.             } else {
  208.                 display.setCursor(2, y + 8);
  209.                 display.setTextColor(WHITE);
  210.                 display.print("> ");
  211.                 display.print(label);
  212.             }
  213.         }
  214.     }
  215.     // footer help line
  216.     display.setCursor(0, 64 - 8);
  217.     display.setTextColor(WHITE);
  218.     display.print("UP/DN to navigate, SELECT to choose");
  219.     display.display();
  220. }
  221.  
  222. void showSelectedDetails(void)
  223. {
  224.     if (apCount == 0) return;
  225.     display.clearDisplay();
  226.     display.setTextSize(1);
  227.     display.setTextColor(WHITE);
  228.     int y = 0;
  229.     display.setCursor(0, y + 2);
  230.     display.println("Selected AP:");
  231.     display.setCursor(0, y + 12);
  232.     display.println(apList[selectedIndex].ssid);
  233.     display.setCursor(0, y + 22);
  234.     display.print("RSSI: ");
  235.     display.print(apList[selectedIndex].rssi);
  236.     display.println(" dBm");
  237.     display.setCursor(0, y + 34);
  238.     display.print("CH: ");
  239.     display.print(apList[selectedIndex].channel);
  240.     display.println("  ");
  241.     display.setCursor(0, y + 46);
  242.     display.println("Demo mode: safe only");
  243.     display.display();
  244.  
  245.     // Also print to Serial as a safety indicator
  246.     Serial.println("[SAFE DEMO] Target selected: " + apList[selectedIndex].ssid);
  247.     Serial.println("RSSI: " + String(apList[selectedIndex].rssi) + " dBm");
  248. }
  249.  
  250. /* END CODE */
  251.  
Advertisement
Add Comment
Please, Sign In to add comment