From e01101eff1c22d46bed1d76fde833abca888ab47 Mon Sep 17 00:00:00 2001 From: Shawn A Date: Tue, 20 Feb 2018 11:54:14 -0600 Subject: [PATCH] #534 --- WiFiManager.cpp | 26 ++++++++++++------- WiFiManager.h | 2 ++ .../AutoConnectWithFSParameters.ino | 6 ++++- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index d3179a9..190febb 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -31,6 +31,10 @@ WiFiManagerParameter::WiFiManagerParameter(const char *custom) { _customHTML = custom; } +WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *placeholder) { + init(id, placeholder, "", 0, "", WFM_LABEL_BEFORE); +} + WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length) { init(id, placeholder, defaultValue, length, "", WFM_LABEL_BEFORE); } @@ -46,17 +50,9 @@ WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *placehold void WiFiManagerParameter::init(const char *id, const char *placeholder, const char *defaultValue, int length, const char *custom, int labelPlacement) { _id = id; _placeholder = placeholder; - _length = length; _labelPlacement = labelPlacement; - _value = new char[length + 1]; - for (int i = 0; i < length + 1; i++) { - _value[i] = 0; - } - if (defaultValue != NULL) { - strncpy(_value, defaultValue, length + 1); // length+1 due to null terminated string - } - - _customHTML = custom; + _customHTML = custom; + setValue(defaultValue,length); } WiFiManagerParameter::~WiFiManagerParameter() { @@ -65,6 +61,16 @@ WiFiManagerParameter::~WiFiManagerParameter() { } } +void WiFiManagerParameter::setValue(const char *defaultValue, int length) { + _length = length; + _value = new char[length + 1]; + for (int i = 0; i < length + 1; i++) { + _value[i] = 0; + } + if (defaultValue != NULL) { + strncpy(_value, defaultValue, length + 1); // length+1 due to null terminated string + } +} const char* WiFiManagerParameter::getValue() { return _value; } diff --git a/WiFiManager.h b/WiFiManager.h index 4a5e239..5b4f445 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -67,6 +67,7 @@ class WiFiManagerParameter { @id is used for HTTP queries and must not contain spaces nor other special characters */ WiFiManagerParameter(const char *custom); + WiFiManagerParameter(const char *id, const char *placeholder); WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length); WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length, const char *custom); WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length, const char *custom, int labelPlacement); @@ -78,6 +79,7 @@ class WiFiManagerParameter { int getValueLength(); int getLabelPlacement(); const char *getCustomHTML(); + void setValue(const char *defaultValue, int length); private: const char *_id; const char *_placeholder; diff --git a/examples/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino b/examples/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino index d46729c..8445054 100644 --- a/examples/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino +++ b/examples/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino @@ -1,6 +1,7 @@ #include // this needs to be first, or it all crashes and burns... #include // https://github.com/tzapu/WiFiManager #include // https://github.com/bblanchon/ArduinoJson +#include //define your default values here, if there are different values in config.json, they are overwritten. char mqtt_server[40]; @@ -68,7 +69,10 @@ void setup() { // 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, 6); - WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 32); + // WiFiManagerParameter custom_blynk_token("blynk", "blynk token", "", 0); + + WiFiManagerParameter custom_blynk_token("blynk", "blynk token"); + custom_blynk_token.setValue("setvalue override",32); //WiFiManager //Local intialization. Once its business is done, there is no need to keep it around