Upgrade to ArduinoJSON 6.xx from 5.xx

This commit is contained in:
debsahu 2018-06-17 15:53:35 -04:00
parent 77be51d6c5
commit e499033ef3
4 changed files with 53 additions and 52 deletions

View file

@ -494,8 +494,8 @@ void setup() {
//// json += ", \"gpio\":" + String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16))); //// json += ", \"gpio\":" + String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16)));
//json += "}"; //json += "}";
DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(9)); DynamicJsonDocument jsonBuffer(JSON_OBJECT_SIZE(9));
JsonObject& json = jsonBuffer.createObject(); JsonObject& json = jsonBuffer.to<JsonObject>();
json["HOSTNAME"] = HOSTNAME; json["HOSTNAME"] = HOSTNAME;
json["version"] = SKETCH_VERSION; json["version"] = SKETCH_VERSION;
@ -526,7 +526,7 @@ void setup() {
//char buffer[json.measureLength() + 1]; //char buffer[json.measureLength() + 1];
//json.printTo(buffer, sizeof(buffer)); //json.printTo(buffer, sizeof(buffer));
String json_str; String json_str;
json.printTo(json_str); serializeJson(json, json_str);
server.sendHeader("Access-Control-Allow-Origin", "*"); server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "application/json", json_str); server.send(200, "application/json", json_str);
//json_str = String(); //json_str = String();

View file

