ArduinoJSON replies and strip.trigger() added

* Use ArduinoJSON to send JSON replies
* Add strip.trigger() according to
https://github.com/toblum/McLighting/issues/166
This commit is contained in:
debsahu 2018-05-11 17:21:12 -04:00
parent d842c6293f
commit b104178b56
4 changed files with 74 additions and 24 deletions

View file

@ -523,11 +523,13 @@ void setup() {
#else #else
json["home_assistant"] = "OFF"; json["home_assistant"] = "OFF";
#endif #endif
char buffer[json.measureLength() + 1]; //char buffer[json.measureLength() + 1];
json.printTo(buffer, sizeof(buffer)); //json.printTo(buffer, sizeof(buffer));
String json_str;
json.printTo(json_str);
server.sendHeader("Access-Control-Allow-Origin", "*"); server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "application/json", String(buffer)); server.send(200, "application/json", json_str);
//json = String(); //json_str = String();
}); });
@ -914,6 +916,7 @@ void loop() {
if (mode == SET_MODE) { if (mode == SET_MODE) {
DBG_OUTPUT_PORT.printf("SET_MODE: %d %d\n", ws2812fx_mode, mode); DBG_OUTPUT_PORT.printf("SET_MODE: %d %d\n", ws2812fx_mode, mode);
strip.setMode(ws2812fx_mode); strip.setMode(ws2812fx_mode);
strip.trigger();
prevmode = SET_MODE; prevmode = SET_MODE;
mode = SETCOLOR; mode = SETCOLOR;
} }
@ -923,14 +926,17 @@ void loop() {
} }
if (mode == SETCOLOR) { if (mode == SETCOLOR) {
strip.setColor(main_color.red, main_color.green, main_color.blue); strip.setColor(main_color.red, main_color.green, main_color.blue);
strip.trigger();
mode = (prevmode == SET_MODE) ? SETSPEED : HOLD; mode = (prevmode == SET_MODE) ? SETSPEED : HOLD;
} }
if (mode == SETSPEED) { if (mode == SETSPEED) {
strip.setSpeed(convertSpeed(ws2812fx_speed)); strip.setSpeed(convertSpeed(ws2812fx_speed));
strip.trigger();
mode = (prevmode == SET_MODE) ? BRIGHTNESS : HOLD; mode = (prevmode == SET_MODE) ? BRIGHTNESS : HOLD;
} }
if (mode == BRIGHTNESS) { if (mode == BRIGHTNESS) {
strip.setBrightness(brightness); strip.setBrightness(brightness);
strip.trigger();
if (prevmode == SET_MODE) prevmode == HOLD; if (prevmode == SET_MODE) prevmode == HOLD;
mode = HOLD; mode = HOLD;
} }
@ -938,28 +944,34 @@ void loop() {
if (mode == WIPE) { if (mode == WIPE) {
strip.setColor(main_color.red, main_color.green, main_color.blue); strip.setColor(main_color.red, main_color.green, main_color.blue);
strip.setMode(FX_MODE_COLOR_WIPE); strip.setMode(FX_MODE_COLOR_WIPE);
strip.trigger();
mode = HOLD; mode = HOLD;
} }
if (mode == RAINBOW) { if (mode == RAINBOW) {
strip.setMode(FX_MODE_RAINBOW); strip.setMode(FX_MODE_RAINBOW);
strip.trigger();
mode = HOLD; mode = HOLD;
} }
if (mode == RAINBOWCYCLE) { if (mode == RAINBOWCYCLE) {
strip.setMode(FX_MODE_RAINBOW_CYCLE); strip.setMode(FX_MODE_RAINBOW_CYCLE);
strip.trigger();
mode = HOLD; mode = HOLD;
} }
if (mode == THEATERCHASE) { if (mode == THEATERCHASE) {
strip.setColor(main_color.red, main_color.green, main_color.blue); strip.setColor(main_color.red, main_color.green, main_color.blue);
strip.setMode(FX_MODE_THEATER_CHASE); strip.setMode(FX_MODE_THEATER_CHASE);
strip.trigger();
mode = HOLD; mode = HOLD;
} }
if (mode == TWINKLERANDOM) { if (mode == TWINKLERANDOM) {
strip.setColor(main_color.red, main_color.green, main_color.blue); strip.setColor(main_color.red, main_color.green, main_color.blue);
strip.setMode(FX_MODE_TWINKLE_RANDOM); strip.setMode(FX_MODE_TWINKLE_RANDOM);
strip.trigger();
mode = HOLD; mode = HOLD;
} }
if (mode == THEATERCHASERAINBOW) { if (mode == THEATERCHASERAINBOW) {
strip.setMode(FX_MODE_THEATER_CHASE_RAINBOW); strip.setMode(FX_MODE_THEATER_CHASE_RAINBOW);
strip.trigger();
mode = HOLD; mode = HOLD;
} }
#endif #endif

View file

@ -278,14 +278,35 @@ void handleSetWS2812FXMode(uint8_t * mypayload) {
ws2812fx_mode = constrain(ws2812fx_mode_tmp, 0, strip.getModeCount() - 1); ws2812fx_mode = constrain(ws2812fx_mode_tmp, 0, strip.getModeCount() - 1);
} }
char* listStatusJSON() { String listStatusJSON(void) {
char json[255]; //char json[255];
char modeName[30]; //char modeName[30];
uint8_t tmp_mode = (mode == SET_MODE) ? (uint8_t) ws2812fx_mode : strip.getMode(); 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 //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); //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; return json;
} }
@ -294,17 +315,31 @@ void getStatusJSON() {
server.send ( 200, "application/json", listStatusJSON() ); server.send ( 200, "application/json", listStatusJSON() );
} }
String listModesJSON() { String listModesJSON(void) {
String modes = "["; // 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++) { for (uint8_t i = 0; i < strip.getModeCount(); i++) {
modes += "{\"mode\":"; JsonObject& object = json.createNestedObject();
modes += i; object["mode"] = i;
modes += ", \"name\":\""; object["name"] = strip.getModeName(i);
modes += strip.getModeName(i);
modes += "\"},";
} }
modes += "{}]"; JsonObject& object = json.createNestedObject();
return modes;
String json_str;
json.printTo(json_str);
return json_str;
} }
void getModesJSON() { void getModesJSON() {
@ -573,11 +608,10 @@ void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) {
if (mqtt == true) { if (mqtt == true) {
DBG_OUTPUT_PORT.print("MQTT: "); DBG_OUTPUT_PORT.print("MQTT: ");
#ifdef ENABLE_MQTT #ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, listStatusJSON()); mqtt_client.publish(mqtt_outtopic, json.c_str());
#endif #endif
#ifdef ENABLE_AMQTT #ifdef ENABLE_AMQTT
String liststat = (String) listStatusJSON(); amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, json.c_str());
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, liststat.c_str());
#endif #endif
} else { } else {
DBG_OUTPUT_PORT.print("WS: "); DBG_OUTPUT_PORT.print("WS: ");

View file

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

View file

@ -1,5 +1,9 @@
/* /*
* 07 May 2018 * 07 May 2018 v 2.1.0
* - Start of versioning v2.1, added version support * - Start of versioning v2.1, added version support
* - '/esp_status' returns lot more info * - '/esp_status' returns lot more info
*
* 11 May 2018 v 2.1.1
* - Use ArduinoJSON to send JSON replies
* - Add strip.trigger()
*/ */