From b104178b564be1f0cbea3cb81dc060f1d813aedd Mon Sep 17 00:00:00 2001 From: debsahu Date: Fri, 11 May 2018 17:21:12 -0400 Subject: [PATCH] ArduinoJSON replies and strip.trigger() added * Use ArduinoJSON to send JSON replies * Add strip.trigger() according to https://github.com/toblum/McLighting/issues/166 --- Arduino/McLighting/McLighting.ino | 20 ++++++-- Arduino/McLighting/request_handlers.h | 70 ++++++++++++++++++++------- Arduino/McLighting/version.h | 2 +- Arduino/McLighting/version_info.ino | 6 ++- 4 files changed, 74 insertions(+), 24 deletions(-) diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index 4123683..47a8548 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -523,11 +523,13 @@ void setup() { #else json["home_assistant"] = "OFF"; #endif - char buffer[json.measureLength() + 1]; - json.printTo(buffer, sizeof(buffer)); + //char buffer[json.measureLength() + 1]; + //json.printTo(buffer, sizeof(buffer)); + String json_str; + json.printTo(json_str); server.sendHeader("Access-Control-Allow-Origin", "*"); - server.send(200, "application/json", String(buffer)); - //json = String(); + server.send(200, "application/json", json_str); + //json_str = String(); }); @@ -914,6 +916,7 @@ void loop() { if (mode == SET_MODE) { DBG_OUTPUT_PORT.printf("SET_MODE: %d %d\n", ws2812fx_mode, mode); strip.setMode(ws2812fx_mode); + strip.trigger(); prevmode = SET_MODE; mode = SETCOLOR; } @@ -923,14 +926,17 @@ void loop() { } if (mode == SETCOLOR) { strip.setColor(main_color.red, main_color.green, main_color.blue); + strip.trigger(); mode = (prevmode == SET_MODE) ? SETSPEED : HOLD; } if (mode == SETSPEED) { strip.setSpeed(convertSpeed(ws2812fx_speed)); + strip.trigger(); mode = (prevmode == SET_MODE) ? BRIGHTNESS : HOLD; } if (mode == BRIGHTNESS) { strip.setBrightness(brightness); + strip.trigger(); if (prevmode == SET_MODE) prevmode == HOLD; mode = HOLD; } @@ -938,28 +944,34 @@ void loop() { if (mode == WIPE) { strip.setColor(main_color.red, main_color.green, main_color.blue); strip.setMode(FX_MODE_COLOR_WIPE); + strip.trigger(); mode = HOLD; } if (mode == RAINBOW) { strip.setMode(FX_MODE_RAINBOW); + strip.trigger(); mode = HOLD; } if (mode == RAINBOWCYCLE) { strip.setMode(FX_MODE_RAINBOW_CYCLE); + strip.trigger(); mode = HOLD; } if (mode == THEATERCHASE) { strip.setColor(main_color.red, main_color.green, main_color.blue); strip.setMode(FX_MODE_THEATER_CHASE); + strip.trigger(); mode = HOLD; } if (mode == TWINKLERANDOM) { strip.setColor(main_color.red, main_color.green, main_color.blue); strip.setMode(FX_MODE_TWINKLE_RANDOM); + strip.trigger(); mode = HOLD; } if (mode == THEATERCHASERAINBOW) { strip.setMode(FX_MODE_THEATER_CHASE_RAINBOW); + strip.trigger(); mode = HOLD; } #endif diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index aa96820..fc6a85a 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -278,14 +278,35 @@ void handleSetWS2812FXMode(uint8_t * mypayload) { ws2812fx_mode = constrain(ws2812fx_mode_tmp, 0, strip.getModeCount() - 1); } -char* listStatusJSON() { - char json[255]; - char modeName[30]; +String listStatusJSON(void) { + //char json[255]; + //char modeName[30]; uint8_t tmp_mode = (mode == SET_MODE) ? (uint8_t) ws2812fx_mode : strip.getMode(); - strncpy_P(modeName, (PGM_P)strip.getModeName(tmp_mode), sizeof(modeName)); // copy from progmem - snprintf(json, sizeof(json), "{\"mode\":%d, \"ws2812fx_mode\":%d, \"ws2812fx_mode_name\":\"%s\", \"speed\":%d, \"brightness\":%d, \"color\":[%d, %d, %d]}", - mode, tmp_mode, modeName, ws2812fx_speed, brightness, main_color.red, main_color.green, main_color.blue); + //strncpy_P(modeName, (PGM_P)strip.getModeName(tmp_mode), sizeof(modeName)); // copy from progmem + + //snprintf(json, sizeof(json), "{\"mode\":%d, \"ws2812fx_mode\":%d, \"ws2812fx_mode_name\":\"%s\", \"speed\":%d, \"brightness\":%d, \"color\":[%d, %d, %d]}", + // 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); + DynamicJsonBuffer jsonBuffer(bufferSize); + JsonObject& root = jsonBuffer.createObject(); + root["mode"] = (uint8_t) mode; + root["ws2812fx_mode"] = tmp_mode; + root["ws2812fx_mode_name"] = strip.getModeName(tmp_mode); + root["speed"] = ws2812fx_speed; + root["brightness"] = brightness; + JsonArray& color = root.createNestedArray("color"); + color.add(main_color.red); + color.add(main_color.green); + color.add(main_color.blue); + +// char* json = (char*) malloc(root.measureLength() + 1); +// root.printTo(json, sizeof(json)); + + String json; + root.printTo(json); + return json; } @@ -294,17 +315,31 @@ void getStatusJSON() { server.send ( 200, "application/json", listStatusJSON() ); } -String listModesJSON() { - String modes = "["; +String listModesJSON(void) { +// String modes = "["; +// for (uint8_t i = 0; i < strip.getModeCount(); i++) { +// modes += "{\"mode\":"; +// modes += i; +// modes += ", \"name\":\""; +// modes += strip.getModeName(i); +// modes += "\"},"; +// } +// modes += "{}]"; +// return modes; + + const size_t bufferSize = JSON_ARRAY_SIZE(strip.getModeCount()+1) + strip.getModeCount()*JSON_OBJECT_SIZE(2); + DynamicJsonBuffer jsonBuffer(bufferSize); + JsonArray& json = jsonBuffer.createArray(); for (uint8_t i = 0; i < strip.getModeCount(); i++) { - modes += "{\"mode\":"; - modes += i; - modes += ", \"name\":\""; - modes += strip.getModeName(i); - modes += "\"},"; + JsonObject& object = json.createNestedObject(); + object["mode"] = i; + object["name"] = strip.getModeName(i); } - modes += "{}]"; - return modes; + JsonObject& object = json.createNestedObject(); + + String json_str; + json.printTo(json_str); + return json_str; } void getModesJSON() { @@ -573,11 +608,10 @@ void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) { if (mqtt == true) { DBG_OUTPUT_PORT.print("MQTT: "); #ifdef ENABLE_MQTT - mqtt_client.publish(mqtt_outtopic, listStatusJSON()); + mqtt_client.publish(mqtt_outtopic, json.c_str()); #endif #ifdef ENABLE_AMQTT - String liststat = (String) listStatusJSON(); - amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, liststat.c_str()); + amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, json.c_str()); #endif } else { DBG_OUTPUT_PORT.print("WS: "); diff --git a/Arduino/McLighting/version.h b/Arduino/McLighting/version.h index ea531a0..db35e16 100644 --- a/Arduino/McLighting/version.h +++ b/Arduino/McLighting/version.h @@ -1 +1 @@ -#define SKETCH_VERSION "2.1.0" +#define SKETCH_VERSION "2.1.1" diff --git a/Arduino/McLighting/version_info.ino b/Arduino/McLighting/version_info.ino index a2efa67..f381e99 100644 --- a/Arduino/McLighting/version_info.ino +++ b/Arduino/McLighting/version_info.ino @@ -1,5 +1,9 @@ /* - * 07 May 2018 + * 07 May 2018 v 2.1.0 * - Start of versioning v2.1, added version support * - '/esp_status' returns lot more info + * + * 11 May 2018 v 2.1.1 + * - Use ArduinoJSON to send JSON replies + * - Add strip.trigger() */