ScanNetworks.ino 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. WiFiEsp example: ScanNetworks
  3. This example prints the Wifi shield's MAC address, and
  4. scans for available Wifi networks using the Wifi shield.
  5. Every ten seconds, it scans again. It doesn't actually
  6. connect to any network, so no encryption scheme is specified.
  7. For more details see: http://yaab-arduino.blogspot.com/p/wifiesp.html
  8. */
  9. #include "WiFiEsp.h"
  10. // Emulate Serial1 on pins 6/7 if not present
  11. #ifndef HAVE_HWSERIAL1
  12. #include "SoftwareSerial.h"
  13. SoftwareSerial Serial1(6, 7); // RX, TX
  14. #endif
  15. void setup() {
  16. // initialize serial for debugging
  17. Serial.begin(115200);
  18. // initialize serial for ESP module
  19. Serial1.begin(9600);
  20. // initialize ESP module
  21. WiFi.init(&Serial1);
  22. // check for the presence of the shield
  23. if (WiFi.status() == WL_NO_SHIELD) {
  24. Serial.println("WiFi shield not present");
  25. // don't continue
  26. while (true);
  27. }
  28. // Print WiFi MAC address
  29. printMacAddress();
  30. }
  31. void loop()
  32. {
  33. // scan for existing networks
  34. Serial.println();
  35. Serial.println("Scanning available networks...");
  36. listNetworks();
  37. delay(10000);
  38. }
  39. void printMacAddress()
  40. {
  41. // get your MAC address
  42. byte mac[6];
  43. WiFi.macAddress(mac);
  44. // print MAC address
  45. char buf[20];
  46. sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
  47. Serial.print("MAC address: ");
  48. Serial.println(buf);
  49. }
  50. void listNetworks()
  51. {
  52. // scan for nearby networks
  53. int numSsid = WiFi.scanNetworks();
  54. if (numSsid == -1) {
  55. Serial.println("Couldn't get a wifi connection");
  56. while (true);
  57. }
  58. // print the list of networks seen
  59. Serial.print("Number of available networks:");
  60. Serial.println(numSsid);
  61. // print the network number and name for each network found
  62. for (int thisNet = 0; thisNet < numSsid; thisNet++) {
  63. Serial.print(thisNet);
  64. Serial.print(") ");
  65. Serial.print(WiFi.SSID(thisNet));
  66. Serial.print("\tSignal: ");
  67. Serial.print(WiFi.RSSI(thisNet));
  68. Serial.print(" dBm");
  69. Serial.print("\tEncryption: ");
  70. printEncryptionType(WiFi.encryptionType(thisNet));
  71. }
  72. }
  73. void printEncryptionType(int thisType) {
  74. // read the encryption type and print out the name
  75. switch (thisType) {
  76. case ENC_TYPE_WEP:
  77. Serial.print("WEP");
  78. break;
  79. case ENC_TYPE_WPA_PSK:
  80. Serial.print("WPA_PSK");
  81. break;
  82. case ENC_TYPE_WPA2_PSK:
  83. Serial.print("WPA2_PSK");
  84. break;
  85. case ENC_TYPE_WPA_WPA2_PSK:
  86. Serial.print("WPA_WPA2_PSK");
  87. break;
  88. case ENC_TYPE_NONE:
  89. Serial.print("None");
  90. break;
  91. }
  92. Serial.println();
  93. }