Configure ESP8266 Wifi with WiFiManager

There’s no doubt that the ESP8266 has made creating little WiFi widgets pretty easy. However, a lot of projects hard code the access point details into the device. There’s a better way to do it: use the WiFiManager library. [Witnessmenow] has a good tutorial and a two-minute video (which you can see below).

Hard coding is fine if you are just tinkering around. However, if you are going to send your device away (or even take it with you somewhere) you probably don’t want to reprogram it every time you change access points. This problem is even worse if you plan on a commercial product. WiFiManager does what a lot of commercial devices do. It initially looks like an access point. You can connect to it using a phone or other WiFi device. Then you can configure it to join your network by setting the network ID, password, etc.

It isn’t very hard to adapt a hard-coded program to use WiFiManager and [Witnessmehow] shows a normal program and a WiFiManager sample side-by-side. He then copies and pastes a few lines of code to get it working.

Essentially, you need to make sure you have the following headers:

#include <ESP8266WiFi.h> //ESP8266 Core WiFi Library (you most likely already have this in your sketch)
#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic

You also need some setup:

WiFiManager wifiManager;
//first parameter is name of access point, second is the password
wifiManager.autoConnect("AP-NAME", "AP-PASSWORD");

You can omit the password if you don’t want one. There are a few other options you can find on the GitHub page. You can also install from there or use the Arduino library manager.

We had recently mentioned this library in our Blynk tutorial. If you are looking for some good consumer-like project to apply this to, how about a learning remote control?

17 thoughts on “Configure ESP8266 Wifi with WiFiManager

  1. That’s a brilliant idea. I just implemented it in about 3 minutes on my ESP and it does exactly what it says on the tin! So bloody simple to use… 3 includes and 2 lines that replace your wifi connect command… that’s it!

    1. Yeah the Hackaday hackers ought to start checking with you to find out what you know about before they post. After all nobody else matters right?

      Oh. Check with tv station about why they keep showing shows you don’t watch too.

    2. @Martin I assume you submitted this to Hackaday back in 2014?

      Can you include a link to your tutorial and video that you created to enlighten the rest of us? Maybe you can share all the other things you’ve done to show how much wiser you are than the rest of us?

      It took me a few weeks of searching and playing around with the ESP8266 before I stumbled on WifiManager last year. I had already started writing something similar and was glad to find something already done.

      I personally really appreciate that people take the time to write articles, summarize and share information that may save others time even when it is “old” news to those who have been doing this for a while.

    1. As far as user data it only sends/receives when you tell it to… I believe the WiFi standard has some sort of keep alive to tell the base station it’s still there, you could check 802.11 the RFC for that info…

  2. I’ve used this library as well. It is handy. I ended up assigning a pin to a button so that I could request to enter new wifi creds. Otherwise the esp would get confused due to a power blip and get stuck waiting for creds -or- I’d have to reprogram it to enter new creds.

  3. It looks very convenient, but can a hacker insert a man in the middle attack by jamming the local WiFi, connecting to the ESP then telling it to use a WiFi bridge device which then facilitates attacks against whatever the ESP’s function is?

    1. The hacker would have to know the password used in the WAP’s autoConnect setup.

      wifiManager.autoConnect(“AP-NAME”, “AP-PASSWORD”);

      But you can optionally omit that password if you want to be part-of-the-problem.

  4. This has me thinking since I have had that headache of setting an ESP device to a specific hot spot and then moving it and it breaks.
    I am using EspEasy firmware on ESP devices and controlling them using Node Red.
    I am using a Mosquitto MQTT broker in between to allow them to talk to each other.
    So I am thinking I may be able to publish a particular topic to the mqtt something like:
    /DeviceName/ChangeIP/123.456.789
    On the ESPEasy device you can set rules so you could (I think) subscribe to that topic and when it comes in, it would set the property for IP Address (EspEasy has many settable properties) then force it to reboot
    Gonna give it a try
    Damn… Just talked myself out of it- I realized if I move the device to a new location the ip address will be different so it will never get the mqtt message.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s