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
1 Person Made This Project!
- cjrodriguez made it!
56 Comments
Question 3 years ago on Step 3
@witnessmenow I have a doubt on this?..I tried the above example and was successful to configure my home WiFi credentials but I am also using another ESP device which communicates with this device(i.e, the ESP device which was used to configure the credentials). The moment I configure the credentials my another ESP device will not be connected to this SoftAP created by WiFi Manager Library. Also in my WiFi network list, my SoftAP ssid will not be present. Even though the default IP is 192.168.4.1 and the same IP I have mentioned in my another ESP based device which actually needs to communicate but unfortunately it is not. Please suggest me what changes I have to do so that 2 cases i.e, One ESP device communicates with router and the same ESP device should also communicate with my another ESP device for any sensor data available from it.Thanks in advance
4 years ago
I have an Arduino UNO 3 and an ESP8266 connected in the following manner.
TX to TX RX to RX and power to Power and ch_en and ground to ground. Just for quick trial I created a blank Sketch in Arduino IDE and added the above code included WiFiManager and then
WiFiManager wifiManager;
wifiManager.autoConnect("AutoConnectAP");
I have seen this on various sites and have tried to do it but when I run the sketch I keep getting an error that the queue.h file can't be found. Where is it and how do I include it and other files that might not be found?
Question 4 years ago
Hi! I know that you have another project where you used WifiManager to set YouTube credentials with YouTube Api. Do you know if there is or exist for Adafruit credentials?
Question 4 years ago
Hi
This was a nice article.Could you tell me how you used DNS server to point a domain to the IP.
4 years ago on Step 3
instructables butchers the include commands. can you list the code on pastebin and link to it?
5 years ago
Yeah...this is fricken COOL! Relatives want me to start building WiFi connected gizmos for them so customizing for their individual environments has the potential to get a little CRAZY. This solves that problem!! Thanx Man!
5 years ago
Hi witnessmenow,
I am using wifi manger for one of my project. Thinks work well for normal cases. After saving all credentials, on esp8266 boot up if device not able to connect to router. it will start wifimanager once network comes back i will comes out of wifimanager look but webserver runs. now i need to stop wifimanager webserver. any way????
Reply 5 years ago
Hello colleagues, I have done some tests with wifimanager adding some things
saludos
5 years ago
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
Reply 5 years ago
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...
Reply 5 years ago
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
Reply 5 years ago
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)
5 years ago
This is extremely useful. Thanks a lot @witnessmenow! Keep hacking!
5 years ago
Any plans to port this to micropython?
https://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/intro.html
Reply 5 years ago
It's not my library! I just really like it and wanted to share
5 years ago
Does it work with AT command firmware ??
Reply 5 years ago
No it doesn't, its for Arduino only
5 years ago
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.
5 years ago
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.
6 years ago
Thanks a lot ! Exactly what i was looking for !