Introduction: Avoid Hard-Coding WiFi Credentials on Your ESP8266 Using the WiFiManager Library

About: Hey everyone! My name is Brian and thanks for checking my Instructables. I'm a software developer by trade but I've recently gotten into Arduino development after discovering the esp8266 chip, a WiFi enable…

Hello everyone!

In this instructable I'm going to talk about how you can avoid hard-coding your WiFi credentials into your Arduino sketches using a really awesome library called WiFiManager. It will only take a couple of minutes to go through these steps.

Check out the video or continue reading here to find out how to add it and how it works.

Step 1: Why Is This Important?

If you have hard-coded values in your sketch, if you change your WiFi router or want to bring your device to somewhere else, you are going to need to re-program your esp8266, which isn't ideal!

It also makes your sketches less sharable, as you probably will want to replace your SSID and Password with placeholders before uploading to somewhere public like GitHub and anyone who is using your code is going to have to update the code before they can use it on their network.

Step 2: How WifiManager Works

WiFiManager is pretty configurable but a basic example of how a sketch that has the WiFiManager library installed works like this.

When the ESP8266 starts up, it will try connect to the last known WiFi network, but if it fails to connect WiFiManager will create its own network, with a name you can configure in your sketch.

You can connect either a phone or a computer to the new network that the ESP8266 creates. Once connected any requests you make on the browser will be redirected to the WiFiManager config page.

From here you can scan for WiFi networks and type in their password. When you hit save the ESP8266 will restart and connect to the entered credentials.

Step 3: Adding It to Your Sketch

Install the WiFiManager Library from the Arduino Library Manager or get it from Github (Which is worth checking out anyways as the Readme documentation is really good)

The library comes with a good few examples, the basic one being AutoConnect.ino, which is a good place to start, but basically you need to do the following

1) Once installed, you can add the following headers to your sketch:

#include
#include

#include <
WiFiManager.h>

2) Replace the WiFi connection code in your setup with the following two lines:

WiFiManager wifiManager;
wifiManager.autoConnect("AutoConnectAP");

And that's it! As I mentioned it is really simple to add this to your sketches, but it makes them much more flexible.

Hopefully you find this useful and as always if you have any questions please ask!

Thanks,

Brian