How to Avoid Hard Coding on ESP8266 with WifiManager Library

If your sketches have the credentials hard-coded in and if you get a new router or want to bring your device to a different Wi-Fi network you’re going to need to reprogram it. The need of reprogramming will make your sketches less shareable. You’ll probably want to replace your credentials with placeholders before you upload it anywhere and anybody using the code will have to put in their own credentials to make it work with their network.

Hard coding is fine if you are just tinkering around. However, if you are going to send your device away you probably don’t want to reprogram it every time you change access points specially when 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.

Wifi-Manager is a library for the ESP8266 by tzapu. It lets you dynamically set your wi-fi credentials. 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;

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

//first parameter is name of access point, second is the 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.

Example of a setup using Wifimanager

#include <Arduino.h>

#include <ESP8266WiFi.h>

#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h> //needed for library

#include <DNSServer.h>

#include <ESP8266WebServer.h>

#include <WiFiManager.h>   //https://github.com/tzapu/WiFiManager

#define USE_SERIAL Serial

ESP8266WiFiMulti WiFiMulti;

void setup() {

  USE_SERIAL.begin(115200);

    Serial.begin(115200);

    wiFiManager wifiManager;

    wifiManager.resetSettings();//reset saved settings

    wifiManager.setAPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0)); //set custom ip for portal. Fetches SSID and pass from EEPROM and tries to connect. If it does not connect it starts an access point with the specified name and goes into a blocking loop awaiting configuration

    wifiManager.autoConnect("AutoConnectAP");  //or use this for auto generated name ESP + ChipID

    wifiManager.autoConnect();

    Serial.println("connected"); //if you get here you have connected to the WiFi

}

sarah ali

sarah ali

Sarah is a passionate writer and blogger. As an early adopter, she enjoys trying out new social media and Internet tools along with WordPress plugins and Web apps.
sarah ali

Leave a Reply

Your email address will not be published.