code cleanup, Bugfixes

Removed redundand code and moved it into function
Minor bugfixes
This commit is contained in:
BPoH_Voodoo
2018-04-26 07:59:53 +02:00
parent ca044ac3f1
commit c486066c1c
7 changed files with 343 additions and 481 deletions
-1
View File
@@ -98,4 +98,3 @@ class GY33_MCU {
};
#endif
+16 -38
View File
@@ -24,25 +24,7 @@
// ***************************************************************************
// Initialize Color Sensor
// ***************************************************************************
byte gammatable[256];
GY33_MCU tcs;
byte gamma8[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };
#endif
// OTA
@@ -123,9 +105,6 @@ NeoAnimationFX<NEOMETHOD> strip(neoStrip);
// https://github.com/kitesurfer1404/WS2812FX
#include "WS2812FX.h"
#ifdef RGBW
WS2812FX strip = WS2812FX(NUMLEDS, PIN, NEO_GRBW + NEO_KHZ800);
#else
@@ -277,13 +256,6 @@ void setup() {
#ifdef ENABLE_BUTTON_GY33
pinMode(BUTTON_GY33, INPUT_PULLUP);
for (int i=0; i<256; i++) {
float x = i;
x /= 255;
x = pow(x, 2.5);
x *= 255;
gammatable[i] = x;
}
if (tcs.begin()) {
DBG_OUTPUT_PORT.println("Found GY-33 sensor");
} else {
@@ -468,24 +440,24 @@ DBG_OUTPUT_PORT.println("Starting....");
// Configure MQTT
// ***************************************************************************
#ifdef ENABLE_MQTT
if (mqtt_host != "" && String(mqtt_port).toInt() > 0) {
if (mqtt_host != "" && atoi(mqtt_port) > 0) {
snprintf(mqtt_intopic, sizeof mqtt_intopic, "%s/in", HOSTNAME);
snprintf(mqtt_outtopic, sizeof mqtt_outtopic, "%s/out", HOSTNAME);
DBG_OUTPUT_PORT.printf("MQTT active: %s:%d\n", mqtt_host, String(mqtt_port).toInt());
mqtt_client.setServer(mqtt_host, String(mqtt_port).toInt());
mqtt_client.setServer(mqtt_host, atoi(mqtt_port));
mqtt_client.setCallback(mqtt_callback);
}
#endif
#ifdef ENABLE_AMQTT
if (mqtt_host != "" && String(mqtt_port).toInt() > 0) {
if (mqtt_host != "" && atoi(mqtt_port) > 0) {
amqttClient.onConnect(onMqttConnect);
amqttClient.onDisconnect(onMqttDisconnect);
amqttClient.onMessage(onMqttMessage);
amqttClient.setServer(mqtt_host, String(mqtt_port).toInt());
amqttClient.setCredentials(mqtt_user, mqtt_pass);
amqttClient.setServer(mqtt_host, atoi(mqtt_port));
if (mqtt_user != "" or mqtt_pass != "") amqttClient.setCredentials(mqtt_user, mqtt_pass);
amqttClient.setClientId(mqtt_clientid);
connectToMqtt();
@@ -582,6 +554,13 @@ DBG_OUTPUT_PORT.println("Starting....");
wifiManager.startConfigPortal(HOSTNAME);
});
server.on("/format_spiffs", []() {
DBG_OUTPUT_PORT.printf("/format_spiffs\n");
server.send(200, "text/plain", "Formatting SPIFFS ..." );
SPIFFS.format();
server.send(200, "text/plain", "Formatting SPIFFS complete" );
});
// ***************************************************************************
// Setup: SPIFFS Webserver handler
@@ -660,7 +639,7 @@ DBG_OUTPUT_PORT.println("Starting....");
exit_func = true;
#endif
mode = OFF;
getArgs();
//getArgs();
getStatusJSON();
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String("OK =off").c_str());
@@ -883,7 +862,9 @@ DBG_OUTPUT_PORT.println("Starting....");
#endif
#ifdef ENABLE_BUTTON_GY33
tcs.setConfig(MCU_LED_05, MCU_WHITE_ON);
tcs.setConfig(MCU_LED_06, MCU_WHITE_ON);
// delay(2000);
// tcs.setConfig(MCU_LED_OFF, MCU_WHITE_OFF);
#endif
}
@@ -959,7 +940,6 @@ void loop() {
}
#ifdef ENABLE_LEGACY_ANIMATIONS
if (mode == WIPE) {
strip.setColor(main_color.white, main_color.red, main_color.green, main_color.blue);
strip.setMode(FX_MODE_COLOR_WIPE);
mode = HOLD;
}
@@ -972,12 +952,10 @@ void loop() {
mode = HOLD;
}
if (mode == THEATERCHASE) {
strip.setColor(main_color.white, main_color.red, main_color.green, main_color.blue);
strip.setMode(FX_MODE_THEATER_CHASE);
mode = HOLD;
}
if (mode == TWINKLERANDOM) {
strip.setColor(main_color.white, main_color.red, main_color.green, main_color.blue);
strip.setMode(FX_MODE_TWINKLE_RANDOM);
mode = HOLD;
}
+10 -6
View File
@@ -6,8 +6,8 @@
#define NUMLEDS 194 // Number of leds in the strip
#define BUILTIN_LED 2 // ESP-12F has the built in LED on GPIO2, see https://github.com/esp8266/Arduino/issues/2192
#define BUTTON 14 // Input pin (14 / D5) for switching the LED strip on / off, connect this PIN to ground to trigger button.
#define BUTTON_GY33 12 // Input pin (12 / D6) for read color data with RGB sensor, connect this PIN to ground to trigger button.
#define RGBW
//#define BUTTON_GY33 12 // Input pin (12 / D6) for read color data with RGB sensor, connect this PIN to ground to trigger button.
#define RGBW // If defined, use RGBW Strips
const char HOSTNAME[] = "McLightingRGBW01"; // Friedly hostname
@@ -17,11 +17,13 @@ const char HOSTNAME[] = "McLightingRGBW01"; // Friedly hostname
//#define ENABLE_MQTT // If defined, enable MQTT client code, see: https://github.com/toblum/McLighting/wiki/MQTT-API
#define ENABLE_HOMEASSISTANT // If defined, enable Homeassistant integration, ENABLE_MQTT must be active
#define ENABLE_BUTTON // If defined, enable button handling code, see: https://github.com/toblum/McLighting/wiki/Button-control
//#define ENABLE_BUTTON_GY33 //
//#define ENABLE_BUTTON_GY33 // If defined, enable button handling code for GY-33 color sensor to scan color
//#define MQTT_HOME_ASSISTANT_SUPPORT // If defined, use AMQTT and select Tools -> IwIP Variant -> Higher Bandwidth
#define ENABLE_LEGACY_ANIMATIONS
#define ENABLE_STATE_SAVE_SPIFFS // If defined, saves state on SPIFFS
//#define ENABLE_STATE_SAVE_EEPROM // If defined, save state on reboot
#define ENABLE_MQTT_HOSTNAME_CHIPID // Uncomment/comment to add ESPChipID to end of MQTT hostname
#define DBG_OUTPUT_PORT Serial // Set debug output port
#if defined(USE_NEOANIMATIONFX) and defined(USE_WS2812FX)
#error "Cant have both NeoAnimationFX and WS2812FX enabled. Choose either one."
@@ -75,7 +77,11 @@ uint32_t autoParams[][4] = { // color, speed, mode, duration (seconds)
uint16_t color_temp = 327; // min is 154 and max is 500
#endif
const char mqtt_clientid[] = "NeoPixelsStrip"; // MQTT ClientID
#ifdef ENABLE_MQTT_HOSTNAME_CHIPID
const char* mqtt_clientid = String(String(HOSTNAME) + "-" + String(ESP.getChipId())).c_str(); // MQTT ClientID
#else
const char* mqtt_clientid = HOSTNAME; // MQTT ClientID
#endif
char mqtt_host[64] = "";
char mqtt_port[6] = "";
@@ -87,7 +93,6 @@ uint32_t autoParams[][4] = { // color, speed, mode, duration (seconds)
// ***************************************************************************
// Global variables / definitions
// ***************************************************************************
#define DBG_OUTPUT_PORT Serial // Set debug output port
// List of all color modes
#ifdef ENABLE_LEGACY_ANIMATIONS
@@ -158,4 +163,3 @@ LEDState main_color = { 0, 255, 0, 0}; // Store the "main color" of the strip u
byte KeyPressCount_gy33 = 0;
byte prevKeyState_gy33 = HIGH; // button is active low
#endif
+283 -403
View File
@@ -25,18 +25,17 @@ void getArgs() {
main_color.green = server.arg("g").toInt();
main_color.blue = server.arg("b").toInt();
}
if (server.arg("s") != "") {
ws2812fx_speed = constrain(server.arg("s").toInt(), 0, 255);
if (server.arg("s") == "") {
ws2812fx_speed = 196;
}
if (server.arg("m") != "") {
ws2812fx_mode = constrain(server.arg("m").toInt(), 0, strip.getModeCount() - 1);
}
if (server.arg("c").toInt() > 0) {
if (server.arg("c") != "") {
brightness = constrain((int) server.arg("c").toInt() * 2.55, 0, 255);
} else if (server.arg("p").toInt() > 0) {
} else if (server.arg("p") != "") {
brightness = constrain(server.arg("p").toInt(), 0, 255);
}
@@ -86,7 +85,6 @@ void handleSetMainColor(uint8_t * mypayload) {
main_color.red = ((rgb >> 16) & 0xFF);
main_color.green = ((rgb >> 8) & 0xFF);
main_color.blue = ((rgb ) & 0xFF);
strip.setColor(main_color.white, main_color.red, main_color.green, main_color.blue);
mode = SETCOLOR;
}
@@ -394,6 +392,265 @@ void handleAutoStop() {
strip.stop();
}
void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) {
// # ==> Set main color
if (payload[0] == '#') {
handleSetMainColor(payload);
if (mqtt == true) {
DBG_OUTPUT_PORT.print("MQTT: ");
} else {
DBG_OUTPUT_PORT.print("WS: ");
webSocket.sendTXT(num, "OK");
}
DBG_OUTPUT_PORT.printf("Set main color to: W: [%u] R: [%u] G: [%u] B: [%u]\n", main_color.white, main_color.red, main_color.green, main_color.blue);
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}
// ? ==> Set speed
if (payload[0] == '?') {
uint8_t d = (uint8_t) strtol((const char *) &payload[1], NULL, 10);
ws2812fx_speed = constrain(d, 0, 255);
strip.setSpeed(convertSpeed(ws2812fx_speed));
if (mqtt == true) {
DBG_OUTPUT_PORT.print("MQTT: ");
} else {
DBG_OUTPUT_PORT.print("WS: ");
webSocket.sendTXT(num, "OK");
}
DBG_OUTPUT_PORT.printf("Set speed to: [%u]\n", ws2812fx_speed);
#ifdef ENABLE_HOMEASSISTANT
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
}
// % ==> Set brightness
if (payload[0] == '%') {
uint8_t b = (uint8_t) strtol((const char *) &payload[1], NULL, 10);
brightness = constrain(b, 0, 255);
strip.setBrightness(brightness);
if (mqtt == true) {
DBG_OUTPUT_PORT.print("MQTT: ");
} else {
DBG_OUTPUT_PORT.print("WS: ");
webSocket.sendTXT(num, "OK");
}
DBG_OUTPUT_PORT.printf("WS: Set brightness to: [%u]\n", brightness);
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}
// * ==> Set main color and light all LEDs (Shortcut)
if (payload[0] == '*') {
handleSetAllMode(payload);
if (mqtt == true) {
DBG_OUTPUT_PORT.print("MQTT: ");
} else {
DBG_OUTPUT_PORT.print("WS: ");
webSocket.sendTXT(num, "OK");
}
DBG_OUTPUT_PORT.printf("Set main color and light all LEDs [%s]\n", payload);
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}
// ! ==> Set single LED in given color
if (payload[0] == '!') {
handleSetSingleLED(payload, 1);
if (mqtt == true) {
DBG_OUTPUT_PORT.print("MQTT: ");
} else {
DBG_OUTPUT_PORT.print("WS: ");
webSocket.sendTXT(num, "OK");
}
DBG_OUTPUT_PORT.printf("Set single LED in given color [%s]\n", payload);
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
}
// + ==> Set multiple LED in the given colors
if (payload[0] == '+') {
handleSetDifferentColors(payload);
if (mqtt == true) {
DBG_OUTPUT_PORT.print("MQTT: ");
} else {
DBG_OUTPUT_PORT.print("WS: ");
webSocket.sendTXT(num, "OK");
}
DBG_OUTPUT_PORT.printf("Set multiple LEDs in given color [%s]\n", payload);
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
}
// + ==> Set range of LEDs in the given color
if (payload[0] == 'R') {
handleRangeDifferentColors(payload);
if (mqtt == true) {
DBG_OUTPUT_PORT.print("MQTT: ");
} else {
DBG_OUTPUT_PORT.print("WS: ");
webSocket.sendTXT(num, "OK");
}
DBG_OUTPUT_PORT.printf("Set range of LEDs in given color [%s]\n", payload);
webSocket.sendTXT(num, "OK");
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
}
#ifdef ENABLE_LEGACY_ANIMATIONS
// = ==> Activate named mode
if (payload[0] == '=') {
// we get mode data
String str_mode = String((char *) &payload[0]);
handleSetNamedMode(str_mode);
if (mqtt == true) {
DBG_OUTPUT_PORT.print("MQTT: ");
} else {
DBG_OUTPUT_PORT.print("WS: ");
webSocket.sendTXT(num, "OK");
}
DBG_OUTPUT_PORT.printf("Activated mode [%u]!\n", mode);
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}
#endif
// $ ==> Get status Info.
if (payload[0] == '$') {
String json = listStatusJSON();
if (mqtt == true) {
DBG_OUTPUT_PORT.print("MQTT: ");
} else {
DBG_OUTPUT_PORT.print("WS: ");
webSocket.sendTXT(num, "OK");
}
DBG_OUTPUT_PORT.println("Get status info: " + json);
webSocket.sendTXT(num, json);
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, listStatusJSON());
#endif
#ifdef ENABLE_AMQTT
String liststat = (String) listStatusJSON();
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, liststat.c_str());
#endif
}
// ~ ==> Get WS2812 modes.
if (payload[0] == '~') {
String json = listModesJSON();
if (mqtt == true) {
DBG_OUTPUT_PORT.print("MQTT: ");
} else {
DBG_OUTPUT_PORT.print("WS: ");
webSocket.sendTXT(num, "OK");
}
DBG_OUTPUT_PORT.println("Get WS2812 modes.");
DBG_OUTPUT_PORT.println(json);
#ifdef ENABLE_MQTT
DBG_OUTPUT_PORT.printf("Error: Not implemented. Message too large for pubsubclient.");
mqtt_client.publish(mqtt_outtopic, "ERROR: Not implemented. Message too large for pubsubclient.");
//String json_modes = listModesJSON();
//DBG_OUTPUT_PORT.printf(json_modes.c_str());
//int res = mqtt_client.publish(mqtt_outtopic, json_modes.c_str(), json_modes.length());
//DBG_OUTPUT_PORT.printf("Result: %d / %d", res, json_modes.length());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String("ERROR: Not implemented. Message too large for AsyncMQTT.").c_str());
#endif
}
// / ==> Set WS2812 mode.
// TODO: Fix this, doesn't return anything. Too long?
// Hint: https://github.com/knolleary/pubsubclient/issues/110
if (payload[0] == '/') {
handleSetWS2812FXMode(payload);
if (mqtt == true) {
DBG_OUTPUT_PORT.print("MQTT: ");
} else {
DBG_OUTPUT_PORT.print("WS: ");
webSocket.sendTXT(num, "OK");
}
DBG_OUTPUT_PORT.printf("Set WS2812 mode: [%s]\n", payload);
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}
}
// ***************************************************************************
// WS request handlers
// ***************************************************************************
@@ -415,201 +672,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
case WStype_TEXT:
DBG_OUTPUT_PORT.printf("WS: [%u] get Text: %s\n", num, payload);
// # ==> Set main color
if (payload[0] == '#') {
handleSetMainColor(payload);
DBG_OUTPUT_PORT.printf("Set main color to: R: [%u] G: [%u] B: [%u] W: [%u]\n", main_color.red, main_color.green, main_color.blue, main_color.white);
webSocket.sendTXT(num, "OK");
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}
// ? ==> Set speed
if (payload[0] == '?') {
uint8_t d = (uint8_t) strtol((const char *) &payload[1], NULL, 10);
ws2812fx_speed = constrain(d, 0, 255);
strip.setSpeed(convertSpeed(ws2812fx_speed));
DBG_OUTPUT_PORT.printf("WS: Set speed to: [%u]\n", ws2812fx_speed);
webSocket.sendTXT(num, "OK");
#ifdef ENABLE_HOMEASSISTANT
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
}
// % ==> Set brightness
if (payload[0] == '%') {
uint8_t b = (uint8_t) strtol((const char *) &payload[1], NULL, 10);
brightness = ((b >> 0) & 0xFF);
DBG_OUTPUT_PORT.printf("WS: Set brightness to: [%u]\n", brightness);
strip.setBrightness(brightness);
webSocket.sendTXT(num, "OK");
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}
// * ==> Set main color and light all LEDs (Shortcut)
if (payload[0] == '*') {
handleSetAllMode(payload);
webSocket.sendTXT(num, "OK");
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}
// ! ==> Set single LED in given color
if (payload[0] == '!') {
handleSetSingleLED(payload, 1);
webSocket.sendTXT(num, "OK");
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
}
// + ==> Set multiple LED in the given colors
if (payload[0] == '+') {
handleSetDifferentColors(payload);
webSocket.sendTXT(num, "OK");
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
}
// + ==> Set range of LEDs in the given color
if (payload[0] == 'R') {
handleRangeDifferentColors(payload);
webSocket.sendTXT(num, "OK");
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
}
#ifdef ENABLE_LEGACY_ANIMATIONS
// = ==> Activate named mode
if (payload[0] == '=') {
// we get mode data
String str_mode = String((char *) &payload[0]);
handleSetNamedMode(str_mode);
DBG_OUTPUT_PORT.printf("Activated mode [%u]!\n", mode);
webSocket.sendTXT(num, "OK");
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}
#endif
// $ ==> Get status Info.
if (payload[0] == '$') {
DBG_OUTPUT_PORT.printf("Get status info.");
String json = listStatusJSON();
DBG_OUTPUT_PORT.println(json);
webSocket.sendTXT(num, json);
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, listStatusJSON());
#endif
#ifdef ENABLE_AMQTT
String liststat = (String) listStatusJSON();
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, liststat.c_str());
#endif
}
// ~ ==> Get WS2812 modes.
if (payload[0] == '~') {
DBG_OUTPUT_PORT.printf("Get WS2812 modes.");
String json = listModesJSON();
DBG_OUTPUT_PORT.println(json);
webSocket.sendTXT(num, json);
#ifdef ENABLE_MQTT
DBG_OUTPUT_PORT.printf("Error: Not implemented. Message too large for pubsubclient.");
mqtt_client.publish(mqtt_outtopic, "ERROR: Not implemented. Message too large for pubsubclient.");
//String json_modes = listModesJSON();
//DBG_OUTPUT_PORT.printf(json_modes.c_str());
//int res = mqtt_client.publish(mqtt_outtopic, json_modes.c_str(), json_modes.length());
//DBG_OUTPUT_PORT.printf("Result: %d / %d", res, json_modes.length());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String("ERROR: Not implemented. Message too large for AsyncMQTT.").c_str());
#endif
}
// / ==> Set WS2812 mode.
if (payload[0] == '/') {
handleSetWS2812FXMode(payload);
webSocket.sendTXT(num, "OK");
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}
checkpayload(payload, false, num);
// start auto cycling
if (strcmp((char *)payload, "start") == 0 ) {
@@ -848,188 +911,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
#endif
#endif
// # ==> Set main color
if (payload[0] == '#') {
handleSetMainColor(payload);
DBG_OUTPUT_PORT.printf("MQTT: Set main color to [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue);
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}
// ? ==> Set speed
if (payload[0] == '?') {
uint8_t d = (uint8_t) strtol((const char *) &payload[1], NULL, 10);
ws2812fx_speed = constrain(d, 0, 255);
strip.setSpeed(convertSpeed(ws2812fx_speed));
DBG_OUTPUT_PORT.printf("MQTT: Set speed to [%u]\n", ws2812fx_speed);
#ifdef ENABLE_HOMEASSISTANT
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
}
// % ==> Set brightness
if (payload[0] == '%') {
uint8_t b = (uint8_t) strtol((const char *) &payload[1], NULL, 10);
brightness = constrain(b, 0, 255);
strip.setBrightness(brightness);
DBG_OUTPUT_PORT.printf("MQTT: Set brightness to [%u]\n", brightness);
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}
// * ==> Set main color and light all LEDs (Shortcut)
if (payload[0] == '*') {
handleSetAllMode(payload);
DBG_OUTPUT_PORT.printf("MQTT: Set main color and light all LEDs [%s]\n", payload);
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}
// ! ==> Set single LED in given color
if (payload[0] == '!') {
handleSetSingleLED(payload, 1);
DBG_OUTPUT_PORT.printf("MQTT: Set single LED in given color [%s]\n", payload);
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
}
// + ==> Set multiple LED in the given colors
if (payload[0] == '+') {
handleSetDifferentColors(payload);
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
}
// R ==> Set range of LEDs in the given colors
if (payload[0] == 'R') {
handleRangeDifferentColors(payload);
DBG_OUTPUT_PORT.printf("MQTT: Set range of LEDS to single color: [%s]\n", payload);
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
}
#ifdef ENABLE_LEGACY_ANIMATIONS
// = ==> Activate named mode
if (payload[0] == '=') {
String str_mode = String((char *) &payload[0]);
handleSetNamedMode(str_mode);
DBG_OUTPUT_PORT.printf("MQTT: Activate named mode [%s]\n", payload);
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}
#endif
// $ ==> Get status Info.
if (payload[0] == '$') {
DBG_OUTPUT_PORT.printf("MQTT: Get status info.\n");
DBG_OUTPUT_PORT.println("MQTT: Out: " + String(listStatusJSON()));
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, listStatusJSON());
#endif
#ifdef ENABLE_AMQTT
String liststat = (String) listStatusJSON();
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, liststat.c_str());
#endif
}
// ~ ==> Get WS2812 modes.
// TODO: Fix this, doesn't return anything. Too long?
// Hint: https://github.com/knolleary/pubsubclient/issues/110
if (payload[0] == '~') {
DBG_OUTPUT_PORT.printf("MQTT: Get WS2812 modes.\n");
#ifdef ENABLE_MQTT
DBG_OUTPUT_PORT.printf("Error: Not implemented. Message too large for pubsubclient.");
mqtt_client.publish(mqtt_outtopic, "ERROR: Not implemented. Message too large for pubsubclient.");
//String json_modes = listModesJSON();
//DBG_OUTPUT_PORT.printf(json_modes.c_str());
//int res = mqtt_client.publish(mqtt_outtopic, json_modes.c_str(), json_modes.length());
//DBG_OUTPUT_PORT.printf("Result: %d / %d", res, json_modes.length());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String("ERROR: Not implemented. Message too large for AsyncMQTT.").c_str());
#endif
}
// / ==> Set WS2812 mode.
if (payload[0] == '/') {
handleSetWS2812FXMode(payload);
DBG_OUTPUT_PORT.printf("MQTT: Set WS2812 mode [%s]\n", payload);
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String(String("OK ") + String((char *)payload)).c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}
checkpayload(payload, true);
#ifdef ENABLE_HOMEASSISTANT
}
@@ -1200,6 +1082,16 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String("OK =static white").c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String("OK =static white").c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
} else {
mode = OFF;
buttonState = false;
@@ -1291,48 +1183,36 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
#ifdef ENABLE_BUTTON_GY33
void shortKeyPress_gy33() {
DBG_OUTPUT_PORT.printf("Short GY-33 button press\n");
if (buttonState == false) {
// tcs.setConfig(MCU_LED_04, MCU_WHITE_OFF);
// delay(500);
uint16_t red, green, blue, cl, ct, lux;
tcs.getRawData(&red, &green, &blue, &cl, &lux, &ct);
DBG_OUTPUT_PORT.printf("Raw Colors: R: [%d] G: [%d] B: [%d] Clear: [%d] Lux: [%d] Colortemp: [%d]\n", (int)red, (int)green, (int)blue, (int)cl, (int)lux, (int)ct);
uint8_t r, g, b, col, conf;
tcs.getData(&r, &g, &b, &col, &conf);
DBG_OUTPUT_PORT.printf("Colors: R: [%d] G: [%d] B: [%d] Color: [%d] Conf: [%d]\n", (int)r, (int)g, (int)b, (int)col, (int)conf);
char newmode[38];
sprintf(newmode, "STA| 1| 0|245|196|0|%3d|%3d|%3d", (int)r, (int)g, (int)b);
DBG_OUTPUT_PORT.println(newmode);
// setModeByStateString(newmode);
main_color.white = 0; main_color.red = gammatable[(int)r]; main_color.green = gammatable[(int)g]; main_color.blue = gammatable[(int)b];
mode = HOLD;
strip.setMode(0);
//strip.setBrightness(196);
strip.setColor(main_color.white, main_color.red, main_color.green, main_color.blue);
buttonState = false;
main_color.white = 0; main_color.red = (pow((r/255.0), 2.5)*255); main_color.green = (pow((g/255.0), 2.5)*255); main_color.blue = (pow((b/255.0), 2.5)*255);
ws2812fx_mode = 0;
mode = SET_MODE;
buttonState = true;
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String("OK =static GY-33").c_str());
#endif
} else {
mode = OFF;
buttonState = false;
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String("OK =off").c_str());
#endif
#ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String("OK =off").c_str());
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, String("OK =static GY-33").c_str());
#endif
#ifdef ENABLE_HOMEASSISTANT
stateOn = false;
if(!ha_send_data.active()) ha_send_data.once(5, tickerSendState);
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
}
// tcs.setConfig(MCU_LED_OFF, MCU_WHITE_OFF);
}
// called when button is kept pressed for less than 2 seconds
void mediumKeyPress_gy33() {
tcs.setConfig(MCU_LED_02, MCU_WHITE_OFF);
tcs.setConfig(MCU_LED_07, MCU_WHITE_ON);
}
// called when button is kept pressed for 2 seconds or more
+2 -1
View File
@@ -29,7 +29,7 @@ There are some informations in the [Wiki](https://github.com/toblum/McLighting/w
Update 17.02.2018:
User @debsahu contributed code for integration with homeassistant. It's currently in a separate branch (https://github.com/toblum/McLighting/tree/feature/ha_integration). If you're using Homeassistant, please try it out and give feedback.
User @FabLab-Luenn created a version of McLighting (https://github.com/FabLab-Luenen/McLighting) for 6812 and other RGBW strips. Give it a try, if you own such strips.
User @FabLab-Luenen created a version of McLighting (https://github.com/FabLab-Luenen/McLighting) for 6812 and other RGBW strips. Give it a try, if you own such strips.
A thank you goes to all contributors.
Update 12. / 15.02.2018:
@@ -151,6 +151,7 @@ I hope I didn't miss any sources and mentioned every author. In case I forgot so
- [x] Make a set of NodeRed nodes.
- [ ] Multiple buttons/GPIO Inputs. [Issue](https://github.com/toblum/McLighting/issues/119)
- [ ] Music visualizer / Bring back ArtNet [Issue](https://github.com/toblum/McLighting/issues/111)
- [ ] Display version and parameters (Number of LEDs, definition settings, ..) in the web UI [Issue](https://github.com/toblum/McLighting/issues/150)
## Licence
+3 -3
View File
@@ -215,7 +215,7 @@ $(function(){
console.log("modes", data);
var modes_html = "";
modes_html += '<div class="col s12 m6 l6 btn_grid">';
if (mode == 3) {
if (mode == "3") {
modes_html += '<a class="btn waves-effect waves-light btn_mode_static red" name="action" data-mode="off">OFF';
} else {
modes_html += '<a class="btn waves-effect waves-light btn_mode_static blue" name="action" data-mode="off">OFF';
@@ -224,7 +224,7 @@ $(function(){
modes_html += '</a>';
modes_html += '</div>'
modes_html += '<div class="col s12 m6 l6 btn_grid">';
if (mode == 4) {
if (mode == "4") {
modes_html += '<a class="btn waves-effect waves-light btn_mode_static red" name="action" data-mode="tv">TV';
} else {
modes_html += '<a class="btn waves-effect waves-light btn_mode_static blue" name="action" data-mode="tv">TV';
@@ -239,7 +239,7 @@ $(function(){
data.forEach(function(current_mode){
if (current_mode.mode !== undefined) {
modes_html += '<div class="col s12 m6 l6 btn_grid">';
if (mode == 1 && current_mode.mode == ws2812fx_mode) {
if (mode == "1" && current_mode.mode == ws2812fx_mode) {
modes_html += '<a class="btn waves-effect waves-light btn_mode red" name="action" data-mode="' + current_mode.mode + '">(' + current_mode.mode +') '+ current_mode.name;
} else {
+3 -3
View File
@@ -61,7 +61,7 @@ $(function(){
console.log("modes", data);
var modes_html = "";
modes_html += '<div class="col s12 m6 l6 btn_grid">';
if (mode == 2) {
if (mode == "3") {
modes_html += '<a class="btn waves-effect waves-light btn_mode_static red" name="action" data-mode="off">OFF';
} else {
modes_html += '<a class="btn waves-effect waves-light btn_mode_static blue" name="action" data-mode="off">OFF';
@@ -70,7 +70,7 @@ $(function(){
modes_html += '</a>';
modes_html += '</div>'
modes_html += '<div class="col s12 m6 l6 btn_grid">';
if (mode == 10) {
if (mode == "4") {
modes_html += '<a class="btn waves-effect waves-light btn_mode_static red" name="action" data-mode="tv">TV';
} else {
modes_html += '<a class="btn waves-effect waves-light btn_mode_static blue" name="action" data-mode="tv">TV';
@@ -85,7 +85,7 @@ $(function(){
data.forEach(function(current_mode){
if (current_mode.mode !== undefined) {
modes_html += '<div class="col s12 m6 l6 btn_grid">';
if (mode == 1 && current_mode.mode == ws2812fx_mode) {
if (mode == "1" && current_mode.mode == ws2812fx_mode) {
modes_html += '<a class="btn waves-effect waves-light btn_mode red" name="action" data-mode="' + current_mode.mode + '">(' + current_mode.mode +') '+ current_mode.name;
} else {