ESP8266 Wifi connection and configuration manager.
Wifi connection and configuration manager for ESP8266 and ESP32.
This library was made to ease the complication of configuring Wifi and other
settings on an ESP8266 or ESP32. It is roughly split into two parts, Wifi configuration
and REST variable configuration.
You can install through the Arduino Library Manager. The package name is
ConfigManager.
Include the library in your sketch
#include <ConfigManager.h>
Create your config
and meta
structs. These are the definitions for your settings.
struct Config {
char name[20];
bool enabled;
int8_t hour;
char password[20];
} config;
struct Metadata {
int8_t version;
} meta;
Note: These are examples of possible settings. The
config
is required, these values are arbitrary.
Initialize a global instance of the library
ConfigManager configManager;
In your setup function start the manager
configManager.setAPName("Demo");
configManager.setAPFilename("/index.html");
configManager.addParameter("name", config.name, 20);
configManager.addParameter("enabled", &config.enabled);
configManager.addParameter("hour", &config.hour);
configManager.addParameter("password", config.password, 20, set);
configManager.addParameter("version", &meta.version, get);
configManager.begin(config);
In your loop function, run the manager loop
configManager.loop();
To access your config data through device code
char* name = config.name;
int version = meta.version;
To change a value through device code
strncpy(config.name, "New Name", 8);
meta.version = 8;
configManager.save();
Upload the index.html
file found in the data
directory into the SPIFFS.
Instructions on how to do this vary based on your IDE. Below are links instructions
on the most common IDEs:
By default, ConfigManager runs in DEBUG_MODE
off. This is to allow the serial iterface to communicate as needed.
To turn on debugging, add the following line inside your setup
routine:
DEBUG_MODE = true
To use the debugger, change your Serial.print
calls to DebugPrint
. Output will then be toggled via the debugger.
Mode getMode()
Gets the current mode, ap or api.
void setAPName(const char *name)
Sets the name used for the access point.
void setAPPassword(const char *password)
Sets the password used for the access point. For WPA2-PSK network it should be at least 8 character long.
If not specified, the access point will be open for anybody to connect to.
void setAPFilename(const char *filename)
Sets the path in SPIFFS to the webpage to be served by the access point.
void setAPTimeout(const int timeout)
Sets the access point timeout, in seconds (default 0, no timeout).
Note: The timeout starts when the access point is started, but is evaluated in the loop function.
void setInitCallback(std::function<void()> callback)
Sets a function that will be called when the ConfigManager is first initialized. This can be used to
set default values on configuration parameters.
void setAPCallback(std::function<void(WebServer*)> callback)
Sets a function that will be called when the WebServer is started in AP mode allowing custom HTTP endpoints to be created.
void setAPICallback(std::function<void(WebServer*)> callback)
Sets a function that will be called when the WebServer is started in API/Settings mode allowing custom HTTP endpoints to be created.
void setWifiConfigURI(const char* uri)
Changes the URI for the Wifi Configuration Page. Defaults to “/”
void setWifiConnectRetries(const int retries)
Sets the number of Wifi connection retires. Defaults to 20.
void setWifiConnectInterval(const int interval)
Sets the interval (in milliseconds) between Wifi connection retries. Defaults to 500ms.
void setWebPort(const int port)
Sets the port that the web server listens on. Defaults to 80.
template<typename T>
void addParameter(const char *name, T *variable)
or
template<typename T>
void addParameter(const char *name, T *variable, ParameterMode mode)
Adds a parameter to the REST interface. The optional mode can be set to
set
orget
to make the parameter read or write only (defaults toboth
).
void addParameter(const char *name, char *variable, size_t size)
or
void addParameter(const char *name, char *variable, size_t size, ParameterMode mode)
Adds a character array parameter to the REST interface.The optional mode can be set to
set
orget
to make the parameter read or write only (defaults toboth
).
Sets SSID/Password to
NULL
Thebool reboot
indicates if the device should restart after clearing the values.
Sets all managed settings (not Wifi) to
NULL
. This is useful to clear all setting, but maintain Wifi.
Thebool reboot
indicates if the device should restart after clearing the values.
Sets all settings (managed and Wifi) to
NULL
. This is useful to re-initialize the memory of the device.
Thebool reboot
indicates if the device should restart after clearing the values.
template<typename T>
void begin(T &config)
Starts the configuration manager. The config parameter will be saved into
and retrieved from the EEPROM.
void save()
Saves the config passed to the begin function to the EEPROM.
void loop()
Handles any waiting REST requests.
server->on("/settings.html", HTTPMethod::HTTP_GET, [server](){
configManager.streamFile(settingsHTMLFile, mimeHTML);
});
Stream a file to the server when using custom routing endpoints.
Seeexample/save_config_demo/save_config_demo.ino
void ConfigManager::stopWebserver()
Stops the built-in webserver to allow your custom project
webserver to run without port/resource interference.
Seeexample/custom-http/custom-http.ino
Gets the HTML page that is used to set the Wifi SSID and password.
Sets the Wifi SSID and password. The form example can be found in the
data
directory.
ssid=access point&password=some password
{
"ssid": "access point",
"password": "some password"
}
Scans visible networks
Will print to Serial Monitor is DEBUG_MODE = true
Response 200 (application/json)
{
"ssid": "access point name",
"strength": *int*,
"security": *bool*
}
Gets the settings set in
addParameter
.
{
"enabled": true,
"hour": 3
}
Sets the settings set in
addParameter
.
{
"enabled": false,
"hour": 4
}
Response 400 (application/json)
Response 204 (application/json)