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.