diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index a51f1db..a55b376 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -360,19 +360,26 @@ void initMqtt() { #else snprintf(mqtt_clientid, sizeof(mqtt_clientid), "%s", HOSTNAME); #endif + mqtt_clientid[sizeof(mqtt_clientid) - 1] = 0x00; snprintf(mqtt_will_topic, sizeof(mqtt_will_topic), "%s/config", HOSTNAME); + mqtt_will_topic[sizeof(mqtt_will_topic) - 1] = 0x00; snprintf(mqtt_intopic, sizeof(mqtt_intopic), "%s/in", HOSTNAME); + mqtt_intopic[sizeof(mqtt_intopic) - 1] = 0x00; snprintf(mqtt_outtopic, sizeof(mqtt_outtopic), "%s/out", HOSTNAME); + mqtt_outtopic[sizeof(mqtt_outtopic) - 1] = 0x00; #if defined(MQTT_HOME_ASSISTANT_SUPPORT) snprintf(mqtt_ha_config, sizeof(mqtt_ha_config), "homeassistant/light/%s/config", HOSTNAME); + mqtt_ha_config[sizeof(mqtt_ha_config) - 1] = 0x00; snprintf(mqtt_ha_state_in, sizeof(mqtt_ha_state_in), "home/%s_ha/state/in", HOSTNAME); + mqtt_ha_state_in[sizeof(mqtt_ha_state_in) - 1] = 0x00; snprintf(mqtt_ha_state_out, sizeof(mqtt_ha_state_out), "home/%s_ha/state/out", HOSTNAME); + mqtt_ha_state_out[sizeof(mqtt_ha_state_out) - 1] = 0x00; #endif if ((strlen(mqtt_host) != 0) && (mqtt_port != 0)) { #if ENABLE_MQTT == 0 DBG_OUTPUT_PORT.printf("MQTT active: %s:%d\r\n", mqtt_host, mqtt_port); mqtt_client->setServer(mqtt_host, mqtt_port); - mqtt_client->setCallback(mqtt_callback); + mqtt_client->setCallback(onMqttMessage); #endif #if ENABLE_MQTT == 1 DBG_OUTPUT_PORT.printf("AMQTT active: %s:%d\r\n", mqtt_host, mqtt_port); @@ -451,11 +458,12 @@ void setup() { //Strip Config #if ENABLE_STATE_SAVE == 1 (readConfigFS()) ? DBG_OUTPUT_PORT.println("WiFiManager config FS read success!"): DBG_OUTPUT_PORT.println("WiFiManager config FS Read failure!"); - delay(500); + delay(250); (readStateFS()) ? DBG_OUTPUT_PORT.println("Strip state config FS read Success!") : DBG_OUTPUT_PORT.println("Strip state config FS read failure!"); #endif #if ENABLE_STATE_SAVE == 0 (setConfByConfString(readEEPROM(0, 222)))? DBG_OUTPUT_PORT.println("WiFiManager config EEPROM read success!"): DBG_OUTPUT_PORT.println("WiFiManager config EEPROM Read failure!"); + delay(250); (setModeByStateString(readEEPROM(256, 66)))? DBG_OUTPUT_PORT.println("Strip state config EEPROM read Success!") : DBG_OUTPUT_PORT.println("Strip state config EEPROM read failure!"); #endif char tmp_strip_size[6], tmp_fxoptions[5], tmp_rgbOrder[5]; //needed tempararily for WiFiManager Settings @@ -562,7 +570,7 @@ void setup() { #else snprintf(last_conf, sizeof(last_conf), "CNF|%64s|%64s|%5d|%32s|%32s|%4d|%2d|%4s|%3d", HOSTNAME, "", "", "", "", WS2812FXStripSettings.stripSize, WS2812FXStripSettings.pin, WS2812FXStripSettings.RGBOrder, WS2812FXStripSettings.fxoptions); #endif - last_conf[sizeof(last_conf)]=0; + last_conf[sizeof(last_conf)] = 0x00; writeEEPROM(0, 222, last_conf); EEPROM.commit(); shouldSaveConfig = false; @@ -677,6 +685,7 @@ void setup() { #if defined(ENABLE_REMOTE) irrecv.enableIRIn(); // Start the receiver snprintf(last_state, sizeof(last_state), "STA|%2d|%3d|%3d|%3d|%3d|%3d|%3d|%3d|%3d|%3d|%3d|%3d|%3d|%3d|%3d|%3d", mode, ws2812fx_mode, ws2812fx_speed, brightness, main_color.red, main_color.green, main_color.blue, main_color.white, back_color.red, back_color.green, back_color.blue, back_color.white, xtra_color.red, xtra_color.green, xtra_color.blue,xtra_color.white); + last_state[sizeof(last_state)]= 0x00; #endif DBG_OUTPUT_PORT.println("finished Main Setup!"); @@ -865,6 +874,7 @@ void loop() { if(!settings_save_state.active()) settings_save_state.once(3, tickerSaveState); #endif snprintf(last_state, sizeof(last_state), "STA|%2d|%3d|%3d|%3d|%3d|%3d|%3d|%3d|%3d|%3d|%3d|%3d|%3d|%3d|%3d|%3d", prevmode, ws2812fx_mode, ws2812fx_speed, brightness, main_color.red, main_color.green, main_color.blue, main_color.white, back_color.red, back_color.green, back_color.blue, back_color.white, xtra_color.red, xtra_color.green, xtra_color.blue, xtra_color.white); + last_state[sizeof(last_state) - 1] = 0x00; } #if defined(ENABLE_MQTT) #if ENABLE_MQTT == 0 @@ -901,7 +911,7 @@ void loop() { #else snprintf(last_conf, sizeof(last_conf), "CNF|%64s|%64s|%5d|%32s|%32s|%4d|%2d|%4s|%3d", HOSTNAME, "", "", "", "", WS2812FXStripSettings.stripSize, WS2812FXStripSettings.pin, WS2812FXStripSettings.RGBOrder, WS2812FXStripSettings.fxoptions); #endif - last_conf[sizeof(last_conf)-1]= 0x00; + last_conf[sizeof(last_conf) - 1] = 0x00; writeEEPROM(0, 222, last_conf); EEPROM.commit(); shouldSaveConfig = false; diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index 41a0508..8083612 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -58,98 +58,103 @@ void convertColors() { } void getArgs() { - if (server.arg("rgb") != "") { - uint32_t rgb = (uint32_t) strtoul(server.arg("rgb").c_str(), NULL, 16); - main_color.white = ((rgb >> 24) & 0xFF); - main_color.red = ((rgb >> 16) & 0xFF); - main_color.green = ((rgb >> 8) & 0xFF); - main_color.blue = ((rgb >> 0) & 0xFF); - } else { - if ((server.arg("r") != "") && (server.arg("r").toInt() >= 0) && (server.arg("r").toInt() <= 255)) { - main_color.red = server.arg("r").toInt(); + + if (mode == SET_ALL || mode == SET_MODE || mode == SET_COLOR) { + if (server.arg("rgb") != "") { + uint32_t rgb = (uint32_t) strtoul(server.arg("rgb").c_str(), NULL, 16); + main_color.white = ((rgb >> 24) & 0xFF); + main_color.red = ((rgb >> 16) & 0xFF); + main_color.green = ((rgb >> 8) & 0xFF); + main_color.blue = ((rgb >> 0) & 0xFF); + } else { + if ((server.arg("r") != "") && (server.arg("r").toInt() >= 0) && (server.arg("r").toInt() <= 255)) { + main_color.red = server.arg("r").toInt(); + } + if ((server.arg("g") != "") && (server.arg("g").toInt() >= 0) && (server.arg("g").toInt() <= 255)) { + main_color.green = server.arg("g").toInt(); + } + if ((server.arg("b") != "") && (server.arg("b").toInt() >= 0) && (server.arg("b").toInt() <= 255)) { + main_color.blue = server.arg("b").toInt(); + } + if ((server.arg("w") != "") && (server.arg("w").toInt() >= 0) && (server.arg("w").toInt() <= 255)){ + main_color.white = server.arg("w").toInt(); + } } - if ((server.arg("g") != "") && (server.arg("g").toInt() >= 0) && (server.arg("g").toInt() <= 255)) { - main_color.green = server.arg("g").toInt(); + if (server.arg("rgb2") != "") { + uint32_t rgb2 = (uint32_t) strtoul(server.arg("rgb2").c_str(), NULL, 16); + back_color.white = ((rgb2 >> 24) & 0xFF); + back_color.red = ((rgb2 >> 16) & 0xFF); + back_color.green = ((rgb2 >> 8) & 0xFF); + back_color.blue = ((rgb2 >> 0) & 0xFF); + } else { + if ((server.arg("r2") != "") && (server.arg("r2").toInt() >= 0) && (server.arg("r2").toInt() <= 255)) { + back_color.red = server.arg("r2").toInt(); + } + if ((server.arg("g2") != "") && (server.arg("g2").toInt() >= 0) && (server.arg("g2").toInt() <= 255)) { + back_color.green = server.arg("g2").toInt(); + } + if ((server.arg("b2") != "") && (server.arg("b2").toInt() >= 0) && (server.arg("b2").toInt() <= 255)) { + back_color.blue = server.arg("b2").toInt(); + } + if ((server.arg("w2") != "") && (server.arg("w2").toInt() >= 0) && (server.arg("w2").toInt() <= 255)){ + back_color.white = server.arg("w2").toInt(); + } } - if ((server.arg("b") != "") && (server.arg("b").toInt() >= 0) && (server.arg("b").toInt() <= 255)) { - main_color.blue = server.arg("b").toInt(); + if (server.arg("rgb3") != "") { + uint32_t rgb3 = (uint32_t) strtoul(server.arg("rgb3").c_str(), NULL, 16); + xtra_color.white = ((rgb3 >> 24) & 0xFF); + xtra_color.red = ((rgb3 >> 16) & 0xFF); + xtra_color.green = ((rgb3 >> 8) & 0xFF); + xtra_color.blue = ((rgb3 >> 0) & 0xFF); + } else { + if ((server.arg("r3") != "") && (server.arg("r3").toInt() >= 0) && (server.arg("r3").toInt() <= 255)) { + xtra_color.red = server.arg("r3").toInt(); + } + if ((server.arg("g3") != "") && (server.arg("g3").toInt() >= 0) && (server.arg("g3").toInt() <= 255)) { + xtra_color.green = server.arg("g3").toInt(); + } + if ((server.arg("b3") != "") && (server.arg("b3").toInt() >= 0) && (server.arg("b3").toInt() <= 255)) { + xtra_color.blue = server.arg("b3").toInt(); + } + if ((server.arg("w3") != "") && (server.arg("w3").toInt() >= 0) && (server.arg("w3").toInt() <= 255)){ + xtra_color.white = server.arg("w3").toInt(); + } } - if ((server.arg("w") != "") && (server.arg("w").toInt() >= 0) && (server.arg("w").toInt() <= 255)){ - main_color.white = server.arg("w").toInt(); + main_color.red = constrain(main_color.red, 0, 255); + main_color.green = constrain(main_color.green, 0, 255); + main_color.blue = constrain(main_color.blue, 0, 255); + main_color.white = constrain(main_color.white, 0, 255); + back_color.red = constrain(back_color.red, 0, 255); + back_color.green = constrain(back_color.green, 0, 255); + back_color.blue = constrain(back_color.blue, 0, 255); + back_color.white = constrain(back_color.white, 0, 255); + xtra_color.red = constrain(xtra_color.red, 0, 255); + xtra_color.green = constrain(xtra_color.green, 0, 255); + xtra_color.blue = constrain(xtra_color.blue, 0, 255); + xtra_color.white = constrain(xtra_color.white, 0, 255); + convertColors(); + } + if (mode == SET_ALL || mode == SET_MODE || mode == SET_SPEED || mode == TV) { + if ((server.arg("s") != "") && (server.arg("s").toInt() >= 0) && (server.arg("s").toInt() <= 255)) { + ws2812fx_speed = constrain(server.arg("s").toInt(), 0, 255); } } - if (server.arg("rgb2") != "") { - uint32_t rgb2 = (uint32_t) strtoul(server.arg("rgb2").c_str(), NULL, 16); - back_color.white = ((rgb2 >> 24) & 0xFF); - back_color.red = ((rgb2 >> 16) & 0xFF); - back_color.green = ((rgb2 >> 8) & 0xFF); - back_color.blue = ((rgb2 >> 0) & 0xFF); - } else { - if ((server.arg("r2") != "") && (server.arg("r2").toInt() >= 0) && (server.arg("r2").toInt() <= 255)) { - back_color.red = constrain(server.arg("r2").toInt(), 0, 255); - } - if ((server.arg("g2") != "") && (server.arg("g2").toInt() >= 0) && (server.arg("g2").toInt() <= 255)) { - back_color.green = server.arg("g2").toInt(); - } - if ((server.arg("b2") != "") && (server.arg("b2").toInt() >= 0) && (server.arg("b2").toInt() <= 255)) { - back_color.blue = server.arg("b2").toInt(); - } - if ((server.arg("w2") != "") && (server.arg("w2").toInt() >= 0) && (server.arg("w2").toInt() <= 255)){ - back_color.white = server.arg("w2").toInt(); + if (mode == SET_ALL || mode == SET_MODE) { + if ((server.arg("m") != "") && (server.arg("m").toInt() >= 0) && (server.arg("m").toInt() <= strip->getModeCount())) { + ws2812fx_mode = constrain(server.arg("m").toInt(), 0, strip->getModeCount() - 1); } } - if (server.arg("rgb3") != "") { - uint32_t rgb3 = (uint32_t) strtoul(server.arg("rgb3").c_str(), NULL, 16); - xtra_color.white = ((rgb3 >> 24) & 0xFF); - xtra_color.red = ((rgb3 >> 16) & 0xFF); - xtra_color.green = ((rgb3 >> 8) & 0xFF); - xtra_color.blue = ((rgb3 >> 0) & 0xFF); - } else { - if ((server.arg("r3") != "") && (server.arg("r3").toInt() >= 0) && (server.arg("r3").toInt() <= 255)) { - xtra_color.red = server.arg("r3").toInt(); - } - if ((server.arg("g3") != "") && (server.arg("g3").toInt() >= 0) && (server.arg("g3").toInt() <= 255)) { - xtra_color.green = server.arg("g3").toInt(); - } - if ((server.arg("b3") != "") && (server.arg("b3").toInt() >= 0) && (server.arg("b3").toInt() <= 255)) { - xtra_color.blue = server.arg("b3").toInt(); - } - if ((server.arg("w3") != "") && (server.arg("w3").toInt() >= 0) && (server.arg("w3").toInt() <= 255)){ - xtra_color.white = server.arg("w3").toInt(); + if (mode == SET_ALL || mode == SET_MODE || mode == SET_BRIGHTNESS || mode == AUTO || mode == TV || mode == E131) { + if ((server.arg("c") != "") && (server.arg("c").toInt() >= 0) && (server.arg("c").toInt() <= 100)) { + brightness = constrain((int) server.arg("c").toInt() * 2.55, 0, 255); + } else if ((server.arg("p") != "") && (server.arg("p").toInt() >= 0) && (server.arg("p").toInt() <= 255)) { + brightness = constrain(server.arg("p").toInt(), 0, 255); } } - if ((server.arg("s") != "") && (server.arg("s").toInt() >= 0) && (server.arg("s").toInt() <= 255)) { - ws2812fx_speed = constrain(server.arg("s").toInt(), 0, 255); - } - - if ((server.arg("m") != "") && (server.arg("m").toInt() >= 0) && (server.arg("m").toInt() <= strip->getModeCount())) { - ws2812fx_mode = constrain(server.arg("m").toInt(), 0, strip->getModeCount() - 1); - } - - if ((server.arg("c") != "") && (server.arg("c").toInt() >= 0) && (server.arg("c").toInt() <= 100)) { - brightness = constrain((int) server.arg("c").toInt() * 2.55, 0, 255); - } else if ((server.arg("p") != "") && (server.arg("p").toInt() >= 0) && (server.arg("p").toInt() <= 255)) { - brightness = constrain(server.arg("p").toInt(), 0, 255); - } - - main_color.red = constrain(main_color.red, 0, 255); - main_color.green = constrain(main_color.green, 0, 255); - main_color.blue = constrain(main_color.blue, 0, 255); - main_color.white = constrain(main_color.white, 0, 255); - back_color.red = constrain(back_color.red, 0, 255); - back_color.green = constrain(back_color.green, 0, 255); - back_color.blue = constrain(back_color.blue, 0, 255); - back_color.white = constrain(back_color.white, 0, 255); - xtra_color.red = constrain(xtra_color.red, 0, 255); - xtra_color.green = constrain(xtra_color.green, 0, 255); - xtra_color.blue = constrain(xtra_color.blue, 0, 255); - xtra_color.white = constrain(xtra_color.white, 0, 255); - convertColors(); - DBG_OUTPUT_PORT.print("Get Args: "); - DBG_OUTPUT_PORT.println(listStatusJSON()); + + DBG_OUTPUT_PORT.printf("Get Args: %s\r\n", listStatusJSON()); } - uint16_t convertSpeed(uint8_t mcl_speed) { //long ws2812_speed = mcl_speed * 256; uint16_t ws2812_speed = 61760 * (exp(0.0002336 * mcl_speed) - exp(-0.03181 * mcl_speed)); @@ -279,11 +284,12 @@ void handleRangeDifferentColors(uint8_t * mypayload) { colorval[8] = 0x00; uint8_t rangebegin = atoi(startled); uint8_t rangeend = atoi(endled); - DBG_OUTPUT_PORT.printf("Setting RANGE from [%i] to [%i] as color [%s] \r\n", rangebegin, rangeend, colorval); + DBG_OUTPUT_PORT.printf("Setting RANGE from [%i] to [%i] as color [%s]\r\n", rangebegin, rangeend, colorval); while ( rangebegin <= rangeend ) { char rangeData[11]; snprintf(rangeData, sizeof(rangeData), "%02d%s", rangebegin, colorval); + rangeData[sizeof(rangeData) - 1] = 0x00; // Set one LED handleSetSingleLED((uint8_t*) rangeData, 0); rangebegin++; @@ -663,10 +669,7 @@ void getModesJSON() { // HTTP request handlers // *************************************************************************** void handleMinimalUpload() { - char temp[1500]; - - snprintf ( temp, 1500, - "\ + char message[] = "\ \
\