Thursday, February 23, 2017

Scanning for WiFi Networks

The next few blog posts will be based on some of the code samples included with the Arduino IDE for ESP8266. The specific examples are chosen for their particular relevance to IoT applications. The examples will NOT be exactly like the examples included in the Arduino IDE, but will be extended to demonstrate additional features, to illustrate coding techniques, etc.

The ESP8266's wifi can be in one of four modes:

# Name Description
0 WIFI_OFF The wifi is... off!
1 WIFI_STA Station mode - the ESP is working as a wifi client
2 WIFI_AP Access point mode - other wifi clients will be able to connect to the ESP
3 WIFI_AP_STA  AP + Station mode - The ESP8266 is acting both as a station and as an access point

The WiFi class is fundamental to most of these examples, so here is a quick overview of some of its properties and methods, as well as associated constants. Since this example is only about scanning wifi networks, and not connecting to them, the methods for actually connecting to an access point will be covered in a later tutorial.

WiFi.mode(modeName) - set the ESP8266's wifi mode to be one of the four values listed above

WiFi.disconnect() - disconnect from a station

WiFi.scanNetworks() - returns the number of available networks as well as some information about each

  • SSID - Service set identifier, essentially the network name
  • BSSID - MAC address of the network access point
  • RSSI - Received Signal Strength Indicator; access points with RSSIs closer to zero have stronger signals
  • isHidden
  • channel
  • encryptionType - see below code for the encryption types recognized by the ESP8266

For the wifi scanner, we will be using station mode, but again we will not be connecting to any of the access points we find. The code will list each access point, it's name, encryption type, etc., in the Serial Monitor window.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * WiFiScanDetails
 * 
 * By: Mike Klepper
 * Date: 22 Feb 2017
 * 
 * This program checks for available networks and displays details about each in the Serial Monitor. 
 * It is based upon the WiFiScan example in the Arduino IDE for the ESP8266
 * 
 */
 
#include "ESP8266WiFi.h"

void setup() 
{
  Serial.begin(115200);
  
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  
  delay(100);

  Serial.println("Setup done");
}

void loop() 
{
  Serial.println("Scan start");

  int numNetworks = WiFi.scanNetworks();
  Serial.println("Scan done");
  
  if(numNetworks == 0)
  {
    Serial.println("No networks found");
  }
  else
  {
    Serial.print(numNetworks);
    Serial.println(" networks found");
    
    for(int i = 0; i < numNetworks; i++)
    {
      Serial.print(i + 1);
      Serial.print(": ");
      Serial.println(WiFi.SSID(i));

      Serial.print("     BSSID = ");
      Serial.println(WiFi.BSSIDstr(i));

      Serial.print("     RSSI = ");
      Serial.println(WiFi.RSSI(i));

      Serial.print("     isHidden = ");
      Serial.println(WiFi.isHidden(i));

      Serial.print("     channel = ");
      Serial.println(WiFi.channel(i));

      Serial.print("     encryptionType = ");
      Serial.print(readableEncryptionType(WiFi.encryptionType(i)));
      Serial.print(" (");
      Serial.print(WiFi.encryptionType(i));
      Serial.println(")");
      
      delay(10);
      yield();
    }
  }
  
  Serial.println("");

  delay(5000);
  yield();
}

String readableEncryptionType(uint8_t encType)
{
  String encTypeAsString;
  
  switch(encType)
  {
    case ENC_TYPE_TKIP:
    {
      encTypeAsString = "WPA";
      break;
    }
    case ENC_TYPE_WEP:
    {
      encTypeAsString = "WEP";
      break;
    }
    case ENC_TYPE_CCMP:
    {
      encTypeAsString = "WPA2";
      break;
    }
    case ENC_TYPE_NONE:
    {
      encTypeAsString = "None";
      break;
    }
    case ENC_TYPE_AUTO:
    {
      encTypeAsString = "Auto";
      break;
    }
    default:
    {
      encTypeAsString = "Other";
      break;
    }
  }

  return encTypeAsString;
}

Lines Explanation
1-10 This is a multi-line comment. Arduino sketches support C/C++/Java/JavaScript's style of single-line and multi-line comments
12 This makes the WiFi class available for use in the code!
14 The setup() function is ran exactly once when the ESP8266 is powered-on or reset
16 Enables serial communication over the USB port between the board and the computer to which it is attached. The baud rate is set to 115200 here
18 Set the ESP8266 into station mode
19 Disconnect from any previous network connection
26 The loop() function is ran after the setup() function completes
30 This line accomplishes two things: it returns the number of available wifi networks, and it allows us to get properties of those networks, as we'll see below
33-37 If the number of networks found is zero, say so, otherwise...
42 ... loop over the available wifi networks
46 Print the current network's SSID
49 Print the current network's BSSID as a string
52 Print the current network's RSSI
55 Print whether the current network is hidden
58 Print the current network's channel
61 Print the current network's encryption type in a human-readable manner (see line 81 for the readableEncryptionType function)
63 Print the current network's encryption type as an integer
73-74 Wait for five seconds, yield so that background processes can be handled, then do it again!
77 This function converts the encryption type into a human-readable string
81 We do a switch-case statement to choose the right descriptive string for the corresponding encryption type
115 Return the descriptive string to whatever called this function

After flashing this code, open the Serial Monitor to see the output.

No comments:

Post a Comment