diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index dc18967..77709f6 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -86,9 +86,10 @@ uint32_t autoParams[][6] = { // main_color, back_color, xtra_color, speed, mod #if defined(ENABLE_MQTT) char mqtt_buf[80]; char mqtt_will_topic[sizeof(HOSTNAME) + 7]; // Topic 'will' will be:HOSTNAME "/status"; - char mqtt_will_payload[] = "ONLINE"; + const char mqtt_will_payload[] = "OFFLINE"; char mqtt_intopic[sizeof(HOSTNAME) + 3]; // Topic 'in' will be: /in char mqtt_outtopic[sizeof(HOSTNAME) + 4]; // Topic 'out' will be: /out + bool mqtt_lwt_boot_flag = true; #if ENABLE_MQTT == 0 #define MQTT_MAX_PACKET_SIZE 512 #define MQTT_MAX_RECONNECT_TRIES 4 diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index 0bf8be6..4177c19 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -1283,18 +1283,23 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght mqtt_reconnect_retries++; DBG_OUTPUT_PORT.printf("Attempting MQTT connection %d / %d ...\r\n", mqtt_reconnect_retries, MQTT_MAX_RECONNECT_TRIES); // Attempt to connect - if (mqtt_client->connect(mqtt_clientid, mqtt_user, mqtt_pass)) { + if (mqtt_client->connect(mqtt_clientid, mqtt_user, mqtt_pass, mqtt_will_topic, 2, true, mqtt_will_payload, true)) { DBG_OUTPUT_PORT.println("MQTT connected!"); // Once connected, publish an announcement... - char * message = new char[18 + strlen(HOSTNAME) + 1]; + char message[18 + strlen(HOSTNAME) + 1]; strcpy(message, "McLighting ready: "); strcat(message, HOSTNAME); mqtt_client->publish(mqtt_outtopic, message); // ... and resubscribe mqtt_client->subscribe(mqtt_intopic, qossub); + if(mqtt_lwt_boot_flag) { + mqtt_client.publish(mqtt_will_topic, "ONLINE"); + //mqtt_lwt_boot_flag = false; + } #if defined(ENABLE_HOMEASSISTANT) ha_send_data.detach(); mqtt_client->subscribe(mqtt_ha_state_in, qossub); + ha_send_data.once(5, tickerSendState); #if defined(MQTT_HOME_ASSISTANT_SUPPORT) const size_t bufferSize = JSON_ARRAY_SIZE(strip->getModeCount()+ 4) + JSON_OBJECT_SIZE(11) + 1500; DynamicJsonDocument jsonBuffer(bufferSize); @@ -1339,9 +1344,9 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght unsigned int msg_len = measureJson(json) + 1; char buffer[msg_len]; serializeJson(json, buffer, sizeof(buffer)); - - mqtt_client->beginPublish(mqtt_ha_config, msg_len, true); - mqtt_client->write((const uint8_t*)buffer, msg_len); + DBG_OUTPUT_PORT.println(buffer); + mqtt_client->beginPublish(String("homeassistant/light/" + String(HOSTNAME) + "/config").c_str(), msg_len-1, true); + mqtt_client->write((const uint8_t*)buffer, msg_len-1); mqtt_client->endPublish(); #endif #endif @@ -1392,13 +1397,17 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght DBG_OUTPUT_PORT.println("Connected to MQTT."); DBG_OUTPUT_PORT.print("Session present: "); DBG_OUTPUT_PORT.println(sessionPresent); - char * message = new char[18 + strlen(HOSTNAME) + 1]; + char message[18 + strlen(HOSTNAME) + 1]; strcpy(message, "McLighting ready: "); strcat(message, HOSTNAME); mqtt_client->publish(mqtt_outtopic, qospub, false, message); //Subscribe - uint16_t packetIdSub1 = mqtt_client->subscribe((char *)mqtt_intopic, qossub); + uint16_t packetIdSub1 = mqtt_client->subscribe(mqtt_intopic, qossub); DBG_OUTPUT_PORT.printf("Subscribing at QoS %d, packetId: ", qossub); DBG_OUTPUT_PORT.println(packetIdSub1); + if(mqtt_lwt_boot_flag) { + mqtt_client->publish(mqtt_will_topic, qospub, false, "ONLINE"); + mqtt_lwt_boot_flag = false; + } #if defined(ENABLE_HOMEASSISTANT) ha_send_data.detach(); uint16_t packetIdSub2 = mqtt_client->subscribe((char *)mqtt_ha_state_in, qossub); diff --git a/Arduino/McLighting/version.h b/Arduino/McLighting/version.h index d165c4f..07c2588 100644 --- a/Arduino/McLighting/version.h +++ b/Arduino/McLighting/version.h @@ -1 +1 @@ -#define SKETCH_VERSION "2.2.2.rgbw.3c" +#define SKETCH_VERSION "2.2.3.rgbw.3c" diff --git a/Arduino/McLighting/version_info.ino b/Arduino/McLighting/version_info.ino index c1b224a..e8d8911 100644 --- a/Arduino/McLighting/version_info.ino +++ b/Arduino/McLighting/version_info.ino @@ -133,7 +133,11 @@ * 18 Mar 2019 * adressed issue: #6 (possibly affects R[r_start][r_end][hexrgb] [...]; +[numled][hexrgb]+[numled][hexrgb]+[numled][hexrgb] !) * - *19 Mar 2019 + * 19 Mar 2019 * included custom mode in UI * adressed issue #4 + * + * Version Bump to 2.2.3 rgbw 3colors + * PubSubClient Bug fixes + * */ diff --git a/platformio.ini b/platformio.ini index 9e35607..e5253ac 100644 --- a/platformio.ini +++ b/platformio.ini @@ -31,12 +31,13 @@ upload_resetmethod = nodemcu lib_deps = WiFiManager@0.14 AsyncMqttClient - https://github.com/bblanchon/ArduinoJson.git#v6.7.0-beta + ArduinoJson WS2812FX NeoPixelBus@2.4.1 WebSockets ESPAsyncE131 ESPAsyncUDP + https://github.com/FabLab-Luenen/GY33_MCU Brzo_I2C [env:esp01_1m]