Skip to content

Commit 1174ed6

Browse files
committed
Improve MQTT reconnect logic, Randomize client_id, publish online every 1 minute
1 parent a5ad628 commit 1174ed6

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

h801-mqtt-json.ino

+14-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// #define MQTT_MAX_PACKET_SIZE 128 --> #define MQTT_MAX_PACKET_SIZE 800
1111

1212
#define MQTT_MAX_PACKET_SIZE 800
13-
#define FIRMWARE_VERSION "2.0.3"
13+
#define FIRMWARE_VERSION "2.0.4"
1414
#define MANUFACTURER "Huacanxing"
1515

1616
#include <string>
@@ -40,10 +40,12 @@ ESP8266HTTPUpdateServer httpUpdater;
4040

4141
/********************************** program variables *****************************************/
4242
char chip_id[9] = "00000000";
43+
char client_id[16] = "00000000_00000";
4344
char myhostname[] = "esp00000000";
4445
IPAddress ip;
4546
uint8_t reconnect_N = 0;
4647
unsigned long last_publish_ms = 0;
48+
unsigned long last_mqtt_connected = 0;
4749
// transitioning variables
4850
float transition_time_s_standard = transition_time_s_conf;
4951
float transition_time_s = transition_time_s_conf;
@@ -378,6 +380,7 @@ void publishJsonSettings() {
378380
rgb_mix["g"] = RGB_mixing[1];
379381
rgb_mix["b"] = RGB_mixing[2];
380382
root["chip_id"] = myhostname;
383+
root["client_id"] = client_id;
381384
root["IP"] = ip.toString();
382385

383386
char buffer[measureJson(root) + 1];
@@ -929,7 +932,8 @@ void reconnect() {
929932
while (!client.connected()) {
930933
Serial1.print("Attempting MQTT connection...");
931934
// Attempt to connect
932-
if (client.connect(chip_id, mqtt_user, mqtt_password, MQTT_UP, 2, true, MQTT_UP_offline)) {
935+
sprintf(client_id, "%s_%05d", chip_id, random(1, 99999));
936+
if (client.connect(client_id, mqtt_user, mqtt_password, MQTT_UP, 2, true, MQTT_UP_offline)) {
933937
Serial1.println("connected");
934938
// blink 10 times green LED for success connected
935939
for (int x=0; x < 10; x++){
@@ -981,11 +985,16 @@ void reconnect() {
981985

982986
void loop()
983987
{
988+
unsigned long now = millis();
984989
if (WiFi.status() == WL_CONNECTED) {
985990
//Confirm that still connected to MQTT broker
986991
if (!client.connected()) {
987-
Serial1.println("Reconnecting to MQTT Broker");
988-
reconnect();
992+
if (now - last_mqtt_connected > 15000) {
993+
Serial1.println("Reconnecting to MQTT Broker");
994+
reconnect();
995+
}
996+
} else {
997+
last_mqtt_connected = now;
989998
}
990999
} else {
9911000
digitalWrite(RED_PIN, 0);
@@ -1004,12 +1013,12 @@ void loop()
10041013
// Post the full status to MQTT every 60000 miliseconds. This is roughly once a minute
10051014
// Usually, clients will store the value internally.
10061015
// This is only used if a client starts up again and did not receive previous messages
1007-
unsigned long now = millis();
10081016
if (now - last_publish_ms > 60000) {
10091017
last_publish_ms = now;
10101018
publishCombinedJsonState();
10111019
publishRGBJsonState();
10121020
publishWhiteJsonState();
1021+
client.publish(MQTT_UP, MQTT_UP_online, true);
10131022
}
10141023
// Process UDP messages if needed
10151024
UDP_loop(now);

0 commit comments

Comments
 (0)