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

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

Comments

author
Deonis (author)2017-06-20

This is great! But when I connect the esp to my wifi router I can't connect to it from my computer which is on the same network. I can see IP address of the esp, I can ping it but that is it. How do I connect to it from a PC?

thanks ...

Denis

author
witnessmenow (author)Deonis2017-06-21

Hi Denis,

By connecting to it with your computer, do you mean opening a web page?

You would need to have a sketch that is running a webserver, have a look at this basic one from the ESP8266 examples

https://github.com/esp8266/Arduino/blob/master/lib...

author
Deonis (author)witnessmenow2017-06-21

Thank you Brian. I was expecting that wifimanager would be running HTTP server on esp once I connect it to my router, but it does not. I was thinking to use ESP8266 as a standalone controller for a wifi enabled light. I was thinking to control it through Python serial library similar to arduino. I am just starting with this board. I most likely doing something wrong.

This is the example I am using now

https://github.com/tzapu/WiFiManager/blob/master/examples/AutoConnect/AutoConnect.ino

thanks again,

Denis

author
witnessmenow (author)Deonis2017-06-21

Hey Denis,

Yeah WiFiManager does host a Web server, but it turns it off once it's finished, but so you would need to add a Web server inside your sketch.

If you combine the first sketch I sent on with the example from WiFiManager you linked to you should have what you are looking for. After it connects your router you will be able to go to the ip address of the esp, get a webpage and there will be a button to toggle pin 2 (I think, but change it to whatever pin you want)

author
LiLShRedDeR (author)2017-05-28

This is extremely useful. Thanks a lot @witnessmenow! Keep hacking!

author
Eradicatore (author)2017-05-05

Any plans to port this to micropython?

https://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/intro.html

author
witnessmenow (author)Eradicatore2017-05-16

It's not my library! I just really like it and wanted to share

author
stan001 (author)2017-05-07

Does it work with AT command firmware ??

author
witnessmenow (author)stan0012017-05-16

No it doesn't, its for Arduino only

author
Babar Latif (author)2017-04-24

Hi Brian,

It sin't working in my case.
I downloaded and included the WiFiManager.h library as you advised.
.
The browser gives the message "Cannot access this page" .......
.
Please help ...... Thanks.

author
microicri (author)2017-04-01

Hi witnessmenow,

thanks for instrutions, but I will need little bit more help if you can give. Would like to use wifi manager with fix IP for my project but it is not stable. I wonder if you test it for longer period or just problem lay at my unit. I have 2 pwm output and 3 digital output connected to ESP8266 and at the moment there are 5 LED's. I have sketch project without wifi manager I test for few days within several client and all was OK, but now with wifi manager it stop after about 40 minutes of working. I have installed wifi manager library 0.12.0, ESp8266 1.0.0., web server 1.0.0.. Can I send you sketch? Thanks Damir. Just to introduce myself. I am just drop to ARDUINO and found this ESP8266 intresting for some home automation projects, so far I have few hundreds projects with PIC, mostly industrial data aquisition, and safety devices.

author
Brabahoule (author)2017-03-23

Thanks a lot ! Exactly what i was looking for !

author
witnessmenow (author)Brabahoule2017-03-23

Your welcome! Yeah its a super useful library! I've added a new instructable on how you can use it for other configurations as well.

https://www.instructables.com/id/Managing-Configurations-on-Your-ESP8266-Using-WiFi/

author
Brabahoule (author)witnessmenow2017-03-24

Thanks, it seems useful too ! I gonna have a look.

author
thorathome (author)2017-03-19

This is useful info on a library I certainly didn't know about. Thanks for sharing. I've got a bunch of 8266s running around, all hard coded. Can't wait to try it out.

Much appreciated.

author
witnessmenow (author)thorathome2017-03-23

Yeah I like to use the library for any project that "goes to production" so to speak (becomes a thing in my house), unfortunately I don't really have that many at the moment but hoping to change that soon!

Thanks for your comment

author
plcabgut (author)2017-03-19

The truth is that it is surprising that there was nothing done before. It's something that worried me a lot about the comments you've said yourself, regarding changing the router or data connection wifi. Thank you very much.

author
witnessmenow (author)plcabgut2017-03-23

You are welcome! Apparently this library has been around for a while but I guess not everyone knows about it yet.

