Wi-Fi Connection in Smart Light Project

After learning the basics of Wi-Fi connection, we may put it into practice based on ESP32-C3, and encapsulate the Wi-Fi features according to application requirements, so as to provide APIs for Wi-Fi initialisation and Wi-Fi connection initialisation.

Driver initialisation

This API specifies parameters for ESP32-C3, such as GPIO pins, fading time, breathing cycle, PWM frequency, clock source of the PWM controller, and PWM duty cycle resolution. For details, please refer to Chapter 5.

app_driver_init();

NVS initialisation

Before initialising Wi-Fi, it is necessary to initialise the NVS library as the Wi-Fi component needs to acquire and store certain parameters. The API is as follows:

nvs_flash_init();

Wi-Fi initialisation

This API handles LwIP and Wi-Fi events, and initialises Wi-Fi drivers.

wifi_initialize();

Wi-Fi connection initialisation

This API implements Wi-Fi configuration, starts the Wi-Fi driver, and waits for Wi-Fi connection to complete.

wifi_station_initialize();

📝 Source code

To code for Wi-Fi connection based on the LED dimming driver project, refer to book-esp32c3-iot-projects/device_firmware/3_wifi_connection.

You may compile and run the code on the development board. The output is as follows:

I (397) wifi station: Application driver initialization
I (397) gpio: GPIO[9]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (427) wifi station: NVS Flash initialization
I (427) wifi station: Wi-Fi initialization
I (547) wifi station: Wi-Fi Station initialization
I (727) wifi station: wifi_station_initialize finished.
I (6427) wifi station: connected to ap SSID:espressif password:espressif
[00] Hello world!