Skip to content

Commit dd5347c

Browse files
committed
Fixes
1 parent 797ab68 commit dd5347c

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

h801-mqtt-json.ino

+23-15
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ unsigned long last_mqtt_connected = 0;
4949
// transitioning variables
5050
float transition_time_s_standard = transition_time_s_conf;
5151
float transition_time_s = transition_time_s_conf;
52+
unsigned long now;
5253
unsigned long last_transition_publish = 0;
5354
unsigned long start_transition_loop_ms = 0;
5455
unsigned long transition_ms = 0;
@@ -459,7 +460,7 @@ void publishJsonDiscovery() {
459460

460461
void publishJsonDiscovery_entity(const char type[], bool sup_color_temp, bool sup_rgb) {
461462
StaticJsonDocument<JSON_BUFFER_SIZE> root;
462-
char idendifier[15], unique_id[27], entity_name[14], stat_t[27], cmd_t[27];
463+
char idendifier[15], unique_id[29], entity_name[16], stat_t[29], cmd_t[29];
463464
char conf_url[strlen(OTA_update_path)+25];
464465
sprintf(idendifier, "H801_%s", chip_id);
465466
sprintf(unique_id, "H801_%s_%s", chip_id, type);
@@ -514,7 +515,7 @@ void publishJsonDiscovery_entity(const char type[], bool sup_color_temp, bool su
514515
char buffer[measureJson(root) + 1];
515516
serializeJson(root, buffer, sizeof(buffer));
516517

517-
char mqtt_discovery_topic[strlen(MQTT_HOMEASSISTANT_DISCOVERY_PREFIX) + 45];
518+
char mqtt_discovery_topic[strlen(MQTT_HOMEASSISTANT_DISCOVERY_PREFIX) + 50];
518519
sprintf(mqtt_discovery_topic, "%s/light/H801_%s/%s/config", MQTT_HOMEASSISTANT_DISCOVERY_PREFIX, chip_id, type);
519520
client.publish(mqtt_discovery_topic, buffer, true);
520521
}
@@ -555,10 +556,11 @@ void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
555556
}
556557
if (transition_time_s <= 0) {
557558
setColor();
559+
publishRGBJsonState();
558560
} else {
559561
Transition();
562+
// Let transition publish each 15s and @end
560563
}
561-
publishRGBJsonState();
562564
}
563565

564566
// Handle White commands
@@ -569,10 +571,11 @@ void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
569571
}
570572
if (transition_time_s <= 0) {
571573
setWhite();
574+
publishWhiteJsonState();
572575
} else {
573576
Transition();
577+
// Let transition publish each 15s and @end
574578
}
575-
publishWhiteJsonState();
576579
}
577580

578581
// Handle White single 1 commands
@@ -583,10 +586,11 @@ void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
583586
}
584587
if (transition_time_s <= 0) {
585588
setWhite();
589+
publishWhiteSingle1JsonState();
586590
} else {
587591
Transition();
592+
// Let transition publish each 15s and @end
588593
}
589-
publishWhiteSingle1JsonState();
590594
}
591595

592596
// Handle White single 2 commands
@@ -597,10 +601,11 @@ void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
597601
}
598602
if (transition_time_s <= 0) {
599603
setWhite();
604+
publishWhiteSingle2JsonState();
600605
} else {
601606
Transition();
607+
// Let transition publish each 15s and @end
602608
}
603-
publishWhiteSingle2JsonState();
604609
}
605610

606611
// Handle combined commands
@@ -612,10 +617,11 @@ void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
612617
if (transition_time_s <= 0) {
613618
setWhite();
614619
setColor();
620+
publishCombinedJsonState();
615621
} else {
616622
Transition();
623+
// Let transition publish each 15s and @end
617624
}
618-
publishCombinedJsonState();
619625
}
620626

621627
// Handle settings commands
@@ -796,7 +802,7 @@ bool processWhiteJson(char* message, bool single1, bool single2) {
796802
m_w1 = brightness;
797803
}
798804
if (single2){
799-
m_w1 = brightness;
805+
m_w2 = brightness;
800806
}
801807
}
802808
}
@@ -1084,7 +1090,7 @@ void reconnect() {
10841090

10851091
void loop()
10861092
{
1087-
unsigned long now = millis();
1093+
now = millis();
10881094
if (WiFi.status() == WL_CONNECTED) {
10891095
//Confirm that still connected to MQTT broker
10901096
if (!client.connected()) {
@@ -1122,9 +1128,9 @@ void loop()
11221128
client.publish(MQTT_UP, MQTT_UP_online, true);
11231129
}
11241130
// Process UDP messages if needed
1125-
UDP_loop(now);
1131+
UDP_loop();
11261132
// Process transitions if needed
1127-
Transition_loop(now);
1133+
Transition_loop();
11281134
}
11291135

11301136

@@ -1222,12 +1228,12 @@ void Transition(void) {
12221228
setW2(transition_w2);
12231229

12241230
// Set variables for beginning transition
1225-
start_transition_loop_ms = millis();
1226-
last_transition_publish = 0;
1231+
start_transition_loop_ms = now;
1232+
last_transition_publish = now;
12271233
transitioning = true;
12281234
}
12291235

1230-
void Transition_loop(unsigned long now) {
1236+
void Transition_loop() {
12311237
if (transitioning) {
12321238
transition_ms = now - start_transition_loop_ms;
12331239

@@ -1244,6 +1250,7 @@ void Transition_loop(unsigned long now) {
12441250
if(!UDP_stream) {
12451251
setWhite();
12461252
setColor();
1253+
last_publish_ms = now;
12471254
publishCombinedJsonState();
12481255
publishRGBJsonState();
12491256
publishWhiteJsonState();
@@ -1256,6 +1263,7 @@ void Transition_loop(unsigned long now) {
12561263
if (now - last_transition_publish > 15000) {
12571264
last_transition_publish = now;
12581265
if(!UDP_stream) {
1266+
last_publish_ms = now;
12591267
publish_from_transition_state();
12601268
}
12611269
}
@@ -1398,7 +1406,7 @@ void UDP_start_stop(void) {
13981406
}
13991407
}
14001408

1401-
void UDP_loop(unsigned long now) {
1409+
void UDP_loop() {
14021410
if (UDP_stream == true) {
14031411
// check if there is an UDP message available
14041412
if(Udp.parsePacket()) {

0 commit comments

Comments
 (0)