Nodemcu or ESP8266 WiFi Manager library allows you to use WiFi without hardcoding SSID and Password, and can help you make ESP8266 IoT device professionally. let’s start with a story.
Suppose you have built an excellent and innovative IoT device using nodemcu or ESP8266 but you have hardcoded the credentials such as SSID and password.
It will work fine if the hardcoded network is available but if for some reason you take your device to somewhere else and the hardcoded credentials are not available then your IoT device will be useless.
Then you will have to to change the SSID and password in the code that means you will have to upload the code again and remember the credentials are case-sensitive so you have to be very careful if you want to save some extra time because uploading and the changing of the code is already gonna take some time so it can make you frustrated.
Must do while making an IoT device with NodeMCU or esp8266
But wait, i have a solution for you so in this Blog,” Nodemcu WIFI without hardcoding credentials ” i am going to explain how to program an IOT device in such a way that can run anywhere.
and you will not have to worry if the network credentials are changing because you will not have to change the credentials in the code.
Config Portals
In this tutorial, we are going to program nodemcu or esp8266 so we can get nice and clean HTML config portal from which we can easily change the credentials. So, we are going to program two types of portal.
AutoConnect Config Portal
Config portal will be automatically opened whenever it will not be able to connect to any available network. The NodeMCU autoconnect config portal is the best portal if you are not willing to change the credentials again and again and all it matters for you is the internet connectivity because it will open portal whenever it is failed to connect.
On Demand Config Portal
You can open config portal any time by using push button or any other trigger option. This type of config portal is the best option if you are willing to change the credentials many times and you use your device with number of network devices.
ESP8266 WIFI Auto Connect Config Portal
Like i have said in the initial introduction, that the Auto connect config portal is opened when NodeMCU is failed to connect to any network. This is the best type of portal if you do not need to change the network unless network is not available.
Install ESP8266 WiFi Manager library
The First thing you have to do is to install wifi manager library by Tpzau. You can do that by opening Arduino and go to tools and then in library manager. Search for WifiManger by tpzau and once you found it click on install.
ESP8266 WiFi Manager Code
After you have installed the library. You will have to write the code.
We will start by importing the libraries, we will be importing these four libraries.
#include <ESP8266WiFi.h> #include <DNSServer.h> #include <ESP8266WebServer.h> #include <WiFiManager.h>
and then we will add,
Serial.begin(115200);
that will help us to connect with serial monitor.
Now we will create object of the class by typing,
WiFiManager wifiManager;
and then we will use AutoConnect command, we will have to write access point name in the parameters and password is optional,
wifiManager.autoConnect("HIGH VOLTAGES","password(optional)");
we can print anything for the verification now.
Serial.println("connected");
and you are done, Here is the complete code.
#include <ESP8266WiFi.h> #include <DNSServer.h> #include <ESP8266WebServer.h> #include <WiFiManager.h> void setup() { // put your setup code here, to run once: Serial.begin(115200); WiFiManager wifiManager; Serial.println("Conecting....."); wifiManager.autoConnect("HIGH VOLTAGES","12345"); Serial.println("connected"); } void loop() { // put your main code here, to run repeatedly: }
Save and upload code
Then you will have to save and upload the code.
Once code is uploaded, open Serial Monitor.
Connect to Access Point
Once code is uploaded , you will observe that Access point with the name you have put in the code will be opened. You will have to connect it for us, it was high voltages so we will connect to it.
Open Config Portal
Once it is connected, you will be redirected to the config portal.
Click on configure wifi and you will see available networks.
Once done click on save and it will be connected, you can verify by serial monitor.
and that is the end of part 1.
ESP8266 WiFi Manager on Demand Config Portal
This Config portal is for those who needs to change credential regularly or often. THis type of config portal will be trigger by any GPIO or push button. So we are using Push button for that.
Circuit:
Now connect push button with pin D2 in the pull up configuration.
Code:
Code is almost similar so there is no need to define each line again. Here it is,
#include <ESP8266WiFi.h> #include <ESP8266WebServer.h> #include <DNSServer.h> #include <WiFiManager.h> #define TRIGGER_PIN D2 void setup() { // put your setup code here, to run once: Serial.begin(115200); Serial.println("n Starting"); pinMode(TRIGGER_PIN, INPUT); } void loop() { // put your main code here, to run repeatedly: if ( digitalRead(TRIGGER_PIN) == LOW ) { WiFiManager wifiManager; if (!wifiManager.startConfigPortal("OnDemand High Voltages")) { Serial.println("failed to connect and hit timeout"); delay(3000); ESP.reset(); delay(5000); } Serial.println("connected...:)"); } }
How to open Config portal:
Just push the button in order to open the config portal.
Connect to Access point and Config Portal:
The next steps are same, you can connect to access point and then open config portal.
Conclusion
These lines of codes must be in every IOT device made by NodeMCU. This can help you save alot of time and it makes your device looks professional.
This is very interesting, You are a very skilled blogger.
I have joined your feed and look forward to seeking more of your fantastic post.
Also, I’ve shared your site in my social networks!
Thank you , thats very kind of you and thank you for appreciation.
its not working. it throws compilation error—– exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module).
Help me out with this. If i remove #include than it compiles successfully
how to combine it with other coding because i had a coding where i control the relay using sinric pro and i try using your method to do the ESP8266 WiFi Manager on Demand Config Portal and it connect successfully except when i open sinric pro it doesnt detect the relay i want to control please help
Brother I was able to connect to the wifi using your code. But if we want to use the name and ssid somewhere else in the code, how come we can directly replace that with new WiFi we connect to