This seems like a topic that has a lot of cover on the internet, and many sites have code snippets for connecting ESP8266 to WiFi networks, however, after using a lot of ESP8266 in my projects I want to recommend my own take on how to achieve this.

As I wrote before, I’ve used many ESP8266 microcontrollers in my home automation project: they are cheap, reliable, and are Arduino compatible, which makes them perfect for any project, even if it’s not a network centered one.

The method I used to connect those microcontrollers to WiFi was pretty much as seen on every code snippet available on the internet.
For instance, the wide speared solution is to set up WiFi.begin ( "SSID", "PASSWORD");  the void setup()  function of every sketch in Arduino.
While this code certainly works, I discovered that sometimes it can be not reliable: the connection might drop and not return, power outages can disconnect it and sometimes if you want to connect to another network, or change the password, it will fail.

So here are my best practices to connect ESP8266 to WiFi networks.

Move WiFi.Begin to void loop()

ESP8266 has a mechanism that automatically reconnects if the connection is dropped, and while this is probably a good solution for some projects, I think it’s better to control this process instead of being controlled by it.
As I mentioned in the paragraph before, this mechanism can sometimes cause problems.
The way I see things, connecting to a WiFi network is not a single task to be done only when the microcontroller boots up, but an ongoing process of making sure you are connected all time.
That’s why I think that connecting should be done in the main loop of the Arduino sketch.

In the code below, I demonstrate how I connect to WiFi networks, and over the time I’ve used it with various types of microcontroller it worked perfectly.

Use WiFi Events to detect the WiFi State

Another important factor is knowing the state of your connection in your sketch’s code. Your code should take into consideration if your chip is connected, connecting or disconnected and do actions accordingly. That way, you can control what to do if a connection is dropped: maybe try to reconnect, order your chip to sleep for a while in order to save energy or perhaps try to login to a backup WiFi network.

Adding handlers to WiFi events is done in the setup portion of your sketch like so:

This, for example, fires up the “onConnected” method when the chip is passed the password check of the connection part.
Please note: this event only detects the beginning of the association process, and at this point, the microcontroller still has no ability to send and receive data and without an IP address.
“onConnected” method is a function that you need to implement in your sketch (example below).

This, on the other hand, fires “onGotIP” method, when the chip gets its final step of the connection, getting is network IP address.

Use WiFi.disconnect()

ESP8266 sometimes remembers its last connected network, which as mentioned can cause problems, such as trying to connect to a new network, or if your changed your password.
I think it’s best practice to clear the chip’s WiFi cache, and that’s exactly what this command does.

Connect in a Non-Blocking way

Another thing that I recommend while programming a sketch is to avoid using Sleep() commands. These can hang the device and cause a lot of problems. Always try using non-blocking technics when possible.
In our case, we wait for a connection in a loop that executes only if a predetermined time has passed, and thus avoiding the sleep command.

In the example above, the code waits for a connection, and if it doesn’t achieve it in a predetermined time, it tries to reconnect.


Full code of connecting ESP8266 to WiFi – my best practice

Hope you enjoyed this tutorial post.
Let me know in the comments below your thoughts and suggestions.



Leave a comment