10
10
// #define MQTT_MAX_PACKET_SIZE 128 --> #define MQTT_MAX_PACKET_SIZE 800
11
11
12
12
#define MQTT_MAX_PACKET_SIZE 800
13
- #define FIRMWARE_VERSION " 2.0.3 "
13
+ #define FIRMWARE_VERSION " 2.0.4 "
14
14
#define MANUFACTURER " Huacanxing"
15
15
16
16
#include < string>
@@ -40,10 +40,12 @@ ESP8266HTTPUpdateServer httpUpdater;
40
40
41
41
/* ********************************* program variables *****************************************/
42
42
char chip_id[9 ] = " 00000000" ;
43
+ char client_id[16 ] = " 00000000_00000" ;
43
44
char myhostname[] = " esp00000000" ;
44
45
IPAddress ip;
45
46
uint8_t reconnect_N = 0 ;
46
47
unsigned long last_publish_ms = 0 ;
48
+ unsigned long last_mqtt_connected = 0 ;
47
49
// transitioning variables
48
50
float transition_time_s_standard = transition_time_s_conf;
49
51
float transition_time_s = transition_time_s_conf;
@@ -378,6 +380,7 @@ void publishJsonSettings() {
378
380
rgb_mix[" g" ] = RGB_mixing[1 ];
379
381
rgb_mix[" b" ] = RGB_mixing[2 ];
380
382
root[" chip_id" ] = myhostname;
383
+ root[" client_id" ] = client_id;
381
384
root[" IP" ] = ip.toString ();
382
385
383
386
char buffer[measureJson (root) + 1 ];
@@ -929,7 +932,8 @@ void reconnect() {
929
932
while (!client.connected ()) {
930
933
Serial1.print (" Attempting MQTT connection..." );
931
934
// 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)) {
933
937
Serial1.println (" connected" );
934
938
// blink 10 times green LED for success connected
935
939
for (int x=0 ; x < 10 ; x++){
@@ -981,11 +985,16 @@ void reconnect() {
981
985
982
986
void loop ()
983
987
{
988
+ unsigned long now = millis ();
984
989
if (WiFi.status () == WL_CONNECTED) {
985
990
// Confirm that still connected to MQTT broker
986
991
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;
989
998
}
990
999
} else {
991
1000
digitalWrite (RED_PIN, 0 );
@@ -1004,12 +1013,12 @@ void loop()
1004
1013
// Post the full status to MQTT every 60000 miliseconds. This is roughly once a minute
1005
1014
// Usually, clients will store the value internally.
1006
1015
// This is only used if a client starts up again and did not receive previous messages
1007
- unsigned long now = millis ();
1008
1016
if (now - last_publish_ms > 60000 ) {
1009
1017
last_publish_ms = now;
1010
1018
publishCombinedJsonState ();
1011
1019
publishRGBJsonState ();
1012
1020
publishWhiteJsonState ();
1021
+ client.publish (MQTT_UP, MQTT_UP_online, true );
1013
1022
}
1014
1023
// Process UDP messages if needed
1015
1024
UDP_loop (now);
0 commit comments