Advertisement
learnelectronics

WeMos D1r2 WiFi Network Scanner

May 20th, 2017
2,426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. /*
  2.  * WeMos D1r2 WiFi Network Scanner
  3.  *
  4.  * learnelectronics
  5.  * 19 MAY 2017
  6.  *
  7.  * www.youtube.com/c/learnelectronics
  8.  *
  9.  * NOTE: To use the Adafruit SSD1306 OLED Driver with the D1 you MUST set
  10.  * the OLED-Reset to LED_BUILTIN
  11.  */
  12.  
  13. #include <Wire.h>                                                                   //I2C Library for the OLED
  14. #include <Adafruit_SSD1306.h>                                                       //OLED Driver
  15. #include "ESP8266WiFi.h"                                                            //ESP8266 WiFi Library
  16.  
  17.  
  18. Adafruit_SSD1306 display(LED_BUILTIN);                                              //create OLED object called display
  19.  
  20. void setup()   {                
  21.  
  22.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);                                        //start the OLED @ Hex addy 0x3C
  23.   display.display();                                                                //show the Adafruit Logo
  24.   delay(2000);                                                                      //for 2 seconds
  25.   display.clearDisplay();                                                           //clear display & buffer
  26.   WiFi.mode(WIFI_STA);                                                              //set WiFi mode
  27.   WiFi.disconnect();                                                                //WiFi disconnect
  28.   delay(100);                                                                       //short wait
  29.  
  30. }
  31.  
  32.  
  33. void loop() {
  34.  
  35.   display.clearDisplay();                                                           //clear display @ beginning of each loop
  36.   display.setTextSize(1);                                                           //set smallest text size
  37.   display.setTextColor(WHITE);                                                      //set text color to WHITE
  38.   display.setCursor(0,0);                                                           //cursor to upper left
  39.  
  40.   //display.println("scan start");
  41.  
  42. int n = WiFi.scanNetworks();                                                        //get # of networks
  43.   display.println("WiFi Scanner");                                                  //print title to buffer
  44.   display.println("---- -------");                                                  //underline title to buffer
  45.   if (n == 0)                                                                       //if no networks found
  46.     display.println("no networks found");                                           //print msg to buffer
  47.   else                                                                              //otherwise..
  48.   {
  49.     display.print(n);                                                               //print n to buffer
  50.     display.println(" networks found");                                             //print msg to buffer
  51.     for (int i = 0; i < n; ++i)                                                     //for loop for # of networks
  52.     {
  53.                                                                                     //print SSID and RSSI for each network found
  54.       display.print(i + 1);
  55.       display.print(": ");
  56.       display.print(WiFi.SSID(i));
  57.       display.print(" (");
  58.       display.print(WiFi.RSSI(i));
  59.       display.print(")");
  60.       display.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*");
  61.       delay(10);
  62.     }
  63.   }
  64.   Serial.println("");
  65.  
  66.   display.display();                                                               //show buffer
  67.   delay(5000);                                                                     //wait 5 seconds
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement