Added functionality to toggle between ArduinoJson 5 and 6

This commit is contained in:
Ruan de Villiers
2020-01-14 21:20:51 +02:00
parent 210b4c5d01
commit 9669bfd1a3
@@ -9,11 +9,14 @@
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
//Comment out this line if you are still using ArduinoJsonV5
#define UseJsonV6
//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 +55,29 @@ void setup() {
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
#ifdef UseJsonV6
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 +110,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 +128,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 +157,12 @@ void setup() {
//save the custom parameters to FS
if (shouldSaveConfig) {
Serial.println("saving config");
#ifdef UseJsonV6
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 +176,13 @@ void setup() {
Serial.println("failed to open config file for writing");
}
json.prettyPrintTo(Serial);
#ifdef UseJsonV6
serializeJson(json, Serial);
serializeJson(json, configFile);
#else
json.printTo(Serial);
json.printTo(configFile);
#endif
configFile.close();
//end save
}
@@ -183,6 +195,4 @@ void setup() {
void loop() {
// put your main code here, to run repeatedly:
}