WiFiManager with ES...
 
Notifications
Clear all

WiFiManager with ESP32 - Stop Hard-coding WiFi Credentials!

17 Posts
8 Users
8 Likes
4,163 Views
(@dronebot-workshop)
Workshop Guru Admin
Joined: 5 years ago
Posts: 1042
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)
Member
Joined: 3 years ago
Posts: 141
 

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)
No Title
Joined: 5 years ago
Posts: 1111
 
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)
Member
Joined: 3 years ago
Posts: 141
 

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 reacted
ReplyQuote
(@jfabernathy)
Member
Joined: 3 years ago
Posts: 141
 

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 reacted
ReplyQuote
 rret
(@rret)
Member
Joined: 2 years ago
Posts: 1
 

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 2 years ago by rret

   
ReplyQuote
(@jfabernathy)
Member
Joined: 3 years ago
Posts: 141
 
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)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6495
 

@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.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@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
(@ionmaker)
Member
Joined: 2 years ago
Posts: 2
 

Has anyone had any issues with connection consistency? I have been using an ESP8266 for about a year with this provisioning code and I often have trouble withe the esp to connect. It either it doesn't connect at all, or opens the WiFI config screen, then drops out in the middle of typing in parameters. Even when I am able to enter the parameters in and it saves correctly, it only connects about 70% of the time after that. I thought it might be SPIFFs so I recently converted to LIttleFS and have same issue. I don't have a timeout, so that's not it. I am ready to buy another router in case that 's the issue. Thanks.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6495
 

@ionmaker I have had lot's of difficulty with WiFi connectivity. It was all a little mysterious until I noticed that my wife's WiFi enabled planter that was so bad the company sent her a second one for free connected easily right after a power failure. The bottom line is that the router by design 'remembers' connection data and if you have enough WiFi around you like I do it gets confused. Now when it happens I just unplug it from power for a few (up t0 10mins) minutes and it usually works. I use a lan scanner on my phone and on my Mac and can see extra addresses, and missing addresses. Maybe you are encountering the same issue.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@ionmaker)
Member
Joined: 2 years ago
Posts: 2
 

@zander Your reply makes a lot of sense. When I can't connect at all, I have reset power to the router, and the connections started working again. Your suggestion of a router memory could indeed be the culprit, but how can I get around that? Perhaps there is a way to specify a WiFi channel in the Arduino sketch that has little traffic. Just spit balling here since I don't know enough about WiFi 🙂 Thanks!


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6495
 

@ionmaker Other than cycling the power to the router, you can do nothing. Channel has nothing to do with it.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@randoid)
Member
Joined: 3 months ago
Posts: 4
 

Has anyone used this library to set static IP address or know of one that easily will?

Thanks.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6495
 

@randoid Any of them might, you just need to see if that particular WiFi API has been exposed in the Library API. The 'standard call is WiFi.config(). For newcomers, we often recommend the NERDS, here is a link to their treatment of this subject LINK to the NERDS

Also, here is the Arduino documentation page on that https://www.arduino.cc/reference/en/libraries/wifi/wifi.config/

It is even more powerful if you use symbolic addresses, that way you can change the static IP without touching the code.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Page 1 / 2