How to make a simple ESP32 Wifi network scanner

In a previous article we looked at the Heltek WiFi Kit 32 – A look at an ESP32 board with a built in OLED display

The example would scan for networks and display them via the serial port, a better example would be to use the oled display on the board and display any detected networks on it

Parts List

You can pick one of these up for under $9 using the link below, a very good price for a board with these features

Name Link
ESP32 WiFi Kit ESP32 WiFi Kit CP2012 Development Board with 0.96″ OLED Display

Code

You need to install the u8g2 library, the easiest way is using the library manager

Open Arduino IDE, then Select Sketch->Include Library->Manage Libraries... Search u8g2 and install it

Enter the following into the IDE

[codesyntax lang=”cpp”]

// Heltech WiFi Kit 32 Wifi Scanner

#include "WiFi.h"
#include <U8g2lib.h>

U8G2_SSD1306_128X64_NONAME_F_HW_I2C   u8g2(U8G2_R0, 16, 15, 4);

void setup()
{
  WiFi.mode(WIFI_STA);
  // Initialize the graphics library.
  u8g2.begin();
  u8g2.setFont(u8g2_font_6x10_tf);
  u8g2.setFontRefHeightExtendedText();
  u8g2.setDrawColor(1);
  u8g2.setFontPosTop();
  u8g2.setFontDirection(0);
}

void  loop()
{ 
    int nNetworkCount = WiFi.scanNetworks();
    u8g2.clearBuffer();
    // Display networks.    
    if(nNetworkCount == 0) 
    {
        // No networks found.
        u8g2.drawStr(0, 0, "0 networks found.");
    } 
    else 
    {
        
        char    chBuffer[128];
        char    chEncryption[64];
        char    chRSSI[64];
        char    chSSID[64];
      
        sprintf(chBuffer, "%d networks found:", nNetworkCount);
        u8g2.drawStr(0, 0, chBuffer);

        for(int nNetwork = 0; nNetwork < nNetworkCount; nNetwork ++) 
        {
            // Obtain ssid for this network.
            WiFi.SSID(nNetwork).toCharArray(chSSID, 64);
            sprintf(chRSSI, "(%d)", WiFi.RSSI(nNetwork));
            sprintf(chEncryption, "%s", WiFi.encryptionType(nNetwork) == WIFI_AUTH_OPEN ? " ": "*");
            sprintf(chBuffer, "%d: %s %s %s", nNetwork + 1, chSSID, chRSSI, chEncryption);
            u8g2.drawStr(0, 8 + ((nNetwork + 1) * 8), chBuffer);
        }
    }

  u8g2.sendBuffer();
  delay(2000);
}

[/codesyntax]

Once this uploaded you should see any networks listed on the OLED

Related posts

A look at an ESP32 board with a built in OLED display

esp32 technical reference manual

A basic ESP32 Web server example