I don't want to hardcode WiFi Credentials into the config.h

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
mholder96
 
Posts: 1
Joined: Thu Aug 11, 2022 7:46 pm

I don't want to hardcode WiFi Credentials into the config.h

Post by mholder96 »

Hi,

Is there a way to avoid having to hardcode the wifi ssid and password in to my Arduino code? I want to design something with an ESP32, that when in a new area, appears as an AP, then I can connect to it and enter the wifi credentials manually, and then continue on to Adafruit IO stuff. I can't find any examples of this being done successfully with Adafruit IO. Is it possible?

User avatar
things805things
 
Posts: 13
Joined: Thu Nov 25, 2021 5:09 pm

Re: I don't want to hardcode WiFi Credentials into the config.h

Post by things805things »

I use WiFiManager to handle this. Until the AdafruitIO libraries are changed to be compatible I had to make a small change as follows. These are for ESP8266 but the same changes can be made for ESP32 src files.

Basically let WiFiManager do it's auto-connect / AP thing and then once connected to WiFi connect to AdafruitIO.
Note I also used custom parameters with WiFiManager to allow the IO_USERNAME and IO_KEY to be set from the config portal so those also don't need to be hard-coded.

// To allow wifimanager to handle the network connection and prevent Adafruit IO from conflicting the
// the class defintion was changed to remove the SSID and KEY and only use the IO_USERNAME and IO_KEY
// in the .h and .cpp files for wifi/AdafruitIO_ESP8266....
//
// public:
// AdafruitIO_ESP8266(const char *user, const char *key);
// ~AdafruitIO_ESP8266();

// and the following lines were removed
// from the AdafruitIO_ESP8266.cpp files...
//
// /*
// delay(100);
// commenting this out since we are already connected via wifimanager
// WiFi.begin(_ssid, _pass);
// delay(100);
// _status = AIO_NET_DISCONNECTED;
// */
//

User avatar
things805things
 
Posts: 13
Joined: Thu Nov 25, 2021 5:09 pm

Re: I don't want to hardcode WiFi Credentials into the config.h

Post by things805things »

The latest Adafruit_IO_Arduino library (4.2.3) has two new examples related to this topic.

1) An example sketch that uses WiFiManager to handle setup of the WiFi SSID and Password. This really just requires initializing the AdafruitIO object with empty strings for SSID and Password like this..
- AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, "", "");

2) Another example that uses WiFiManager for WiFi SSID and Password as well as the AdafruitIO Username and Key.
This is useful if you want to create projects and share them with others that may use them on a different WiFi network and use a different Adafruit IO account for IOT integrations such as collecting sensor data or voice command integration via IFFT.

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”