Merge pull request #993 from Ruandv/issue654

Fix ArduinoJson (Using ARDUINOJSON_VERSION_MAJOR)
This commit is contained in:
Shawn A
2020-01-27 14:28:25 -06:00
committed by GitHub
6 changed files with 65 additions and 46 deletions
+13 -13
View File
@@ -301,7 +301,7 @@ int WiFiManager::connectWifi(String ssid, String pass) {
DEBUG_WM(WiFi.localIP());
}
//fix for auto connect racing issue
if (WiFi.status() == WL_CONNECTED) {
if (WiFi.status() == WL_CONNECTED && (WiFi.SSID() == ssid)) {
DEBUG_WM(F("Already connected. Bailing out."));
return WL_CONNECTED;
}
@@ -322,7 +322,7 @@ int WiFiManager::connectWifi(String ssid, String pass) {
DEBUG_WM(res);
}
} else {
if (WiFi.SSID()) {
if (WiFi.SSID() != "") {
DEBUG_WM(F("Using last saved values, should be faster"));
//trying to fix connection in progress hanging
ETS_UART_INTR_DISABLE();
@@ -362,7 +362,7 @@ uint8_t WiFiManager::waitForConnectResult() {
keepConnecting = false;
DEBUG_WM (F("Connection timed out"));
}
if (status == WL_CONNECTED || status == WL_CONNECT_FAILED) {
if (status == WL_CONNECTED) {
keepConnecting = false;
}
delay(100);
@@ -450,12 +450,12 @@ void WiFiManager::handleRoot() {
return;
}
String page = FPSTR(HTTP_HEAD);
String page = FPSTR(HTTP_HEADER);
page.replace("{v}", "Options");
page += FPSTR(HTTP_SCRIPT);
page += FPSTR(HTTP_STYLE);
page += _customHeadElement;
page += FPSTR(HTTP_HEAD_END);
page += FPSTR(HTTP_HEADER_END);
page += String(F("<h1>"));
page += _apName;
page += String(F("</h1>"));
@@ -471,12 +471,12 @@ void WiFiManager::handleRoot() {
/** Wifi config page handler */
void WiFiManager::handleWifi(boolean scan) {
String page = FPSTR(HTTP_HEAD);
String page = FPSTR(HTTP_HEADER);
page.replace("{v}", "Config ESP");
page += FPSTR(HTTP_SCRIPT);
page += FPSTR(HTTP_STYLE);
page += _customHeadElement;
page += FPSTR(HTTP_HEAD_END);
page += FPSTR(HTTP_HEADER_END);
if (scan) {
int n = WiFi.scanNetworks();
@@ -666,12 +666,12 @@ void WiFiManager::handleWifiSave() {
optionalIPFromString(&_sta_static_sn, sn.c_str());
}
String page = FPSTR(HTTP_HEAD);
String page = FPSTR(HTTP_HEADER);
page.replace("{v}", "Credentials Saved");
page += FPSTR(HTTP_SCRIPT);
page += FPSTR(HTTP_STYLE);
page += _customHeadElement;
page += FPSTR(HTTP_HEAD_END);
page += FPSTR(HTTP_HEADER_END);
page += FPSTR(HTTP_SAVED);
page += FPSTR(HTTP_END);
@@ -687,12 +687,12 @@ void WiFiManager::handleWifiSave() {
void WiFiManager::handleInfo() {
DEBUG_WM(F("Info"));
String page = FPSTR(HTTP_HEAD);
String page = FPSTR(HTTP_HEADER);
page.replace("{v}", "Info");
page += FPSTR(HTTP_SCRIPT);
page += FPSTR(HTTP_STYLE);
page += _customHeadElement;
page += FPSTR(HTTP_HEAD_END);
page += FPSTR(HTTP_HEADER_END);
page += F("<dl>");
page += F("<dt>Chip ID</dt><dd>");
page += ESP.getChipId();
@@ -728,12 +728,12 @@ void WiFiManager::handleInfo() {
void WiFiManager::handleReset() {
DEBUG_WM(F("Reset"));
String page = FPSTR(HTTP_HEAD);
String page = FPSTR(HTTP_HEADER);
page.replace("{v}", "Info");
page += FPSTR(HTTP_SCRIPT);
page += FPSTR(HTTP_STYLE);
page += _customHeadElement;
page += FPSTR(HTTP_HEAD_END);
page += FPSTR(HTTP_HEADER_END);
page += F("Module will reset in a few seconds.");
page += FPSTR(HTTP_END);
+3 -3
View File
@@ -22,10 +22,10 @@ extern "C" {
#include "user_interface.h"
}
const char HTTP_HEAD[] PROGMEM = "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\" name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/><title>{v}</title>";
const char HTTP_HEADER[] PROGMEM = "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/><title>{v}</title>";
const char HTTP_STYLE[] PROGMEM = "<style>.c{text-align: center;} div,input{padding:5px;font-size:1em;} input{width:95%;} body{text-align: center;font-family:verdana;} button{border:0;border-radius:0.3rem;background-color:#1fa3ec;color:#fff;line-height:2.4rem;font-size:1.2rem;width:100%;} .q{float: right;width: 64px;text-align: right;} .l{background: url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAALVBMVEX///8EBwfBwsLw8PAzNjaCg4NTVVUjJiZDRUUUFxdiZGSho6OSk5Pg4eFydHTCjaf3AAAAZElEQVQ4je2NSw7AIAhEBamKn97/uMXEGBvozkWb9C2Zx4xzWykBhFAeYp9gkLyZE0zIMno9n4g19hmdY39scwqVkOXaxph0ZCXQcqxSpgQpONa59wkRDOL93eAXvimwlbPbwwVAegLS1HGfZAAAAABJRU5ErkJggg==\") no-repeat left center;background-size: 1em;}</style>";
const char HTTP_SCRIPT[] PROGMEM = "<script>function c(l){document.getElementById('s').value=l.innerText||l.textContent;document.getElementById('p').focus();}</script>";
const char HTTP_HEAD_END[] PROGMEM = "</head><body><div style='text-align:left;display:inline-block;min-width:260px;'>";
const char HTTP_HEADER_END[] PROGMEM = "</head><body><div style='text-align:left;display:inline-block;min-width:260px;'>";
const char HTTP_PORTAL_OPTIONS[] PROGMEM = "<form action=\"/wifi\" method=\"get\"><button>Configure WiFi</button></form><br/><form action=\"/0wifi\" method=\"get\"><button>Configure WiFi (No Scan)</button></form><br/><form action=\"/i\" method=\"get\"><button>Info</button></form><br/><form action=\"/r\" method=\"post\"><button>Reset</button></form>";
const char HTTP_ITEM[] PROGMEM = "<div><a href='#p' onclick='c(this)'>{v}</a>&nbsp;<span class='q {i}'>{r}%</span></div>";
const char HTTP_FORM_START[] PROGMEM = "<form method='get' action='wifisave'><input id='s' name='s' length=32 placeholder='SSID'><br/><input id='p' name='p' length=64 type='password' placeholder='password'><br/>";
@@ -125,7 +125,7 @@ class WiFiManager
//const int WM_DONE = 0;
//const int WM_WAIT = 10;
//const String HTTP_HEAD = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/><title>{v}</title>";
//const String HTTP_HEADER = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/><title>{v}</title>";
void setupConfigPortal();
void startWPS();
@@ -1,7 +1,5 @@
#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>
@@ -23,7 +21,6 @@ void saveConfigCallback () {
shouldSaveConfig = true;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
@@ -48,16 +45,22 @@ void setup() {
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
#ifdef ARDUINOJSON_VERSION_MAJOR >= 6
DynamicJsonDocument json(1024);
auto deserializeError = deserializeJson(json, buf.get());
serializeJson(json, Serial);
if ( ! deserializeError ) {
#else
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success()) {
#endif
Serial.println("\nparsed json");
strcpy(mqtt_server, json["mqtt_server"]);
strcpy(mqtt_port, json["mqtt_port"]);
strcpy(blynk_token, json["blynk_token"]);
} else {
Serial.println("failed to load json config");
}
@@ -69,8 +72,6 @@ void setup() {
}
//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
@@ -86,8 +87,8 @@ void setup() {
wifiManager.setSaveConfigCallback(saveConfigCallback);
//set static ip
wifiManager.setSTAStaticIPConfig(IPAddress(10,0,1,99), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
wifiManager.setSTAStaticIPConfig(IPAddress(10, 0, 1, 99), IPAddress(10, 0, 1, 1), IPAddress(255, 255, 255, 0));
//add all your parameters here
wifiManager.addParameter(&custom_mqtt_server);
wifiManager.addParameter(&custom_mqtt_port);
@@ -99,7 +100,7 @@ void setup() {
//set minimu quality of signal so it ignores AP's under that quality
//defaults to 8%
//wifiManager.setMinimumSignalQuality();
//sets timeout until configuration portal gets turned off
//useful to make it all retry or go to sleep
//in seconds
@@ -124,12 +125,20 @@ void setup() {
strcpy(mqtt_server, custom_mqtt_server.getValue());
strcpy(mqtt_port, custom_mqtt_port.getValue());
strcpy(blynk_token, custom_blynk_token.getValue());
Serial.println("The values in the file are: ");
Serial.println("\tmqtt_server : " + String(mqtt_server));
Serial.println("\tmqtt_port : " + String(mqtt_port));
Serial.println("\tblynk_token : " + String(blynk_token));
//save the custom parameters to FS
if (shouldSaveConfig) {
Serial.println("saving config");
#ifdef ARDUINOJSON_VERSION_MAJOR >= 6
DynamicJsonDocument json(1024);
#else
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
#endif
json["mqtt_server"] = mqtt_server;
json["mqtt_port"] = mqtt_port;
json["blynk_token"] = blynk_token;
@@ -139,19 +148,22 @@ void setup() {
Serial.println("failed to open config file for writing");
}
#ifdef ARDUINOJSON_VERSION_MAJOR >= 6
serializeJson(json, Serial);
serializeJson(json, configFile);
#else
json.printTo(Serial);
json.printTo(configFile);
#endif
configFile.close();
//end save
}
Serial.println("local ip");
Serial.println(WiFi.localIP());
}
void loop() {
// put your main code here, to run repeatedly:
}
@@ -10,10 +10,10 @@
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
//define your default values here, if there are different values in config.json, they are overwritten.
//length should be max size + 1
//length should be max size + 1
char mqtt_server[40];
char mqtt_port[6] = "8080";
char blynk_token[33] = "YOUR_BLYNK_TOKEN";
char blynk_token[34] = "YOUR_BLYNK_TOKEN";
//default custom static IP
char static_ip[16] = "10.0.1.56";
char static_gw[16] = "10.0.1.1";
@@ -52,29 +52,29 @@ void setup() {
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
#ifdef ARDUINOJSON_VERSION_MAJOR >= 6
DynamicJsonDocument json(1024);
auto deserializeError = deserializeJson(json, buf.get());
serializeJson(json, Serial);
if ( ! deserializeError ) {
#else
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success()) {
#endif
Serial.println("\nparsed json");
strcpy(mqtt_server, json["mqtt_server"]);
strcpy(mqtt_port, json["mqtt_port"]);
strcpy(blynk_token, json["blynk_token"]);
if(json["ip"]) {
if (json["ip"]) {
Serial.println("setting custom ip from config");
//static_ip = json["ip"];
strcpy(static_ip, json["ip"]);
strcpy(static_gw, json["gateway"]);
strcpy(static_sn, json["subnet"]);
//strcat(static_ip, json["ip"]);
//static_gw = json["gateway"];
//static_sn = json["subnet"];
Serial.println(static_ip);
/* Serial.println("converting ip");
IPAddress ip = ipFromCharArray(static_ip);
Serial.println(ip);*/
} else {
Serial.println("no custom ip in config");
}
@@ -107,13 +107,13 @@ void setup() {
wifiManager.setSaveConfigCallback(saveConfigCallback);
//set static ip
IPAddress _ip,_gw,_sn;
IPAddress _ip, _gw, _sn;
_ip.fromString(static_ip);
_gw.fromString(static_gw);
_sn.fromString(static_sn);
wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn);
//add all your parameters here
wifiManager.addParameter(&custom_mqtt_server);
wifiManager.addParameter(&custom_mqtt_port);
@@ -125,7 +125,7 @@ void setup() {
//set minimu quality of signal so it ignores AP's under that quality
//defaults to 8%
wifiManager.setMinimumSignalQuality();
//sets timeout until configuration portal gets turned off
//useful to make it all retry or go to sleep
//in seconds
@@ -154,8 +154,12 @@ void setup() {
//save the custom parameters to FS
if (shouldSaveConfig) {
Serial.println("saving config");
#ifdef ARDUINOJSON_VERSION_MAJOR >= 6
DynamicJsonDocument json(1024);
#else
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
#endif
json["mqtt_server"] = mqtt_server;
json["mqtt_port"] = mqtt_port;
json["blynk_token"] = blynk_token;
@@ -169,8 +173,13 @@ void setup() {
Serial.println("failed to open config file for writing");
}
json.prettyPrintTo(Serial);
#ifdef ARDUINOJSON_VERSION_MAJOR >= 6
serializeJson(json, Serial);
serializeJson(json, configFile);
#else
json.printTo(Serial);
json.printTo(configFile);
#endif
configFile.close();
//end save
}
@@ -183,6 +192,4 @@ void setup() {
void loop() {
// put your main code here, to run repeatedly:
}
+1 -1
View File
@@ -9,5 +9,5 @@
},
"frameworks": "arduino",
"platforms": "espressif8266",
"version": "0.14"
"version": "0.15.0-beta"
}
+1 -1
View File
@@ -1,5 +1,5 @@
name=WiFiManager
version=0.14
version=0.15.0-beta
author=tzapu
maintainer=tzapu
sentence=ESP8266 WiFi Connection manager with fallback web configuration portal