The ESP8266 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 ESP8266 and implement some best practices for a stable and reliable connection.
Required hardware
- ESP8266-Module (z.B. NodeMCU Amica)
- USB-Cable
- Computer with installed Arduino IDE or VS Code
Preparation
Make sure that you have installed the ESP8266 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> #include <ESP8266WiFi.h> #ifndef WiFi_SSID #define WiFi_SSID "WiFi Name" #define WiFi_PW "WiFi Password" #endif const char* ssid = WiFi_SSID; const char* password = WiFi_PW; // Timeout duration in milliseconds const int timeout = 10000; // 10 Seconds // put function declarations here: void connectToWiFi(); void setup() { // put your setup code here, to run once: Serial.begin(115200); connectToWiFi(); } void loop() { // put your main code here, to run repeatedly: if (WiFi.status() != WL_CONNECTED) { Serial.println("WiFi connection lost. Reconnecting..."); connectToWiFi(); } // Your main code can go here } void connectToWiFi() { Serial.print("Connecting to WiFi "); WiFi.begin(ssid, password); unsigned long startAttemptTime = millis(); while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < timeout) { delay(500); Serial.print("."); } if (WiFi.status() == WL_CONNECTED) { Serial.println(""); Serial.print("WiFi connected - local IP address: "); Serial.println(WiFi.localIP()); } else { Serial.println(""); Serial.println("Failed to connect to WiFi"); } } // put function definitions here:
Code explanation
- Integrate libraries: The code begins with the integration of the necessary libraries Arduino.h and ESP8266WiFi.h.
- 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.
- 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 ESP8266 is output.
Conclusion
Establishing a WiFi connection with the ESP8266 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 ESP8266!