@ -289,8 +289,8 @@ String listStatusJSON(void) {
// mode, tmp_mode, modeName, ws2812fx_speed, brightness, main_color.red, main_color.green, main_color.blue); // mode, tmp_mode, modeName, ws2812fx_speed, brightness, main_color.red, main_color.green, main_color.blue);
const size_t bufferSize = JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(6); const size_t bufferSize = JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(6);
DynamicJsonBuffer jsonBuffer(bufferSize); DynamicJsonDocument jsonBuffer(bufferSize);
JsonObject& root = jsonBuffer.createObject(); JsonObject& root = jsonBuffer.to<JsonObject>();
root["mode"] = (uint8_t) mode; root["mode"] = (uint8_t) mode;
root["ws2812fx_mode"] = tmp_mode; root["ws2812fx_mode"] = tmp_mode;
root["ws2812fx_mode_name"] = strip.getModeName(tmp_mode); root["ws2812fx_mode_name"] = strip.getModeName(tmp_mode);
@ -305,7 +305,7 @@ String listStatusJSON(void) {
// root.printTo(json, sizeof(json)); // root.printTo(json, sizeof(json));
String json; String json;
root.printTo(json); serializeJson(root, json);
return json; return json;
} }
@ -328,8 +328,8 @@ String listModesJSON(void) {
// return modes; // return modes;
const size_t bufferSize = JSON_ARRAY_SIZE(strip.getModeCount()+1) + strip.getModeCount()*JSON_OBJECT_SIZE(2); const size_t bufferSize = JSON_ARRAY_SIZE(strip.getModeCount()+1) + strip.getModeCount()*JSON_OBJECT_SIZE(2);
DynamicJsonBuffer jsonBuffer(bufferSize); DynamicJsonDocument jsonBuffer(bufferSize);
JsonArray& json = jsonBuffer.createArray(); JsonArray& json = jsonBuffer.to<JsonArray>();
for (uint8_t i = 0; i < strip.getModeCount(); i++) { for (uint8_t i = 0; i < strip.getModeCount(); i++) {
JsonObject& object = json.createNestedObject(); JsonObject& object = json.createNestedObject();
object["mode"] = i; object["mode"] = i;
@ -338,7 +338,7 @@ String listModesJSON(void) {
JsonObject& object = json.createNestedObject(); JsonObject& object = json.createNestedObject();
String json_str; String json_str;
json.printTo(json_str); serializeJson(json, json_str);
return json_str; return json_str;
} }
@ -790,9 +790,8 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
void sendState() { void sendState() {
const size_t bufferSize = JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(6); const size_t bufferSize = JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(6);
//StaticJsonBuffer<bufferSize> jsonBuffer; DynamicJsonDocument jsonBuffer(bufferSize);
DynamicJsonBuffer jsonBuffer(bufferSize); JsonObject& root = jsonBuffer.to<JsonObject>();
JsonObject& root = jsonBuffer.createObject();
root["state"] = (stateOn) ? on_cmd : off_cmd; root["state"] = (stateOn) ? on_cmd : off_cmd;
JsonObject& color = root.createNestedObject("color"); JsonObject& color = root.createNestedObject("color");
@ -810,8 +809,8 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
strncpy_P(modeName, (PGM_P)strip.getModeName(strip.getMode()), sizeof(modeName)); // copy from progmem strncpy_P(modeName, (PGM_P)strip.getModeName(strip.getMode()), sizeof(modeName)); // copy from progmem
root["effect"] = modeName; root["effect"] = modeName;
char buffer[root.measureLength() + 1]; char buffer[measureJson(root) + 1];
root.printTo(buffer, sizeof(buffer)); serializeJson(root, buffer, sizeof(buffer));
#ifdef ENABLE_MQTT #ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_ha_state_out.c_str(), buffer, true); mqtt_client.publish(mqtt_ha_state_out.c_str(), buffer, true);
@ -828,16 +827,16 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
bool processJson(char* message) { bool processJson(char* message) {
const size_t bufferSize = JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(5) + 150; const size_t bufferSize = JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(5) + 150;
//StaticJsonBuffer<bufferSize> jsonBuffer; DynamicJsonDocument jsonBuffer(bufferSize);
DynamicJsonBuffer jsonBuffer(bufferSize); DeserializationError error = deserializeJson(jsonBuffer, message);
JsonObject& root = jsonBuffer.parseObject(message); if (error) {
DBG_OUTPUT_PORT.print("parseObject() failed: ");
if (!root.success()) { DBG_OUTPUT_PORT.println(error.c_str());
DBG_OUTPUT_PORT.println("parseObject() failed");
return false; return false;
} }
//DBG_OUTPUT_PORT.println("JSON ParseObject() done!"); //DBG_OUTPUT_PORT.println("JSON ParseObject() done!");
JsonObject& root = jsonBuffer.as<JsonObject>();
if (root.containsKey("state")) { if (root.containsKey("state")) {
const char* state_in = root["state"]; const char* state_in = root["state"];
if (strcmp(state_in, on_cmd) == 0 and !(animation_on)) { if (strcmp(state_in, on_cmd) == 0 and !(animation_on)) {
@ -886,7 +885,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
if (root.containsKey("effect")) { if (root.containsKey("effect")) {
animation_on = true; animation_on = true;
String effectString = root["effect"].asString(); String effectString = root["effect"].as<String>();
for (uint8_t i = 0; i < strip.getModeCount(); i++) { for (uint8_t i = 0; i < strip.getModeCount(); i++) {
if(String(strip.getModeName(i)) == effectString) { if(String(strip.getModeName(i)) == effectString) {
@ -963,8 +962,8 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
ha_send_data.detach(); ha_send_data.detach();
mqtt_client.subscribe(mqtt_ha_state_in.c_str(), qossub); mqtt_client.subscribe(mqtt_ha_state_in.c_str(), qossub);
#ifdef MQTT_HOME_ASSISTANT_SUPPORT #ifdef MQTT_HOME_ASSISTANT_SUPPORT
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(strip.getModeCount()) + JSON_OBJECT_SIZE(11)); DynamicJsonDocument jsonBuffer(JSON_ARRAY_SIZE(strip.getModeCount()) + JSON_OBJECT_SIZE(11));
JsonObject& json = jsonBuffer.createObject(); JsonObject& json = jsonBuffer.to<JsonObject>();
json["name"] = HOSTNAME; json["name"] = HOSTNAME;
json["platform"] = "mqtt_json"; json["platform"] = "mqtt_json";
json["state_topic"] = mqtt_ha_state_out; json["state_topic"] = mqtt_ha_state_out;
@ -979,8 +978,8 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
for (uint8_t i = 0; i < strip.getModeCount(); i++) { for (uint8_t i = 0; i < strip.getModeCount(); i++) {
effect_list.add(strip.getModeName(i)); effect_list.add(strip.getModeName(i));
} }
char buffer[json.measureLength() + 1]; char buffer[json.measureJson() + 1];
json.printTo(buffer, sizeof(buffer)); serializeJson(json, buffer, sizeof(buffer));
mqtt_client.publish(String("homeassistant/light/" + String(HOSTNAME) + "/config").c_str(), buffer, true); mqtt_client.publish(String("homeassistant/light/" + String(HOSTNAME) + "/config").c_str(), buffer, true);
#endif #endif
#endif #endif
@ -1043,8 +1042,8 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
uint16_t packetIdSub2 = amqttClient.subscribe((char *)mqtt_ha_state_in.c_str(), qossub); uint16_t packetIdSub2 = amqttClient.subscribe((char *)mqtt_ha_state_in.c_str(), qossub);
DBG_OUTPUT_PORT.printf("Subscribing at QoS %d, packetId: ", qossub); DBG_OUTPUT_PORT.println(packetIdSub2); DBG_OUTPUT_PORT.printf("Subscribing at QoS %d, packetId: ", qossub); DBG_OUTPUT_PORT.println(packetIdSub2);
#ifdef MQTT_HOME_ASSISTANT_SUPPORT #ifdef MQTT_HOME_ASSISTANT_SUPPORT
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(strip.getModeCount()) + JSON_OBJECT_SIZE(11)); DynamicJsonDocument jsonBuffer(JSON_ARRAY_SIZE(strip.getModeCount()) + JSON_OBJECT_SIZE(11));
JsonObject& json = jsonBuffer.createObject(); JsonObject& json = jsonBuffer.to<JsonObject>();
json["name"] = HOSTNAME; json["name"] = HOSTNAME;
json["platform"] = "mqtt_json"; json["platform"] = "mqtt_json";
json["state_topic"] = mqtt_ha_state_out; json["state_topic"] = mqtt_ha_state_out;
@ -1059,8 +1058,8 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
for (uint8_t i = 0; i < strip.getModeCount(); i++) { for (uint8_t i = 0; i < strip.getModeCount(); i++) {
effect_list.add(strip.getModeName(i)); effect_list.add(strip.getModeName(i));
} }
char buffer[json.measureLength() + 1]; char buffer[json.measureJson() + 1];
json.printTo(buffer, sizeof(buffer)); serializeJson(json, buffer, sizeof(buffer));
DBG_OUTPUT_PORT.println(buffer); DBG_OUTPUT_PORT.println(buffer);
amqttClient.publish(String("homeassistant/light/" + String(HOSTNAME) + "/config").c_str(), qospub, true, buffer); amqttClient.publish(String("homeassistant/light/" + String(HOSTNAME) + "/config").c_str(), qospub, true, buffer);
#endif #endif
@ -1213,9 +1212,8 @@ bool writeConfigFS(bool saveConfig){
//FS save //FS save
updateFS = true; updateFS = true;
DBG_OUTPUT_PORT.print("Saving config: "); DBG_OUTPUT_PORT.print("Saving config: ");
DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(4)); DynamicJsonDocument jsonBuffer(JSON_OBJECT_SIZE(4));
// StaticJsonBuffer<JSON_OBJECT_SIZE(4)> jsonBuffer; JsonObject& json = jsonBuffer.to<JsonObject>();
JsonObject& json = jsonBuffer.createObject();
json["mqtt_host"] = mqtt_host; json["mqtt_host"] = mqtt_host;
json["mqtt_port"] = mqtt_port; json["mqtt_port"] = mqtt_port;
json["mqtt_user"] = mqtt_user; json["mqtt_user"] = mqtt_user;
@ -1225,8 +1223,8 @@ bool writeConfigFS(bool saveConfig){
File configFile = SPIFFS.open("/config.json", "w"); File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) DBG_OUTPUT_PORT.println("failed to open config file for writing"); if (!configFile) DBG_OUTPUT_PORT.println("failed to open config file for writing");
json.printTo(DBG_OUTPUT_PORT); serializeJson(json, DBG_OUTPUT_PORT);
json.printTo(configFile); serializeJson(json, configFile);
configFile.close(); configFile.close();
updateFS = false; updateFS = false;
return true; return true;
@ -1250,13 +1248,13 @@ bool readConfigFS() {
size_t size = configFile.size(); size_t size = configFile.size();
std::unique_ptr<char[]> buf(new char[size]); std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size); configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(4)+300); DynamicJsonDocument jsonBuffer(JSON_OBJECT_SIZE(4)+300);
// StaticJsonBuffer<JSON_OBJECT_SIZE(4)+300> jsonBuffer; DeserializationError error = deserializeJson(jsonBuffer, buf.get());
JsonObject& json = jsonBuffer.parseObject(buf.get());
DBG_OUTPUT_PORT.print("Config: "); DBG_OUTPUT_PORT.print("Config: ");
json.printTo(DBG_OUTPUT_PORT); if (!error) {
if (json.success()) {
DBG_OUTPUT_PORT.println(" Parsed!"); DBG_OUTPUT_PORT.println(" Parsed!");
JsonObject& json = jsonBuffer.as<JsonObject>();
serializeJson(json, DBG_OUTPUT_PORT);
strcpy(mqtt_host, json["mqtt_host"]); strcpy(mqtt_host, json["mqtt_host"]);
strcpy(mqtt_port, json["mqtt_port"]); strcpy(mqtt_port, json["mqtt_port"]);
strcpy(mqtt_user, json["mqtt_user"]); strcpy(mqtt_user, json["mqtt_user"]);
@ -1264,7 +1262,8 @@ bool readConfigFS() {
updateFS = false; updateFS = false;
return true; return true;
} else { } else {
DBG_OUTPUT_PORT.println("Failed to load json config"); DBG_OUTPUT_PORT.print("Failed to load json config: ");
DBG_OUTPUT_PORT.println(error.c_str());
} }
} else { } else {
DBG_OUTPUT_PORT.println("Failed to open /config.json"); DBG_OUTPUT_PORT.println("Failed to open /config.json");
@ -1282,9 +1281,8 @@ bool writeStateFS(){
updateFS = true; updateFS = true;
//save the strip state to FS JSON //save the strip state to FS JSON
DBG_OUTPUT_PORT.print("Saving cfg: "); DBG_OUTPUT_PORT.print("Saving cfg: ");
DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(7)); DynamicJsonDocument jsonBuffer(JSON_OBJECT_SIZE(7));
// StaticJsonBuffer<JSON_OBJECT_SIZE(7)> jsonBuffer; JsonObject& json = jsonBuffer.to<JsonObject>();
JsonObject& json = jsonBuffer.createObject();
json["mode"] = static_cast<int>(mode); json["mode"] = static_cast<int>(mode);
json["strip_mode"] = (int) strip.getMode(); json["strip_mode"] = (int) strip.getMode();
json["brightness"] = brightness; json["brightness"] = brightness;
@ -1302,8 +1300,8 @@ bool writeStateFS(){
updateStateFS = false; updateStateFS = false;
return false; return false;
} }
json.printTo(DBG_OUTPUT_PORT); serializeJson(json, DBG_OUTPUT_PORT);
json.printTo(configFile); serializeJson(json, configFile);
configFile.close(); configFile.close();
updateFS = false; updateFS = false;
spiffs_save_state.detach(); spiffs_save_state.detach();
@ -1325,11 +1323,11 @@ bool readStateFS() {
// Allocate a buffer to store contents of the file. // Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]); std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size); configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(7)+200); DynamicJsonDocument jsonBuffer(JSON_OBJECT_SIZE(7)+200);
// StaticJsonBuffer<JSON_OBJECT_SIZE(7)+200> jsonBuffer; DeserializationError error = deserializeJson(jsonBuffer, buf.get());
JsonObject& json = jsonBuffer.parseObject(buf.get()); if (!error) {
json.printTo(DBG_OUTPUT_PORT); JsonObject& json = jsonBuffer.as<JsonObject>();
if (json.success()) { serializeJson(json, DBG_OUTPUT_PORT);
mode = static_cast<MODE>((int) json["mode"]); mode = static_cast<MODE>((int) json["mode"]);
ws2812fx_mode = json["strip_mode"]; ws2812fx_mode = json["strip_mode"];
brightness = json["brightness"]; brightness = json["brightness"];

View file

@ -1 +1 @@
#define SKETCH_VERSION "2.1.1" #define SKETCH_VERSION "2.1.2"

View file

@ -6,4 +6,7 @@
* 11 May 2018 v 2.1.1 * 11 May 2018 v 2.1.1
* - Use ArduinoJSON to send JSON replies * - Use ArduinoJSON to send JSON replies
* - Add strip.trigger() * - Add strip.trigger()
*
* 17 Jun 2018 v 2.1.2
* - Upgrade to ArduinoJSON 6.xx from ArduinoJSON 5.xx
*/ */