Skip to content

Commit

Permalink
Don't hang on MQTT connection attempt
Browse files Browse the repository at this point in the history
- On connection lost, do not try to reconnect immediately
- Don't block main loop while trying to reconnect
- Prefer bool over boolean
  • Loading branch information
jbaudoux committed Mar 24, 2024
1 parent 1f89830 commit 14428a1
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions BSB_LAN/include/mqtt_handler.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
char *build_pvalstr(bool extended);
unsigned long mqtt_reconnect_timer;

//Luposoft: function mqtt_sendtoBroker
/* Function: mqtt_sendtoBroker()
* Does: send messages to mqtt-broker
Expand Down Expand Up @@ -171,14 +173,14 @@ const String mqtt_get_will_topic() {
* Parameters passed back:
* none
* Function value returned:
* boolean
* bool
* Global resources used:
* Serial instance
* Ethernet instance
* MQTT instance
* *************************************************************** */

boolean mqtt_connect() {
bool mqtt_connect() {
char* tempstr = (char*)malloc(sizeof(mqtt_broker_addr)); // make a copy of mqtt_broker_addr for destructive strtok operation
strcpy(tempstr, mqtt_broker_addr);
uint16_t mqtt_port = 1883;
Expand All @@ -189,6 +191,7 @@ boolean mqtt_connect() {
}
free(tempstr);

bool first_connect = false;
if(MQTTPubSubClient == NULL) {
mqtt_client= new ComClient();
uint16_t bufsize;
Expand All @@ -200,8 +203,20 @@ boolean mqtt_connect() {
default: bufsize = 32; break;
}
MQTTPubSubClient->setBufferSize(bufsize);
mqtt_reconnect_timer = 0;
first_connect = true;
}
if (!MQTTPubSubClient->connected()) {
if (!first_connect && !mqtt_reconnect_timer) {
// We just lost connection, don't try to reconnect immediately
mqtt_reconnect_timer = millis();
printlnToDebug(PSTR("MQTT connection lost"));
return false;
}
if (mqtt_reconnect_timer and millis() - mqtt_reconnect_timer < 1000)
// Wait 1s between reconnection attempts
return false;

char* MQTTUser = NULL;
if(MQTTUsername[0]) {
MQTTUser = MQTTUsername;
Expand All @@ -213,31 +228,27 @@ boolean mqtt_connect() {
MQTTPubSubClient->setServer(mqtt_host, mqtt_port);
String MQTTWillTopic = mqtt_get_will_topic();
String MQTTRealClientId = mqtt_get_client_id();
int retries = 0;
printFmtToDebug(PSTR("Client ID: %s\r\n"), MQTTRealClientId.c_str());
printFmtToDebug(PSTR("Will topic: %s\r\n"), MQTTWillTopic.c_str());
while (!MQTTPubSubClient->connected() && retries < 3) {
MQTTPubSubClient->connect(MQTTRealClientId.c_str(), MQTTUser, MQTTPass, MQTTWillTopic.c_str(), 0, true, PSTR("offline"));
retries++;
if (!MQTTPubSubClient->connected()) {
delay(1000);
printlnToDebug(PSTR("Failed to connect to MQTT broker, retrying..."));
MQTTPubSubClient->connect(MQTTRealClientId.c_str(), MQTTUser, MQTTPass, MQTTWillTopic.c_str(), 0, true, PSTR("offline"));
if (!MQTTPubSubClient->connected()) {
printlnToDebug(PSTR("Failed to connect to MQTT broker, retrying..."));
} else {
printlnToDebug(PSTR("Connected to MQTT broker, updating will topic"));
mqtt_reconnect_timer = 0;
const char* mqtt_subscr;
if (MQTTTopicPrefix[0]) {
mqtt_subscr = MQTTTopicPrefix;
} else {
printlnToDebug(PSTR("Connect to MQTT broker, updating will topic"));
const char* mqtt_subscr;
if (MQTTTopicPrefix[0]) {
mqtt_subscr = MQTTTopicPrefix;
} else {
mqtt_subscr="fromBroker";
}
MQTTPubSubClient->subscribe(mqtt_subscr); //Luposoft: set the topic listen to
printFmtToDebug(PSTR("Subscribed to topic '%s'\r\n"), mqtt_subscr);
MQTTPubSubClient->setKeepAlive(120); //Luposoft: just for savety
MQTTPubSubClient->setCallback(mqtt_callback); //Luposoft: set to function is called when incoming message
MQTTPubSubClient->publish(MQTTWillTopic.c_str(), PSTR("online"), true);
printFmtToDebug(PSTR("Published status 'online' to topic '%s'\r\n"), MQTTWillTopic.c_str());
return true;
mqtt_subscr="fromBroker";
}
MQTTPubSubClient->subscribe(mqtt_subscr); //Luposoft: set the topic listen to
printFmtToDebug(PSTR("Subscribed to topic '%s'\r\n"), mqtt_subscr);
MQTTPubSubClient->setKeepAlive(120); //Luposoft: just for savety
MQTTPubSubClient->setCallback(mqtt_callback); //Luposoft: set to function is called when incoming message
MQTTPubSubClient->publish(MQTTWillTopic.c_str(), PSTR("online"), true);
printFmtToDebug(PSTR("Published status 'online' to topic '%s'\r\n"), MQTTWillTopic.c_str());
return true;
}
} else {
return true;
Expand Down

0 comments on commit 14428a1

Please sign in to comment.