WiFiManager with ES...
 
Notifications
Clear all

WiFiManager with ESP32 - Stop Hard-coding WiFi Credentials!


DroneBot Workshop
(@dronebot-workshop)
Workshop Guru Admin
Joined: 3 years ago
Posts: 1096
Topic starter  

Stop hard-coding your WiFi connection information in your ESP32 Sketches - use WiFi Manager instead!

The ESP32 is an amazing microcontroller. It’s a powerful 32-bit processor with multiple I/O ports. 12-bit A/D converters and a variety of interfaces. But the one thing that makes it stand out above the crowd is its built-in WiFi and Bluetooth capabilities.

When we experiment with the ESP32 WiFi we generally just hard-code our WiFi login information directly in our sketches. Some people use an external file, but it is just another way to accomplish the same thing.

In both cases, our code is compiled and loaded onto our ESP32, so the ESP32 is locked into using that WiFi SSID and password. If you want to change these parameters to use your device on another WiFi network, then you need to change your code and recompile.

This isn’t a great arrangement, especially if you want to move your creation to another network, share it with a friend or even create a commercial product.

Enter WiFiManager. This great library lets you avoid hard-coding your WiFi parameters. Instead, you use a web-based interface to configure your SSID and password, so you can use your ESP32 project anywhere.

You can also add additional parameters of your own, making WiFiManager an ideal solution for both private and commercial designs.

Today I’ll show you how to use WiFiManager with the ESP32 (it can also be used with the ESP8266).

Here is the Table of Contents for today's video:

00:00 - Introduction
03:09 - WiFiManager
11:45 - WiFiManager Installation
13:29 - Using WiFiManager
18:05 - WiFiManager On-Demand
23:59 - Modifying Sketches to use WiFiManager
30:07 - Adding Custom Parameters
36:42 - JSON & SPIFFS
39:40 - Saving Parameter Data
53:04 - Conclusion

You’ll find WiFiManager is easy to use, and that you can quickly adapt your existing sketches to eliminate the hard-coded SSID and password. Give it a try and let me know what you think.

Hope you enjoy the video!

Bill

😎

"Never trust a computer you can’t throw out a window." — Steve Wozniak


Quote
jfabernathy
(@jfabernathy)
Estimable Member
Joined: 2 years ago
Posts: 148
 

This video covered exactly what I needed a year ago when I was doing my IOTstack and ESP32 Temperature and Humidity stations around the house and remote backyard buildings. In that case I hard coded the SSID/password along with station name. So I had to have a version of code for each ESP32 board and reprogram them when I wanted to move them.

I'll have to modify that code to include WiFiManager.  Thanks for the video.

Now a question. My newest remote sensor boards are Adafruit ESP32-S2 Feather boards with builtin BME280 sensors. These boards are programmed in Circuitpython. That version of my program is much simpler than the Arduino version for the Adafruit ESP32 Hazzah Feather board I use with an I2C temp/humidity sensor. However, I'd love to find a "WiFiManger" type solution for Circuitpython.

Have you heard of such a solution in Circuitpython?

If your code won't compile, have another glass of bourbon. Eventual the problem will be solved.


ReplyQuote
byron
(@byron)
Prominent Member
Joined: 3 years ago
Posts: 878
 
Posted by: @jfabernathy

Have you heard of such a solution in Circuitpython?

It works on micropython so I would think you could get it to work with circuitpython. Here is a handy link to setting it up in micropython.

https://randomnerdtutorials.com/micropython-wi-fi-manager-esp32-esp8266/


ReplyQuote
jfabernathy
(@jfabernathy)
Estimable Member
Joined: 2 years ago
Posts: 148
 

Time for another dumb question. I watched the video again and started playing with the Basic demo.  I took an ESP32 that had been running an IOT program to connect to a hard coded IP an transmit data to my IOT server.  So that program got wiped out by the demo and it ran and used it's saved credentials the very first time.  So that was puzzling to me.

Since I had not run wifimanager before how did it already know my credentials?  I un-commented the line so it would run

wm.resetSettings();

Then I could go thru the process of setting up WiFi the same as Bill did in the video.  Next I commented the wm.resetSettings(); out and this time it used the saved credentials.  

So where does it save these credentials? I know later in the video Bill takes about setting up custom data fields and saving that data to SPI NOR flash on-board, but I missed how the credentials are saved?

If your code won't compile, have another glass of bourbon. Eventual the problem will be solved.


Alevans51 liked
ReplyQuote
jfabernathy
(@jfabernathy)
Estimable Member
Joined: 2 years ago
Posts: 148
 

I've put the WiFiManager demo into my IOTStack environmental data collection system by combining the On Demand and Saved custom parameters parts of the demo.

I've noted that most of the IOT electrical plugs I have require a power switch which you press for a few extra seconds to make the plug forget it's WiFi setup.  So I used a push button connected to the ESP32 just like Bill did in the On demand section.  So my use case now is I press and hole that button while I press reset on the ESP32.  I then use my phone to connect to the "IOTsensor" AP and configure the WiFi for the sensor and also enter the IOTStack mqtt server IP address along with the location name for the sensor. 

It works great. So for each ESP sensor board I have I just add a push button and then they all run the same code.  

This was a good real world learning example.

Thanks you Bill.

 

If your code won't compile, have another glass of bourbon. Eventual the problem will be solved.


Inst-Tech and Ron liked
ReplyQuote
rret
 rret
(@rret)
New Member
Joined: 3 months ago
Posts: 2
 

looks as if wifiManager does not need ESP8266WiFi.h or WiFi.h

is that correct?

(wasn't really stated in the video or website - or I missed it.)

thanks.

This post was modified 3 months ago by rret

ReplyQuote
jfabernathy
(@jfabernathy)
Estimable Member
Joined: 2 years ago
Posts: 148
 
Posted by: @rret

looks as if wifiManager does not need ESP8266WiFi.h or WiFi.h

is that correct?

(wasn't really stated in the video or website - or I missed it.)

thanks.

The examples worked as they appeared for me in Arduino, but when I tried to run them in PlatformIO, I got the error about not finding ESP8266WiFi.  So I don't know what the answer is.

If your code won't compile, have another glass of bourbon. Eventual the problem will be solved.


ReplyQuote
Ron
 Ron
(@zander)
Noble Member
Joined: 2 years ago
Posts: 2262
 

@jfabernathy If the sketch compiles clean w/o them then they are either not needed (unlikely) or they are part of the arduino core. If you really want to know, I think you can look at the hundreds of compile time messages and search for those .h files. I am fairly sure they will show up. As to why platformIO doesn't work, my money would be on a configuration error. Do you have a path to 8266 somewhere in the config? I just tried to look, but that is one complex beast.

It is considered poor judgement to traverse a chasm in 2 leaps.


ReplyQuote
frogandtoad
(@frogandtoad)
Noble Member
Joined: 3 years ago
Posts: 1213
 

@jfabernathy

Posted by: @jfabernathy
Posted by: @rret

looks as if wifiManager does not need ESP8266WiFi.h or WiFi.h

is that correct?

(wasn't really stated in the video or website - or I missed it.)

thanks.

The examples worked as they appeared for me in Arduino, but when I tried to run them in PlatformIO, I got the error about not finding ESP8266WiFi.  So I don't know what the answer is.

Arduino does a lot of magic behind the scenes, and many times that can be a good thing, or a bad thing depending on your view and level of experience.  If you know how... always best to check the header file(s), to see what libraries they are including on your behalf.

Cheers


ReplyQuote