acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

ESPWiFiAnalyzer.ino (10937B)

      1 /*
      2  * ESP WiFi Analyzer
      3  * Require ESP8266/ESP32 board support.
      4  */
      5 
      6 // POWER SAVING SETTING
      7 #define SCAN_INTERVAL 3000
      8 // #define SCAN_COUNT_SLEEP 3
      9 // #define LCD_PWR_PIN 14
     10 
     11 /*******************************************************************************
     12  * Start of Arduino_GFX setting
     13  *
     14  * Arduino_GFX try to find the settings depends on selected board in Arduino IDE
     15  * Or you can define the display dev kit not in the board list
     16  * Defalult pin list for non display dev kit:
     17  * Arduino Nano, Micro and more: CS:  9, DC:  8, RST:  7, BL:  6, SCK: 13, MOSI: 11, MISO: 12
     18  * ESP32 various dev board     : CS:  5, DC: 27, RST: 33, BL: 22, SCK: 18, MOSI: 23, MISO: nil
     19  * ESP32-C3 various dev board  : CS:  7, DC:  2, RST:  1, BL:  3, SCK:  4, MOSI:  6, MISO: nil
     20  * ESP32-S2 various dev board  : CS: 34, DC: 38, RST: 33, BL: 21, SCK: 36, MOSI: 35, MISO: nil
     21  * ESP32-S3 various dev board  : CS: 40, DC: 41, RST: 42, BL: 48, SCK: 36, MOSI: 35, MISO: nil
     22  * ESP8266 various dev board   : CS: 15, DC:  4, RST:  2, BL:  5, SCK: 14, MOSI: 13, MISO: 12
     23  * Raspberry Pi Pico dev board : CS: 17, DC: 27, RST: 26, BL: 28, SCK: 18, MOSI: 19, MISO: 16
     24  * RTL8720 BW16 old patch core : CS: 18, DC: 17, RST:  2, BL: 23, SCK: 19, MOSI: 21, MISO: 20
     25  * RTL8720_BW16 Official core  : CS:  9, DC:  8, RST:  6, BL:  3, SCK: 10, MOSI: 12, MISO: 11
     26  * RTL8722 dev board           : CS: 18, DC: 17, RST: 22, BL: 23, SCK: 13, MOSI: 11, MISO: 12
     27  * RTL8722_mini dev board      : CS: 12, DC: 14, RST: 15, BL: 13, SCK: 11, MOSI:  9, MISO: 10
     28  * Seeeduino XIAO dev board    : CS:  3, DC:  2, RST:  1, BL:  0, SCK:  8, MOSI: 10, MISO:  9
     29  * Teensy 4.1 dev board        : CS: 39, DC: 41, RST: 40, BL: 22, SCK: 13, MOSI: 11, MISO: 12
     30  ******************************************************************************/
     31 #include <Arduino_GFX_Library.h>
     32 
     33 #define GFX_BL DF_GFX_BL // default backlight pin, you may replace DF_GFX_BL to actual backlight pin
     34 
     35 /* More dev device declaration: https://github.com/moononournation/Arduino_GFX/wiki/Dev-Device-Declaration */
     36 #if defined(DISPLAY_DEV_KIT)
     37 Arduino_GFX *gfx = create_default_Arduino_GFX();
     38 #else /* !defined(DISPLAY_DEV_KIT) */
     39 
     40 /* More data bus class: https://github.com/moononournation/Arduino_GFX/wiki/Data-Bus-Class */
     41 Arduino_DataBus *bus = create_default_Arduino_DataBus();
     42 
     43 /* More display class: https://github.com/moononournation/Arduino_GFX/wiki/Display-Class */
     44 Arduino_GFX *gfx = new Arduino_ILI9341(bus, DF_GFX_RST, 3 /* rotation */, false /* IPS */);
     45 
     46 #endif /* !defined(DISPLAY_DEV_KIT) */
     47 /*******************************************************************************
     48  * End of Arduino_GFX setting
     49  ******************************************************************************/
     50 
     51 #if defined(ESP32)
     52 #include "WiFi.h"
     53 #else
     54 #include "ESP8266WiFi.h"
     55 #define log_i(format, ...) Serial.printf(format, ##__VA_ARGS__)
     56 #endif
     57 
     58 int16_t w, h, text_size, banner_height, graph_baseline, graph_height, channel_width, signal_width;
     59 
     60 // RSSI RANGE
     61 #define RSSI_CEILING -40
     62 #define RSSI_FLOOR -100
     63 
     64 // Channel color mapping from channel 1 to 14
     65 uint16_t channel_color[] = {
     66     RED, ORANGE, YELLOW, GREEN, CYAN, BLUE, MAGENTA,
     67     RED, ORANGE, YELLOW, GREEN, CYAN, BLUE, MAGENTA};
     68 
     69 uint8_t scan_count = 0;
     70 
     71 void setup()
     72 {
     73   Serial.begin(115200);
     74   // Serial.setDebugOutput(true);
     75   // while(!Serial);
     76   Serial.println("ESP WiFi Analyzer");
     77 
     78 #ifdef GFX_EXTRA_PRE_INIT
     79   GFX_EXTRA_PRE_INIT();
     80 #endif
     81 
     82   // Set WiFi to station mode and disconnect from an AP if it was previously connected
     83   WiFi.mode(WIFI_STA);
     84   WiFi.disconnect();
     85   delay(100);
     86 
     87 #if defined(LCD_PWR_PIN)
     88   pinMode(LCD_PWR_PIN, OUTPUT);    // sets the pin as output
     89   digitalWrite(LCD_PWR_PIN, HIGH); // power on
     90 #endif
     91 
     92 #ifdef GFX_BL
     93   pinMode(GFX_BL, OUTPUT);
     94   digitalWrite(GFX_BL, HIGH);
     95 #endif
     96 
     97   // init LCD
     98   gfx->begin();
     99   w = gfx->width();
    100   h = gfx->height();
    101   text_size = (h < 200) ? 1 : 2;
    102   banner_height = text_size * 3 * 4;
    103   graph_baseline = h - 20;                            // minus 2 text lines
    104   graph_height = graph_baseline - banner_height - 30; // minus 3 text lines
    105   channel_width = w / 17;
    106   signal_width = channel_width * 2;
    107 
    108   // init banner
    109   gfx->setTextSize(text_size);
    110   gfx->fillScreen(BLACK);
    111   gfx->setTextColor(RED);
    112   gfx->setCursor(0, 0);
    113   gfx->print("ESP");
    114   gfx->setTextColor(WHITE);
    115   gfx->print(" WiFi Analyzer");
    116 }
    117 
    118 bool matchBssidPrefix(uint8_t *a, uint8_t *b)
    119 {
    120   for (uint8_t i = 0; i < 5; i++)
    121   { // only compare first 5 bytes
    122     if (a[i] != b[i])
    123     {
    124       return false;
    125     }
    126   }
    127   return true;
    128 }
    129 
    130 void loop()
    131 {
    132   uint8_t ap_count_list[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    133   int32_t noise_list[] = {RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR};
    134   int32_t peak_list[] = {RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR, RSSI_FLOOR};
    135   int16_t peak_id_list[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
    136   int32_t channel;
    137   int16_t idx;
    138   int32_t rssi;
    139   uint8_t *bssid;
    140   String ssid;
    141   uint16_t color;
    142   int16_t height, offset, text_width;
    143 
    144   // WiFi.scanNetworks will return the number of networks found
    145 #if defined(ESP32)
    146   int n = WiFi.scanNetworks(false /* async */, true /* show_hidden */, true /* passive */, 500 /* max_ms_per_chan */);
    147 #else
    148   int n = WiFi.scanNetworks(false /* async */, true /* show_hidden */);
    149 #endif
    150 
    151   // clear old graph
    152   gfx->fillRect(0, banner_height, w, h - banner_height, BLACK);
    153   gfx->setTextSize(1);
    154 
    155   if (n == 0)
    156   {
    157     gfx->setTextColor(WHITE);
    158     gfx->setCursor(0, banner_height);
    159     gfx->println("no networks found");
    160   }
    161   else
    162   {
    163     for (int i = 0; i < n; i++)
    164     {
    165       channel = WiFi.channel(i);
    166       idx = channel - 1;
    167       rssi = WiFi.RSSI(i);
    168       bssid = WiFi.BSSID(i);
    169 
    170       // channel peak stat
    171       if (peak_list[idx] < rssi)
    172       {
    173         peak_list[idx] = rssi;
    174         peak_id_list[idx] = i;
    175       }
    176 
    177       // check signal come from same AP
    178       bool duplicate_SSID = false;
    179       for (int j = 0; j < i; j++)
    180       {
    181         if ((WiFi.channel(j) == channel) && matchBssidPrefix(WiFi.BSSID(j), bssid))
    182         {
    183           duplicate_SSID = true;
    184           break;
    185         }
    186       }
    187 
    188       if (!duplicate_SSID)
    189       {
    190         ap_count_list[idx]++;
    191 
    192         // noise stat
    193         int32_t noise = rssi - RSSI_FLOOR;
    194         noise *= noise;
    195         if (channel > 4)
    196         {
    197           noise_list[idx - 4] += noise;
    198         }
    199         if (channel > 3)
    200         {
    201           noise_list[idx - 3] += noise;
    202         }
    203         if (channel > 2)
    204         {
    205           noise_list[idx - 2] += noise;
    206         }
    207         if (channel > 1)
    208         {
    209           noise_list[idx - 1] += noise;
    210         }
    211         noise_list[idx] += noise;
    212         if (channel < 14)
    213         {
    214           noise_list[idx + 1] += noise;
    215         }
    216         if (channel < 13)
    217         {
    218           noise_list[idx + 2] += noise;
    219         }
    220         if (channel < 12)
    221         {
    222           noise_list[idx + 3] += noise;
    223         }
    224         if (channel < 11)
    225         {
    226           noise_list[idx + 4] += noise;
    227         }
    228       }
    229     }
    230 
    231     // plot found WiFi info
    232     for (int i = 0; i < n; i++)
    233     {
    234       channel = WiFi.channel(i);
    235       idx = channel - 1;
    236       rssi = WiFi.RSSI(i);
    237       color = channel_color[idx];
    238       height = constrain(map(rssi, RSSI_FLOOR, RSSI_CEILING, 1, graph_height), 1, graph_height);
    239       offset = (channel + 1) * channel_width;
    240 
    241       // trim rssi with RSSI_FLOOR
    242       if (rssi < RSSI_FLOOR)
    243       {
    244         rssi = RSSI_FLOOR;
    245       }
    246 
    247       // plot chart
    248       // gfx->drawLine(offset, graph_baseline - height, offset - signal_width, graph_baseline + 1, color);
    249       // gfx->drawLine(offset, graph_baseline - height, offset + signal_width, graph_baseline + 1, color);
    250       gfx->startWrite();
    251       gfx->drawEllipseHelper(offset, graph_baseline + 1, signal_width, height, 0b0011, color);
    252       gfx->endWrite();
    253 
    254       if (i == peak_id_list[idx])
    255       {
    256         // Print SSID, signal strengh and if not encrypted
    257         String ssid = WiFi.SSID(i);
    258         if (ssid.length() == 0)
    259         {
    260           ssid = WiFi.BSSIDstr(i);
    261         }
    262         text_width = (ssid.length() + 6) * 6;
    263         if (text_width > w)
    264         {
    265           offset = 0;
    266         }
    267         else
    268         {
    269           offset -= signal_width;
    270           if ((offset + text_width) > w)
    271           {
    272             offset = w - text_width;
    273           }
    274         }
    275         gfx->setTextColor(color);
    276         gfx->setCursor(offset, graph_baseline - 10 - height);
    277         gfx->print(ssid);
    278         gfx->print('(');
    279         gfx->print(rssi);
    280         gfx->print(')');
    281 #if defined(ESP32)
    282         if (WiFi.encryptionType(i) == WIFI_AUTH_OPEN)
    283 #else
    284         if (WiFi.encryptionType(i) == ENC_TYPE_NONE)
    285 #endif
    286         {
    287           gfx->print('*');
    288         }
    289       }
    290     }
    291   }
    292 
    293   // print WiFi stat
    294   gfx->setTextColor(WHITE);
    295   gfx->setCursor(0, banner_height);
    296   gfx->print(n);
    297   gfx->print(" networks found, lesser noise channels: ");
    298   bool listed_first_channel = false;
    299   int32_t min_noise = noise_list[0];          // init with channel 1 value
    300   for (channel = 2; channel <= 11; channel++) // channels 12-14 may not available
    301   {
    302     idx = channel - 1;
    303     log_i("min_noise: %d, noise_list[%d]: %d", min_noise, idx, noise_list[idx]);
    304     if (noise_list[idx] < min_noise)
    305     {
    306       min_noise = noise_list[idx];
    307     }
    308   }
    309 
    310   for (channel = 1; channel <= 11; channel++) // channels 12-14 may not available
    311   {
    312     idx = channel - 1;
    313     // check channel with min noise
    314     if (noise_list[idx] == min_noise)
    315     {
    316       if (!listed_first_channel)
    317       {
    318         listed_first_channel = true;
    319       }
    320       else
    321       {
    322         gfx->print(", ");
    323       }
    324       gfx->print(channel);
    325     }
    326   }
    327 
    328   // draw graph base axle
    329   gfx->drawFastHLine(0, graph_baseline, 320, WHITE);
    330   for (channel = 1; channel <= 14; channel++)
    331   {
    332     idx = channel - 1;
    333     offset = (channel + 1) * channel_width;
    334     gfx->setTextColor(channel_color[idx]);
    335     gfx->setCursor(offset - ((channel < 10) ? 3 : 6), graph_baseline + 2);
    336     gfx->print(channel);
    337     if (ap_count_list[idx] > 0)
    338     {
    339       gfx->setCursor(offset - ((ap_count_list[idx] < 10) ? 9 : 12), graph_baseline + 8 + 2);
    340       gfx->print('{');
    341       gfx->print(ap_count_list[idx]);
    342       gfx->print('}');
    343     }
    344   }
    345 
    346   // Wait a bit before scanning again
    347   delay(SCAN_INTERVAL);
    348 
    349 #if defined(SCAN_COUNT_SLEEP)
    350   // POWER SAVING
    351   if (++scan_count >= SCAN_COUNT_SLEEP)
    352   {
    353 #if defined(LCD_PWR_PIN)
    354     pinMode(LCD_PWR_PIN, INPUT); // disable pin
    355 #endif
    356 
    357 #if defined(GFX_BL)
    358     pinMode(GFX_BL, INPUT); // disable pin
    359 #endif
    360 
    361 #if defined(ESP32)
    362     esp_sleep_enable_ext0_wakeup(GPIO_NUM_36, LOW);
    363     esp_deep_sleep_start();
    364 #else
    365     ESP.deepSleep(0);
    366 #endif
    367   }
    368 #endif // defined(SCAN_COUNT_SLEEP)
    369 }