mirror of
https://github.com/alexhopeoconnor/WiFiManager.git
synced 2026-10-13 14:51:21 +10:00
modified custom parameters feature so user can provide their own storage mechanism
added FS json storage of parameters example
This commit is contained in:
+131
-111
@@ -1,58 +1,69 @@
|
||||
/**************************************************************
|
||||
* WiFiManager is a library for the ESP8266/Arduino platform
|
||||
* (https://github.com/esp8266/Arduino) to enable easy
|
||||
* configuration and reconfiguration of WiFi credentials and
|
||||
* store them in EEPROM.
|
||||
* inspired by:
|
||||
* http://www.esp8266.com/viewtopic.php?f=29&t=2520
|
||||
* https://github.com/chriscook8/esp-arduino-apboot
|
||||
* https://github.com/esp8266/Arduino/tree/esp8266/hardware/esp8266com/esp8266/libraries/DNSServer/examples/CaptivePortalAdvanced
|
||||
* Built by AlexT https://github.com/tzapu
|
||||
* Licensed under MIT license
|
||||
WiFiManager is a library for the ESP8266/Arduino platform
|
||||
(https://github.com/esp8266/Arduino) to enable easy
|
||||
configuration and reconfiguration of WiFi credentials and
|
||||
store them in EEPROM.
|
||||
inspired by:
|
||||
http://www.esp8266.com/viewtopic.php?f=29&t=2520
|
||||
https://github.com/chriscook8/esp-arduino-apboot
|
||||
https://github.com/esp8266/Arduino/tree/esp8266/hardware/esp8266com/esp8266/libraries/DNSServer/examples/CaptivePortalAdvanced
|
||||
Built by AlexT https://github.com/tzapu
|
||||
Licensed under MIT license
|
||||
**************************************************************/
|
||||
|
||||
#include "WiFiManager.h"
|
||||
|
||||
WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length){
|
||||
_id = id;
|
||||
_placeholder = placeholder;
|
||||
_length = length;
|
||||
_value = new char[length];
|
||||
for(int i=0; i<length; i++)
|
||||
_value[i] = 0;
|
||||
if(defaultValue!=NULL)
|
||||
strncpy(_value, defaultValue, length);
|
||||
WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length) {
|
||||
_id = id;
|
||||
_placeholder = placeholder;
|
||||
_length = length;
|
||||
_value = new char[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
_value[i] = 0;
|
||||
}
|
||||
if (defaultValue != NULL) {
|
||||
strncpy(_value, defaultValue, length);
|
||||
}
|
||||
}
|
||||
|
||||
const char* WiFiManagerParameter::getValue(){
|
||||
return _value;
|
||||
const char* WiFiManagerParameter::getValue() {
|
||||
return _value;
|
||||
}
|
||||
const char* WiFiManagerParameter::getID(){
|
||||
return _id;
|
||||
const char* WiFiManagerParameter::getID() {
|
||||
return _id;
|
||||
}
|
||||
const char* WiFiManagerParameter::getPlaceholder(){
|
||||
return _placeholder;
|
||||
const char* WiFiManagerParameter::getPlaceholder() {
|
||||
return _placeholder;
|
||||
}
|
||||
int WiFiManagerParameter::getValueLength(){
|
||||
return _length;
|
||||
int WiFiManagerParameter::getValueLength() {
|
||||
return _length;
|
||||
}
|
||||
|
||||
WiFiManager::WiFiManager() {
|
||||
for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++)
|
||||
_params[i] = NULL;
|
||||
_eepromLength = 0;
|
||||
//for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++)
|
||||
// _params[i] = NULL;
|
||||
}
|
||||
|
||||
void WiFiManager::addParameter(WiFiManagerParameter *p){
|
||||
for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++){
|
||||
if(_params[i]==NULL){
|
||||
_params[i] = p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
void WiFiManager::addParameter(WiFiManagerParameter *p) {
|
||||
/*for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++){
|
||||
if(_params[i]==NULL){
|
||||
_params[i] = p;
|
||||
_paramsCount++;
|
||||
break;
|
||||
}
|
||||
}*/
|
||||
//if(_params[_paramsCount] == NULL){
|
||||
_params[_paramsCount] = p;
|
||||
_paramsCount++;
|
||||
DEBUG_PRINT("Adding parameter");
|
||||
DEBUG_PRINT(p->getID());
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
void WiFiManager::loadParameters(){
|
||||
/*
|
||||
void WiFiManager::loadParameters() {
|
||||
DEBUG_PRINT("Load parameters - not needed");
|
||||
if(_eepromLength==0){
|
||||
// if not initialized
|
||||
for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++){
|
||||
@@ -78,30 +89,33 @@ void WiFiManager::loadParameters(){
|
||||
DEBUG_PRINT(_params[i]->getID());
|
||||
DEBUG_PRINT(_params[i]->getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
void WiFiManager::saveParameters() {
|
||||
DEBUG_PRINT("save params callback");
|
||||
/*
|
||||
if(_eepromLength==0){
|
||||
// if not initialized
|
||||
for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++){
|
||||
if(_params[i]==NULL)
|
||||
break;
|
||||
_eepromLength+= _params[i]->getValueLength();
|
||||
}
|
||||
DEBUG_PRINT(F("EEPROM initialized with length "));
|
||||
DEBUG_PRINT(_eepromLength);
|
||||
|
||||
void WiFiManager::saveParameters(){
|
||||
if(_eepromLength==0){
|
||||
// if not initialized
|
||||
for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++){
|
||||
if(_params[i]==NULL)
|
||||
break;
|
||||
_eepromLength+= _params[i]->getValueLength();
|
||||
}
|
||||
DEBUG_PRINT(F("EEPROM initialized with length "));
|
||||
DEBUG_PRINT(_eepromLength);
|
||||
|
||||
EEPROM.begin(_eepromLength);
|
||||
}
|
||||
int pos = 0;
|
||||
for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++){
|
||||
if(_params[i]==NULL)
|
||||
break;
|
||||
int l = _params[i]->getValueLength();
|
||||
for(int j=0; j<l; j++)
|
||||
EEPROM.write(pos++, _params[i]->_value[j]);
|
||||
}
|
||||
EEPROM.commit();
|
||||
EEPROM.begin(_eepromLength);
|
||||
}
|
||||
int pos = 0;
|
||||
for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++){
|
||||
if(_params[i]==NULL)
|
||||
break;
|
||||
int l = _params[i]->getValueLength();
|
||||
for(int j=0; j<l; j++)
|
||||
EEPROM.write(pos++, _params[i]->_value[j]);
|
||||
}
|
||||
EEPROM.commit();
|
||||
*/
|
||||
}
|
||||
|
||||
void WiFiManager::begin() {
|
||||
@@ -109,13 +123,13 @@ void WiFiManager::begin() {
|
||||
}
|
||||
|
||||
void WiFiManager::begin(char const *apName) {
|
||||
begin(apName,NULL);
|
||||
begin(apName, NULL);
|
||||
}
|
||||
|
||||
void WiFiManager::begin(char const *apName, char const *apPasswd) {
|
||||
dnsServer.reset(new DNSServer());
|
||||
server.reset(new ESP8266WebServer(80));
|
||||
|
||||
|
||||
DEBUG_PRINT(F(""));
|
||||
_apName = apName;
|
||||
_apPasswd = apPasswd;
|
||||
@@ -124,9 +138,9 @@ void WiFiManager::begin(char const *apName, char const *apPasswd) {
|
||||
DEBUG_PRINT(F("Configuring access point... "));
|
||||
DEBUG_PRINT(_apName);
|
||||
if (_apPasswd != NULL) {
|
||||
if(strlen(_apPasswd) < 8 || strlen(_apPasswd) > 63) {
|
||||
// fail passphrase to short or long!
|
||||
DEBUG_PRINT(F("Invalid AccessPoint password"));
|
||||
if (strlen(_apPasswd) < 8 || strlen(_apPasswd) > 63) {
|
||||
// fail passphrase to short or long!
|
||||
DEBUG_PRINT(F("Invalid AccessPoint password"));
|
||||
}
|
||||
DEBUG_PRINT(_apPasswd);
|
||||
}
|
||||
@@ -147,7 +161,7 @@ void WiFiManager::begin(char const *apName, char const *apPasswd) {
|
||||
DEBUG_PRINT(F("AP IP address: "));
|
||||
DEBUG_PRINT(WiFi.softAPIP());
|
||||
|
||||
/* Setup the DNS server redirecting all the domains to the apIP */
|
||||
/* Setup the DNS server redirecting all the domains to the apIP */
|
||||
dnsServer->setErrorReplyCode(DNSReplyCode::NoError);
|
||||
dnsServer->start(DNS_PORT, "*", WiFi.softAPIP());
|
||||
|
||||
@@ -165,24 +179,24 @@ void WiFiManager::begin(char const *apName, char const *apPasswd) {
|
||||
|
||||
boolean WiFiManager::autoConnect() {
|
||||
String ssid = "ESP" + String(ESP.getChipId());
|
||||
return autoConnect(ssid.c_str(),NULL);
|
||||
return autoConnect(ssid.c_str(), NULL);
|
||||
}
|
||||
|
||||
boolean WiFiManager::autoConnect(char const *apName) {
|
||||
return autoConnect(apName,NULL);
|
||||
return autoConnect(apName, NULL);
|
||||
}
|
||||
|
||||
boolean WiFiManager::autoConnect(char const *apName, char const *apPasswd) {
|
||||
DEBUG_PRINT(F(""));
|
||||
DEBUG_PRINT(F("AutoConnect"));
|
||||
|
||||
|
||||
// read eeprom for ssid and pass
|
||||
String ssid = getSSID();
|
||||
String pass = getPassword();
|
||||
//use SDK functions to get SSID and pass
|
||||
//String ssid = WiFi.SSID();
|
||||
//String pass = WiFi.psk();
|
||||
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
connectWifi(ssid, pass);
|
||||
int s = WiFi.status();
|
||||
@@ -192,26 +206,26 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPasswd) {
|
||||
//connected
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//not connected
|
||||
//setup AP
|
||||
WiFi.mode(WIFI_AP);
|
||||
//notify we entered AP mode
|
||||
if( _apcallback != NULL) {
|
||||
if ( _apcallback != NULL) {
|
||||
_apcallback();
|
||||
}
|
||||
connect = false;
|
||||
begin(apName,apPasswd);
|
||||
begin(apName, apPasswd);
|
||||
|
||||
bool looping = true;
|
||||
while(timeout == 0 || millis() < start + timeout) {
|
||||
while (timeout == 0 || millis() < start + timeout) {
|
||||
//DNS
|
||||
dnsServer->processNextRequest();
|
||||
//HTTP
|
||||
server->handleClient();
|
||||
|
||||
|
||||
if(connect) {
|
||||
|
||||
if (connect) {
|
||||
delay(2000);
|
||||
DEBUG_PRINT(F("Connecting to new AP"));
|
||||
connect = false;
|
||||
@@ -226,13 +240,13 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPasswd) {
|
||||
//delay(1000);
|
||||
//return false;
|
||||
} else {
|
||||
//connected
|
||||
//connected
|
||||
WiFi.mode(WIFI_STA);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
yield();
|
||||
yield();
|
||||
}
|
||||
|
||||
server.reset();
|
||||
@@ -272,7 +286,7 @@ String WiFiManager::getPassword() {
|
||||
}
|
||||
|
||||
/*
|
||||
String WiFiManager::getEEPROMString(int start, int len) {
|
||||
String WiFiManager::getEEPROMString(int start, int len) {
|
||||
EEPROM.begin(512);
|
||||
delay(10);
|
||||
String string = "";
|
||||
@@ -282,10 +296,10 @@ String WiFiManager::getEEPROMString(int start, int len) {
|
||||
}
|
||||
EEPROM.end();
|
||||
return string;
|
||||
}
|
||||
}
|
||||
*/
|
||||
/*
|
||||
void WiFiManager::setEEPROMString(int start, int len, String string) {
|
||||
void WiFiManager::setEEPROMString(int start, int len, String string) {
|
||||
EEPROM.begin(512);
|
||||
delay(10);
|
||||
int si = 0;
|
||||
@@ -303,7 +317,7 @@ void WiFiManager::setEEPROMString(int start, int len, String string) {
|
||||
}
|
||||
EEPROM.end();
|
||||
DEBUG_PRINT(F("Wrote " + string);
|
||||
}*/
|
||||
}*/
|
||||
|
||||
String WiFiManager::urldecode(const char *src)
|
||||
{
|
||||
@@ -348,7 +362,7 @@ void WiFiManager::resetSettings() {
|
||||
//delay(200);
|
||||
|
||||
//overwrite the current parameter values (with defaults)
|
||||
saveParameters();
|
||||
//saveParameters();
|
||||
}
|
||||
|
||||
void WiFiManager::setTimeout(unsigned long seconds) {
|
||||
@@ -392,7 +406,7 @@ void WiFiManager::handleRoot() {
|
||||
server->sendContent(title);
|
||||
server->sendContent(F("<h3>WiFiManager</h3>"));
|
||||
|
||||
|
||||
|
||||
server->sendContent_P(HTTP_PORTAL_OPTIONS);
|
||||
server->sendContent_P(HTTP_END);
|
||||
|
||||
@@ -431,7 +445,7 @@ void WiFiManager::handleWifi(bool scan) {
|
||||
rssiQ += getRSSIasQuality(WiFi.RSSI(i));
|
||||
item.replace("{v}", WiFi.SSID(i));
|
||||
item.replace("{r}", rssiQ);
|
||||
if(WiFi.encryptionType(i) != ENC_TYPE_NONE) {
|
||||
if (WiFi.encryptionType(i) != ENC_TYPE_NONE) {
|
||||
item.replace("{i}", FPSTR(HTTP_ITEM_PADLOCK));
|
||||
} else {
|
||||
item.replace("{i}", "");
|
||||
@@ -443,13 +457,15 @@ void WiFiManager::handleWifi(bool scan) {
|
||||
server->sendContent("<br/>");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
server->sendContent_P(HTTP_FORM_START);
|
||||
char parLength[2];
|
||||
// add the extra parameters to the form
|
||||
for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++){
|
||||
if(_params[i]==NULL)
|
||||
for (int i = 0; i < _paramsCount; i++) {
|
||||
if (_params[i] == NULL) {
|
||||
break;
|
||||
}
|
||||
|
||||
String pitem = FPSTR(HTTP_FORM_PARAM);
|
||||
pitem.replace("{i}", _params[i]->getID());
|
||||
pitem.replace("{n}", _params[i]->getID());
|
||||
@@ -460,14 +476,15 @@ void WiFiManager::handleWifi(bool scan) {
|
||||
|
||||
server->sendContent(pitem);
|
||||
}
|
||||
if(_params[0]!=NULL)
|
||||
if (_params[0] != NULL) {
|
||||
server->sendContent_P("<br/>");
|
||||
}
|
||||
|
||||
server->sendContent_P(HTTP_FORM_END);
|
||||
server->sendContent_P(HTTP_END);
|
||||
server->client().stop();
|
||||
|
||||
DEBUG_PRINT(F("Sent config page"));
|
||||
|
||||
DEBUG_PRINT(F("Sent config page"));
|
||||
}
|
||||
|
||||
/** Handle the WLAN save form and redirect to WLAN config page again */
|
||||
@@ -478,15 +495,18 @@ void WiFiManager::handleWifiSave() {
|
||||
_ssid = urldecode(server->arg("s").c_str());
|
||||
_pass = urldecode(server->arg("p").c_str());
|
||||
|
||||
//parameter
|
||||
for(int i=0; i<WIFI_MANAGER_MAX_PARAMS; i++){
|
||||
if(_params[i]==NULL)
|
||||
//parameters
|
||||
for (int i = 0; i < _paramsCount; i++) {
|
||||
if (_params[i] == NULL) {
|
||||
break;
|
||||
}
|
||||
//read parameter
|
||||
String value = urldecode(server->arg(_params[i]->getID()).c_str());
|
||||
//store it in array
|
||||
value.toCharArray(_params[i]->_value, _params[i]->_length);
|
||||
DEBUG_PRINT(F("Parameter"));
|
||||
DEBUG_PRINT(_params[i]->getID());
|
||||
DEBUG_PRINT(value);
|
||||
DEBUG_PRINT(F("Parameter"));
|
||||
DEBUG_PRINT(_params[i]->getID());
|
||||
DEBUG_PRINT(value);
|
||||
}
|
||||
saveParameters();
|
||||
|
||||
@@ -501,19 +521,19 @@ void WiFiManager::handleWifiSave() {
|
||||
server->sendContent_P(HTTP_SCRIPT);
|
||||
server->sendContent_P(HTTP_STYLE);
|
||||
server->sendContent_P(HTTP_HEAD_END);
|
||||
|
||||
|
||||
server->sendContent_P(HTTP_SAVED);
|
||||
|
||||
server->sendContent_P(HTTP_END);
|
||||
server->client().stop();
|
||||
|
||||
DEBUG_PRINT(F("Sent wifi save page"));
|
||||
|
||||
|
||||
DEBUG_PRINT(F("Sent wifi save page"));
|
||||
|
||||
connect = true; //signal ready to connect/reset
|
||||
}
|
||||
|
||||
void WiFiManager::handle204() {
|
||||
DEBUG_PRINT(F("204 No Response"));
|
||||
DEBUG_PRINT(F("204 No Response"));
|
||||
server->sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
server->sendHeader("Pragma", "no-cache");
|
||||
server->sendHeader("Expires", "-1");
|
||||
@@ -563,9 +583,9 @@ void WiFiManager::setAPCallback( void (*func)(void) ) {
|
||||
|
||||
template <typename Generic>
|
||||
void WiFiManager::DEBUG_PRINT(Generic text) {
|
||||
if(_debug) {
|
||||
if (_debug) {
|
||||
Serial.print("*WM: ");
|
||||
Serial.println(text);
|
||||
Serial.println(text);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -573,12 +593,12 @@ void WiFiManager::DEBUG_PRINT(Generic text) {
|
||||
int WiFiManager::getRSSIasQuality(int RSSI) {
|
||||
int quality = 0;
|
||||
|
||||
if(RSSI <= -100){
|
||||
quality = 0;
|
||||
}else if(RSSI >= -50){
|
||||
quality = 100;
|
||||
if (RSSI <= -100) {
|
||||
quality = 0;
|
||||
} else if (RSSI >= -50) {
|
||||
quality = 100;
|
||||
} else {
|
||||
quality = 2 * (RSSI + 100);
|
||||
quality = 2 * (RSSI + 100);
|
||||
}
|
||||
return quality;
|
||||
}
|
||||
|
||||
+7
-5
@@ -36,7 +36,7 @@ const char HTTP_FORM_END[] PROGMEM = "<br/><button type='submit'>save</button></
|
||||
const char HTTP_SAVED[] PROGMEM = "<div>Credentials Saved<br />Trying to connect ESP to network.<br />If it fails reconnect to AP to try again</div>";
|
||||
const char HTTP_END[] PROGMEM = "</div></body></html>";
|
||||
|
||||
#define WIFI_MANAGER_MAX_PARAMS 6
|
||||
#define WIFI_MANAGER_MAX_PARAMS 10
|
||||
|
||||
class WiFiManagerParameter {
|
||||
public:
|
||||
@@ -81,10 +81,11 @@ public:
|
||||
//sets a custom ip /gateway /subnet configuration
|
||||
void setAPConfig(IPAddress ip, IPAddress gw, IPAddress sn);
|
||||
void setAPCallback( void (*func)(void) );
|
||||
|
||||
void addParameter(WiFiManagerParameter *p);
|
||||
|
||||
void loadParameters();
|
||||
//adds a custom parameter
|
||||
void addParameter(WiFiManagerParameter *p);
|
||||
|
||||
//void loadParameters();
|
||||
void saveParameters();
|
||||
private:
|
||||
std::unique_ptr<DNSServer> dnsServer;
|
||||
@@ -121,6 +122,7 @@ private:
|
||||
IPAddress _ip;
|
||||
IPAddress _gw;
|
||||
IPAddress _sn;
|
||||
int _paramsCount = 0;
|
||||
|
||||
String getEEPROMString(int start, int len);
|
||||
void setEEPROMString(int start, int len, String string);
|
||||
@@ -150,7 +152,7 @@ private:
|
||||
void (*_apcallback)(void) = NULL;
|
||||
|
||||
WiFiManagerParameter* _params[WIFI_MANAGER_MAX_PARAMS];
|
||||
int _eepromLength;
|
||||
//int _eepromLength;
|
||||
|
||||
template <typename Generic>
|
||||
void DEBUG_PRINT(Generic text);
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
|
||||
|
||||
//needed for library
|
||||
#include <DNSServer.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
|
||||
|
||||
|
||||
// The extra parameters to be configured (can be either global or just in the setup)
|
||||
WiFiManagerParameter mqtt_server("server", "mqtt server", NULL, 40);
|
||||
WiFiManagerParameter mqtt_port("port", "mqtt port", "8080", 8);
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(115200);
|
||||
|
||||
//WiFiManager
|
||||
//Local intialization. Once its business is done, there is no need to keep it around
|
||||
WiFiManager wifiManager;
|
||||
|
||||
//add all your parameters here
|
||||
wifiManager.addParameter(&mqtt_server);
|
||||
wifiManager.addParameter(&mqtt_port);
|
||||
|
||||
//reset settings - for testing
|
||||
wifiManager.resetSettings();
|
||||
|
||||
//load previously saved parameters
|
||||
wifiManager.loadParameters();
|
||||
|
||||
//sets timeout until configuration portal gets turned off
|
||||
//useful to make it all retry or go to sleep
|
||||
//in seconds
|
||||
wifiManager.setTimeout(180);
|
||||
|
||||
//fetches ssid and pass and tries to connect
|
||||
//if it does not connect it starts an access point with the specified name
|
||||
//here "AutoConnectAP"
|
||||
//and goes into a blocking loop awaiting configuration
|
||||
if(!wifiManager.autoConnect("AutoConnectAP")) {
|
||||
Serial.println("failed to connect and hit timeout");
|
||||
delay(3000);
|
||||
//reset and try again, or maybe put it to deep sleep
|
||||
ESP.reset();
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
//if you get here you have connected to the WiFi
|
||||
Serial.println("connected...yeey :)");
|
||||
|
||||
// use your parameter values as needed
|
||||
Serial.print(mqtt_server.getPlaceholder());
|
||||
Serial.print(": ");
|
||||
Serial.println(mqtt_server.getValue());
|
||||
|
||||
Serial.print(mqtt_port.getPlaceholder());
|
||||
Serial.print(": ");
|
||||
Serial.println(mqtt_port.getValue());
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
#include <FS.h> //this needs to be first, or it all crashes and burns...
|
||||
|
||||
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
|
||||
|
||||
//needed for library
|
||||
#include <DNSServer.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
|
||||
|
||||
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
//define your default values here, if there are different values in config.json, they are overwritten.
|
||||
const char *mqtt_server = NULL;
|
||||
const char *mqtt_port = "8080";
|
||||
const char *blynk_token = "YOUR_BLYNK_TOKEN";
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(115200);
|
||||
Serial.println();
|
||||
|
||||
//read configuration from FS json
|
||||
//SPIFFS.format();
|
||||
|
||||
Serial.println("Mounting FS...");
|
||||
|
||||
if (!SPIFFS.begin()) {
|
||||
Serial.println("Failed to mount file system");
|
||||
//abort
|
||||
return;
|
||||
}
|
||||
|
||||
if (SPIFFS.exists("/config.json")) {
|
||||
//file exists, reading and loading
|
||||
Serial.println("Reading config file");
|
||||
File configFile = SPIFFS.open("/config.json", "r");
|
||||
if (configFile) {
|
||||
Serial.println("Opened config file");
|
||||
size_t size = configFile.size();
|
||||
// Allocate a buffer to store contents of the file.
|
||||
std::unique_ptr<char[]> buf(new char[size]);
|
||||
|
||||
configFile.readBytes(buf.get(), size);
|
||||
StaticJsonBuffer<200> jsonBuffer;
|
||||
JsonObject& json = jsonBuffer.parseObject(buf.get());
|
||||
json.printTo(Serial);
|
||||
if (json.success()) {
|
||||
Serial.println("\nParsed json");
|
||||
|
||||
mqtt_server = json["mqtt_server"];
|
||||
mqtt_port = json["mqtt_port"];
|
||||
blynk_token = json["blynk_token"];
|
||||
} else {
|
||||
Serial.println("Failed to load json config");
|
||||
}
|
||||
}
|
||||
}
|
||||
//end read
|
||||
|
||||
// The extra parameters to be configured (can be either global or just in the setup)
|
||||
// After connecting, parameter.getValue() will get you the configured value
|
||||
// id/name placeholder/prompt default length
|
||||
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
|
||||
WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 8);
|
||||
WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 32);
|
||||
|
||||
|
||||
//WiFiManager
|
||||
//Local intialization. Once its business is done, there is no need to keep it around
|
||||
WiFiManager wifiManager;
|
||||
|
||||
//add all your parameters here
|
||||
wifiManager.addParameter(&custom_mqtt_server);
|
||||
wifiManager.addParameter(&custom_mqtt_port);
|
||||
wifiManager.addParameter(&custom_blynk_token);
|
||||
|
||||
//reset settings - for testing
|
||||
//wifiManager.resetSettings();
|
||||
|
||||
//sets timeout until configuration portal gets turned off
|
||||
//useful to make it all retry or go to sleep
|
||||
//in seconds
|
||||
//wifiManager.setTimeout(120);
|
||||
|
||||
//fetches ssid and pass and tries to connect
|
||||
//if it does not connect it starts an access point with the specified name
|
||||
//here "AutoConnectAP"
|
||||
//and goes into a blocking loop awaiting configuration
|
||||
if (!wifiManager.autoConnect("AutoConnectAP")) {
|
||||
Serial.println("failed to connect and hit timeout");
|
||||
delay(3000);
|
||||
//reset and try again, or maybe put it to deep sleep
|
||||
ESP.reset();
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
//if you get here you have connected to the WiFi
|
||||
Serial.println("connected...yeey :)");
|
||||
|
||||
//read updated parameters
|
||||
mqtt_server = custom_mqtt_server.getValue();
|
||||
mqtt_port = custom_mqtt_port.getValue();
|
||||
blynk_token = custom_blynk_token.getValue();
|
||||
|
||||
//save the custom parameters to FS
|
||||
StaticJsonBuffer<200> jsonBuffer;
|
||||
JsonObject& json = jsonBuffer.createObject();
|
||||
json["mqtt_server"] = mqtt_server;
|
||||
json["mqtt_port"] = mqtt_port;
|
||||
json["blynk_token"] = blynk_token;
|
||||
|
||||
File configFile = SPIFFS.open("/config.json", "w");
|
||||
if (!configFile) {
|
||||
Serial.println("Failed to open config file for writing");
|
||||
return;
|
||||
}
|
||||
|
||||
json.printTo(Serial);
|
||||
json.printTo(configFile);
|
||||
configFile.close();
|
||||
//end save
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user