author
FernandoG41 (author)2017-03-19

Really cool!! Thank you so much for sharing!!

author
witnessmenow (author)FernandoG412017-03-23

You are more than welcome! Thanks for commenting!

author
isdubkov (author)2017-03-19

Cool! Is this library on Github written by you?

author
witnessmenow (author)isdubkov2017-03-23

No, I'm not the author of this library. Funnily enough before I found it I had a similar idea and went checking to make sure something didn't exist. Glad I did because they have done a really great job with this library!

author
b4x4 (author)2017-03-19

Now what we need, is a way to store multiple WiFi settings so we can use it in the car etc....

author
witnessmenow (author)b4x42017-03-23

Yeah that would be pretty cool! I think it would be worth opening a Github issue on the library requesting that functionality.

author
henols (author)2017-03-20

What happens if the WiFi is down and my esp starts, later the WiFi goes up again? Will the esp connect or will it just stay in config mode?

author
AlexanderP124 (author)henols2017-03-20

It will stay in configuration mode, but will rememeber the last credentials that worked until you actually connect and reconfigure it. So you can restart ESP8266 and it'll reconnect to your access point if it's up.

Personally I find it inconvenient as in case of power outage, the microcontroller restarts much faster than wifi router starts the network. This means you'll have to go and restart all your micro devices if you happen to have several of them, which is common for home automation.

To tackle this, it's better to have some timeout for configuration, such as in this example - https://github.com/tzapu/WiFiManager/blob/master/examples/AutoConnectWithTimeout/AutoConnectWithTimeout.ino

author

Thanks for answering as I wasnt sure!

author
alexpikkert (author)2017-03-21

Thanks for sharing this library, very useful !
Would something like this also exist for the MKR1000 from Arduino ?

author
witnessmenow (author)alexpikkert2017-03-23

Sorry I have never used a MKR1000 so I'm not sure.

author
KevinB479 (author)2017-03-23

Thank you witnessmenew - you've just made my 2017

author
witnessmenow (author)KevinB4792017-03-23

You are welcome! Glad I could help!

author
NiggyB (author)2017-03-23

Brilliant! Thank you!

author
witnessmenow (author)NiggyB2017-03-23

Your welcome! Thanks for the comment

author
mohir (author)2017-03-22

Thank you, this is cool,

would it work with Blynk?

author
witnessmenow (author)mohir2017-03-23

Yes! I don't use Blynk myself but if check out the github page for the library their examples have Blynk settings in them

author
oscarmoutinho (author)2017-03-20

Very useful.
Sometimes I need a static IP address.
Could it be possible to set a static IP address (and netmask and default gateway) when we are setting the WiFi Credentials ?

author
plcabgut (author)2017-03-19

La verdad es que es sorprendente que no hubiera nada hecho antes. Es algo que me preocupaba mucho por los comentarios que has Dicho tu mismo, respecto a cambiar el router o datos de la conexion wifi. Muchas gracias.

author
MichaelR12 (author)2017-03-19

Tthat looks really useful. The only problem now is I'm going to have to reprogram a dozen ESP's!

author
gada888 (author)2017-03-14

Thanks,useful

author
witnessmenow (author)gada8882017-03-17

Your welcome!

author
TheThinker (author)2017-03-15

This is great! Thx for pointing out this lib.

author
witnessmenow (author)TheThinker2017-03-17

Your welcome, glad you found it useful!

author
dapug (author)2017-03-15

I was just looking for a solution for this as recently as yesterday, and then I found this! Thanks for posting!

author
witnessmenow (author)dapug2017-03-17

Your welcome! Yeah it's really useful OK was happy to share it others cause I find it great too

author
tittiamo68 (author)2017-03-15

Thank you

Very helpful

author
witnessmenow (author)tittiamo682017-03-17

No problem, thanks for commenting

author
sumondigi (author)2017-03-17

thank u very much. I will follow u ..

author
witnessmenow (author)sumondigi2017-03-17

Your very welcome! Thanks alot

About This Instructable

31,408views

350favorites

License:

Bio: 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 ... More »
More by witnessmenow:Using Witnessmenow's ESP8266/Arduino LibrariesArduino Commute CheckerSocial Stats Counter
Add instructable to: