Skip to content

ESP32 Establish WiFi connection

    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

    Code explanation

    1. 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.
    2. 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.
    3. 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.
    4. 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.
    5. 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.
    6. 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.
    7. 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!


    0 Kommentare
    Inline Feedbacks
    View all comments