I have a lot of boards and usually need to re-use code even for different projects.

wifiscan.ino 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "rpcWiFi.h"
  2. #include "TFT_eSPI.h"
  3. TFT_eSPI tft;
  4. void setup()
  5. {
  6. // Set up the display
  7. tft.begin();
  8. tft.setRotation(3);
  9. tft.fillScreen(TFT_BLACK);
  10. tft.setFreeFont(&FreeSans9pt7b);
  11. tft.fillScreen(TFT_BLACK);
  12. // Intro messages
  13. tft.setTextColor(TFT_WHITE);
  14. tft.drawString("Wi-Fi Scanner", 10, 10);
  15. tft.setTextColor(TFT_RED);
  16. tft.drawString("Scanning...", 10, 30);
  17. // Set up WiFi
  18. WiFi.mode(WIFI_STA);
  19. WiFi.disconnect(); // Ensure we are not connected to an AP
  20. }
  21. void loop()
  22. {
  23. // Scan for networks and create array of seen SSIDs
  24. int number_of_networks = WiFi.scanNetworks();
  25. String seen_ssids[number_of_networks];
  26. // Reset the screen to it's title only to prepare to display APs
  27. tft.fillScreen(TFT_BLACK);
  28. tft.setTextColor(TFT_WHITE);
  29. tft.drawString("Wi-Fi Networks:", 10, 10);
  30. // Text positioning (starting at 10)
  31. int textpos = 30;
  32. int seen_ptr = 0;
  33. // Loop through networks
  34. for(int i = 0; i < number_of_networks; i++)
  35. {
  36. // Check if we already saw that SSID, we don't have much
  37. // screen space, so there's no point showing repeats
  38. bool seen = false;
  39. for(int x = 0; x < number_of_networks; x++)
  40. {
  41. if(WiFi.SSID(i) == seen_ssids[x])
  42. {
  43. seen = true;
  44. break;
  45. }
  46. }
  47. // As long as it isn't already displayed, continue
  48. if(!seen && WiFi.SSID(i)!="")
  49. {
  50. // Add a "*" to the start of the SSID if it is locked
  51. String enc_string = "* ";
  52. if(WiFi.encryptionType(i) == WIFI_AUTH_OPEN)
  53. {
  54. enc_string = "";
  55. }
  56. String text = enc_string + WiFi.SSID(i);
  57. // Determine the font color based on signal strength
  58. if(WiFi.RSSI(i) > -61){tft.setTextColor(TFT_GREEN);}
  59. else if(WiFi.RSSI(i) < -60 && WiFi.RSSI(i) > -91){tft.setTextColor(TFT_ORANGE);}
  60. else{tft.setTextColor(TFT_RED);}
  61. // Print the SSID to screen
  62. tft.drawString(text, 10, textpos);
  63. // Adjust necessary counters
  64. textpos += 20;
  65. seen_ssids[seen_ptr] = WiFi.SSID(i);
  66. seen_ptr++;
  67. }
  68. }
  69. delay(5000);
  70. }