Merge pull request #105 from debsahu/master

MQTT and WS changes send feedback to HA correctly without hangups.
This commit is contained in:
Deb 2018-03-13 21:04:08 -04:00 committed by GitHub
commit b0d7be7514
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -191,30 +191,57 @@ void handleSetNamedMode(String str_mode) {
if (str_mode.startsWith("=off")) { if (str_mode.startsWith("=off")) {
mode = OFF; mode = OFF;
#ifdef ENABLE_HOMEASSISTANT
stateOn = false;
#endif
} }
if (str_mode.startsWith("=all")) { if (str_mode.startsWith("=all")) {
mode = ALL; mode = ALL;
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
#endif
} }
if (str_mode.startsWith("=wipe")) { if (str_mode.startsWith("=wipe")) {
mode = WIPE; mode = WIPE;
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
#endif
} }
if (str_mode.startsWith("=rainbow")) { if (str_mode.startsWith("=rainbow")) {
mode = RAINBOW; mode = RAINBOW;
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
#endif
} }
if (str_mode.startsWith("=rainbowCycle")) { if (str_mode.startsWith("=rainbowCycle")) {
mode = RAINBOWCYCLE; mode = RAINBOWCYCLE;
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
#endif
} }
if (str_mode.startsWith("=theaterchase")) { if (str_mode.startsWith("=theaterchase")) {
mode = THEATERCHASE; mode = THEATERCHASE;
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
#endif
} }
if (str_mode.startsWith("=twinkleRandom")) { if (str_mode.startsWith("=twinkleRandom")) {
mode = TWINKLERANDOM; mode = TWINKLERANDOM;
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
#endif
} }
if (str_mode.startsWith("=theaterchaseRainbow")) { if (str_mode.startsWith("=theaterchaseRainbow")) {
mode = THEATERCHASERAINBOW; mode = THEATERCHASERAINBOW;
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
#endif
} }
if (str_mode.startsWith("=tv")) { if (str_mode.startsWith("=tv")) {
mode = TV; mode = TV;
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
#endif
} }
} }
@ -381,6 +408,10 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
handleSetMainColor(payload); handleSetMainColor(payload);
DBG_OUTPUT_PORT.printf("Set main color to: [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue); DBG_OUTPUT_PORT.printf("Set main color to: [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue);
webSocket.sendTXT(num, "OK"); webSocket.sendTXT(num, "OK");
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
sendState();
#endif
} }
// ? ==> Set speed // ? ==> Set speed
@ -399,12 +430,20 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
DBG_OUTPUT_PORT.printf("WS: Set brightness to: [%u]\n", brightness); DBG_OUTPUT_PORT.printf("WS: Set brightness to: [%u]\n", brightness);
strip.setBrightness(brightness); strip.setBrightness(brightness);
webSocket.sendTXT(num, "OK"); webSocket.sendTXT(num, "OK");
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
sendState();
#endif
} }
// * ==> Set main color and light all LEDs (Shortcut) // * ==> Set main color and light all LEDs (Shortcut)
if (payload[0] == '*') { if (payload[0] == '*') {
handleSetAllMode(payload); handleSetAllMode(payload);
webSocket.sendTXT(num, "OK"); webSocket.sendTXT(num, "OK");
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
sendState();
#endif
} }
// ! ==> Set single LED in given color // ! ==> Set single LED in given color
@ -434,6 +473,9 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
DBG_OUTPUT_PORT.printf("Activated mode [%u]!\n", mode); DBG_OUTPUT_PORT.printf("Activated mode [%u]!\n", mode);
webSocket.sendTXT(num, "OK"); webSocket.sendTXT(num, "OK");
#ifdef ENABLE_HOMEASSISTANT
sendState();
#endif
} }
// $ ==> Get status Info. // $ ==> Get status Info.
@ -458,6 +500,10 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
if (payload[0] == '/') { if (payload[0] == '/') {
handleSetWS2812FXMode(payload); handleSetWS2812FXMode(payload);
webSocket.sendTXT(num, "OK"); webSocket.sendTXT(num, "OK");
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
sendState();
#endif
} }
// start auto cycling // start auto cycling
@ -471,11 +517,6 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
handleAutoStop(); handleAutoStop();
webSocket.sendTXT(num, "OK"); webSocket.sendTXT(num, "OK");
} }
#ifdef ENABLE_HOMEASSISTANT
sendState();
#endif
break; break;
} }
} }
@ -483,6 +524,9 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
void checkForRequests() { void checkForRequests() {
webSocket.loop(); webSocket.loop();
server.handleClient(); server.handleClient();
#ifdef ENABLE_MQTT
mqtt_client.loop();
#endif
} }
@ -628,7 +672,7 @@ void checkForRequests() {
DBG_OUTPUT_PORT.printf("MQTT: Message arrived [%s]\n", payload); DBG_OUTPUT_PORT.printf("MQTT: Message arrived [%s]\n", payload);
#ifdef ENABLE_HOMEASSISTANT #ifdef ENABLE_HOMEASSISTANT
if ((strcmp(topic, mqtt_ha_state_in.c_str()) == 0)) { if (strcmp(topic, mqtt_ha_state_in.c_str()) == 0) {
if (!processJson((char*)payload)) { if (!processJson((char*)payload)) {
return; return;
} }
@ -647,6 +691,10 @@ void checkForRequests() {
handleSetMainColor(payload); handleSetMainColor(payload);
DBG_OUTPUT_PORT.printf("MQTT: Set main color to [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue); DBG_OUTPUT_PORT.printf("MQTT: Set main color to [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue);
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str()); mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
sendState();
#endif
} }
// ? ==> Set speed // ? ==> Set speed
@ -665,6 +713,10 @@ void checkForRequests() {
strip.setBrightness(brightness); strip.setBrightness(brightness);
DBG_OUTPUT_PORT.printf("MQTT: Set brightness to [%u]\n", brightness); DBG_OUTPUT_PORT.printf("MQTT: Set brightness to [%u]\n", brightness);
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str()); mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
sendState();
#endif
} }
// * ==> Set main color and light all LEDs (Shortcut) // * ==> Set main color and light all LEDs (Shortcut)
@ -672,6 +724,10 @@ void checkForRequests() {
handleSetAllMode(payload); handleSetAllMode(payload);
DBG_OUTPUT_PORT.printf("MQTT: Set main color and light all LEDs [%s]\n", payload); DBG_OUTPUT_PORT.printf("MQTT: Set main color and light all LEDs [%s]\n", payload);
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str()); mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
sendState();
#endif
} }
// ! ==> Set single LED in given color // ! ==> Set single LED in given color
@ -700,6 +756,9 @@ void checkForRequests() {
handleSetNamedMode(str_mode); handleSetNamedMode(str_mode);
DBG_OUTPUT_PORT.printf("MQTT: Activate named mode [%s]\n", payload); DBG_OUTPUT_PORT.printf("MQTT: Activate named mode [%s]\n", payload);
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str()); mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#ifdef ENABLE_HOMEASSISTANT
sendState();
#endif
} }
// $ ==> Get status Info. // $ ==> Get status Info.
@ -728,10 +787,13 @@ void checkForRequests() {
handleSetWS2812FXMode(payload); handleSetWS2812FXMode(payload);
DBG_OUTPUT_PORT.printf("MQTT: Set WS2812 mode [%s]\n", payload); DBG_OUTPUT_PORT.printf("MQTT: Set WS2812 mode [%s]\n", payload);
mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str()); mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
sendState();
#endif
} }
#ifdef ENABLE_HOMEASSISTANT #ifdef ENABLE_HOMEASSISTANT
sendState();
} }
#endif #endif
free(payload); free(payload);
@ -753,7 +815,7 @@ void checkForRequests() {
// ... and resubscribe // ... and resubscribe
mqtt_client.subscribe(mqtt_intopic); mqtt_client.subscribe(mqtt_intopic);
#ifdef ENABLE_HOMEASSISTANT #ifdef ENABLE_HOMEASSISTANT
mqtt_client.subscribe(String(mqtt_ha + "#").c_str()); mqtt_client.subscribe(mqtt_ha_state_in.c_str());
#endif #endif
DBG_OUTPUT_PORT.printf("MQTT topic in: %s\n", mqtt_intopic); DBG_OUTPUT_PORT.printf("MQTT topic in: %s\n", mqtt_intopic);
@ -782,9 +844,19 @@ void checkForRequests() {
if (buttonState == false) { if (buttonState == false) {
setModeByStateString(BTN_MODE_SHORT); setModeByStateString(BTN_MODE_SHORT);
buttonState = true; buttonState = true;
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String("OK =static white").c_str());
#endif
} else { } else {
mode = OFF; mode = OFF;
buttonState = false; buttonState = false;
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String("OK =off").c_str());
#ifdef ENABLE_HOMEASSISTANT
stateOn = false;
sendState();
#endif
#endif
} }
} }
@ -792,12 +864,26 @@ void checkForRequests() {
void mediumKeyPress() { void mediumKeyPress() {
DBG_OUTPUT_PORT.printf("Medium button press\n"); DBG_OUTPUT_PORT.printf("Medium button press\n");
setModeByStateString(BTN_MODE_MEDIUM); setModeByStateString(BTN_MODE_MEDIUM);
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String("OK =fire flicker").c_str());
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
sendState();
#endif
#endif
} }
// called when button is kept pressed for 2 seconds or more // called when button is kept pressed for 2 seconds or more
void longKeyPress() { void longKeyPress() {
DBG_OUTPUT_PORT.printf("Long button press\n"); DBG_OUTPUT_PORT.printf("Long button press\n");
setModeByStateString(BTN_MODE_LONG); setModeByStateString(BTN_MODE_LONG);
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String("OK =fireworks random").c_str());
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
sendState();
#endif
#endif
} }
void button() { void button() {