The ESP32 is a low-cost microcontroller with an integrated WiFi module that is ideal for IoT projects. In this article, I will show you how to establish a WiFi connection with the ESP32 and implement a proven method for a stable and reliable connection.
Required hardware
- ESP32-Modul (z.B. ESP32_Devkit c_v4)
- USB cable
- Computer with Arduino IDE or VS Code installed
Preparation
Make sure that you have installed the ESP32 library in the Arduino IDE. You can do this via the library manager in the Arduino IDE.
I will show you how to install the Arduino IDE or, as in my case, prefer to work with Visual Studio Code and PlatformIO in other articles.
Code example
#include <Arduino.h> // this line is only necessary when using VS Studio code #include <WiFi.h> // WiFi library for the ESP32 // WiFi configuration parameters #ifndef WiFi_SSID #define WiFi_SSID "your network name" #define WiFi_PW "your password" #endif const char* ssid = WiFi_SSID; const char* password = WiFi_PW; // Time interval for checking the connection (in milliseconds) unsigned long previousMillis = 0; const long interval = 10000; // Checks the connection every 10 seconds // Function declarations go here: void connectToWiFi(); // Function for establishing the WLAN connection void checkWiFiConnection(); //// Function for checking the WiFi connection void setup() { Serial.begin(115200); // Starts serial communication with 115200 baud rate connectToWiFi(); // Connects to the WiFi } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; checkWiFiConnection(); // Checks the WLAN connection regularly } } // Function for establishing the WiFi connection void connectToWiFi() { Serial.print("Connect with WiFi: "); Serial.println(ssid); WiFi.begin(ssid, password); // Starts the connection process // Waits until the connection is established while (WiFi.status() != WL_CONNECTED) { delay(1000); // Waits 1 second Serial.print("."); } Serial.println(); Serial.println("Verbunden!"); Serial.print("IP-Adresse: "); Serial.println(WiFi.localIP()); // Outputs the assigned IP address } // Function to check the WiFi connection void checkWiFiConnection() { if (WiFi.status() != WL_CONNECTED) { Serial.println("Connection lost. Attempts to reconnect..."); connectToWiFi(); // Attempts to re-establish the connection } else { Serial.println("Stable connection."); } }
Code explanation
- Integrate libraries: The code begins with the integration of the necessary libraries Arduino.h and WiFi.h. As already mentioned in the code, it is not necessary to include the header file “Arduino.h” when using the Arduino IDE.
- WiFi login data: The SSID and password of the WiFi network are defined. In this case, this is done via preprocessor directives, which facilitates subsequent customization.
- Constants and function declarations: Constants are defined for the SSID, the password and the timeout duration. A function declaration for the connectToWiFi function is also added.
- Setup function: The serial communication is initialized in the setup() function and an attempt is made to establish a connection to the WiFi by calling the connectToWiFi() function.
- Loop function: The loop() function continuously checks whether the WiFi connection still exists. If the connection is lost, an attempt is made to re-establish it.
- connectToWiFi function: This function attempts to establish a connection to the WiFi and outputs the connection status on the serial console. If successful, the local IP address of the ESP32 is output.
- checkWiFi-Connection: This checks whether the connection has been lost and attempts to reconnect.
Conclusion
Establishing a WiFi connection with the ESP32 is relatively simple, but can be made more stable and robust by implementing some additional measures. With the code example above, you are well equipped to start your own IoT projects.
Have fun programming and experimenting with the ESP32!