Learn C++ with ESP32 : WIFI Scan Made Easy


Introduction

In this blog, we will implement the WiFi scan functionality of the ESP32 using C++ and the ESP-IDF framework. By employing object-oriented programming principles, we can create modular, reusable, and clear code. Ultimately, you will learn how to integrate C++ classes with ESP32 development and utilize the ESP32 module to scan for nearby WiFi networks.

Development Environment Setup

1. Install Visual Studio Code from the official website.
2. Install the ESP-IDF extension.

Creating a New Project

1. In VS Code, press Ctrl (Cmd) + Shift + P, then type ESP-IDF: New Project.

2. Choose a template project (such as the blink example), set the project name (for example, WIFI_Scan), and select a storage path.

3. The system will automatically generate a new ESP-IDF project for you, which includes basic CMake files and an example code.

File Structure

my_project/
├── CMakeLists.txt
├── main/
│ ├── CMakeLists.txt
│ └── main.cpp
└── ...

Writing the C++ WiFi Scan Class

We will create a C++ class named WiFiScanner to handle WiFi initialization and scanning operations. This class will utilize the ESP32 WiFi driver to initiate scans and retrieve a list of available WiFi networks, printing the scan results to the terminal via the ESP-IDF logging system (ESP_LOG).
Learn C++ with ESP32 WIFI Scan

Code Explanation

1. WiFiScanner Class:
Purpose: Encapsulates WiFi functionalities, modularizing the WiFi scanning task.
Constructor: The WiFiScanner() constructor calls the private method init_wifi() to initialize the WiFi module.
scan() Method: This public method executes WiFi scanning, configuring scan parameters, starting the scan, retrieving results, and printing WiFi network information via the logging system.
init_wifi() Method: A private method that initializes WiFi and sets it to WIFI_MODE_STA.

2. app_main():
NVS Initialization: Initializes NVS (Non-Volatile Storage) to ensure proper WiFi functionality. If NVS is corrupted or not formatted, it will be erased and reinitialized.
Event Loop: The esp_event_loop_create_default() function initializes the ESP-IDF event loop to handle system and WiFi-related events.
WiFiScanner Object: An instance of the WiFiScanner class is created in app_main(), and its scan() method is called to initiate WiFi scanning.

3. ESP-IDF Logging System (ESP_LOG):
ESP_LOGI(TAG, ...): Prints informational log messages, such as the number of discovered WiFi networks and their details.
ESP_LOGE(TAG, ...): Prints error log messages, for example, when the WiFi scan fails. This logging system is designed for embedded systems, providing flexible log level adjustments that aid in debugging and system tracing.統追蹤。

Compilation and Flashing

In the VSCode window, find the "ESP32-IDF : Build, Flash and Monitor" icon to execute and flash the program.

Running the Program

After running the program, you will see the following output in the terminal:

Conclusion

We have learned how to implement the WiFi scanning functionality of the ESP32 using C++ and the ESP-IDF framework. By encapsulating WiFi functionality within the WiFiScanner class, we have made the code more modular and easier to maintain. We utilized the ESP-IDF logging system to clearly print scan results to the terminal, which is extremely helpful for embedded development.

The combination of C++ and ESP-IDF allows us to write cleaner and more maintainable code while fully leveraging the powerful capabilities of the ESP32 microcontroller. Now, you can try to expand this project further by adding more features and continue exploring networking programming techniques in ESP32 development.