Skip to content

Commit

Permalink
Merge pull request #683 from uschindler/dev/update_2_10
Browse files Browse the repository at this point in the history
Update pubsubclient to v2.11.0 by thingsboard
  • Loading branch information
fredlcore authored Dec 9, 2024
2 parents 1efe6bf + c9b44ec commit ebb5559
Show file tree
Hide file tree
Showing 7 changed files with 294 additions and 206 deletions.
30 changes: 14 additions & 16 deletions BSB_LAN/include/mqtt_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,38 +181,37 @@ char* mqtt_get_will_topic() {
* *************************************************************** */

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;
char* mqtt_host = strtok(tempstr,":"); // hostname is before an optional colon that separates the port
char* token = strtok(NULL, ":"); // remaining part is the port number
if (token != 0) {
mqtt_port = atoi(token);
}
free(tempstr);

bool first_connect = false;
if(MQTTPubSubClient == NULL) {
mqtt_client= new ComClient();
MQTTPubSubClient = new PubSubClient(mqtt_client[0]);
MQTTPubSubClient->setBufferSize(2048);
MQTTPubSubClient->setKeepAlive(30);
MQTTPubSubClient->setSocketTimeout(120);
MQTTPubSubClient->setBufferSize(2048, 2048);
MQTTPubSubClient->setKeepAlive(120); // raise to higher value so broker does not disconnect on latency
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("MQTT connection lost");
printFmtToDebug("MQTT connection lost with status code %d\r\n", MQTTPubSubClient->state());
return false;
}
if (mqtt_reconnect_timer && millis() - mqtt_reconnect_timer < 10000) {
// Wait 1s between reconnection attempts
return false;
}

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;
char* mqtt_host = strtok(tempstr,":"); // hostname is before an optional colon that separates the port
char* token = strtok(NULL, ":"); // remaining part is the port number
if (token != 0) {
mqtt_port = atoi(token);
}
free(tempstr);

char* MQTTUser = NULL;
if(MQTTUsername[0]) {
MQTTUser = MQTTUsername;
Expand All @@ -227,7 +226,7 @@ bool mqtt_connect() {
printFmtToDebug("Will topic: %s\r\n", mqtt_get_will_topic());
MQTTPubSubClient->connect(mqtt_get_client_id(), MQTTUser, MQTTPass, mqtt_get_will_topic(), 1, true, "offline");
if (!MQTTPubSubClient->connected()) {
printlnToDebug("Failed to connect to MQTT broker, retrying...");
printFmtToDebug("Failed to connect to MQTT broker with status code %d, retrying...\r\n", MQTTPubSubClient->state());
mqtt_reconnect_timer = millis();
} else {
printlnToDebug("Connected to MQTT broker, updating will topic");
Expand All @@ -237,7 +236,6 @@ bool mqtt_connect() {
strcat(tempTopic, "/#");
MQTTPubSubClient->subscribe(tempTopic, 1); //Luposoft: set the topic listen to
printFmtToDebug("Subscribed to topic '%s'\r\n", tempTopic);
MQTTPubSubClient->setKeepAlive(120); //Luposoft: just for savety
MQTTPubSubClient->setCallback(mqtt_callback); //Luposoft: set to function is called when incoming message
MQTTPubSubClient->publish(mqtt_get_will_topic(), "online", true);
printFmtToDebug("Published status 'online' to topic '%s'\r\n", mqtt_get_will_topic());
Expand Down
2 changes: 2 additions & 0 deletions BSB_LAN/src/PubSubClient/CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
2.9
* Add ability to use function for callback not just for ESP boards
2.8
* Add setBufferSize() to override MQTT_MAX_PACKET_SIZE
* Add setKeepAlive() to override MQTT_KEEPALIVE
Expand Down
8 changes: 8 additions & 0 deletions BSB_LAN/src/PubSubClient/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## ThingsBoard note

This is a fork of the main repository, which was last updated in 2020.
Since we have an SDK based on this client, we decided to continue with this repository.
We also believe that this library provides a lot of opportunities for people who want to build their own IoT devices.
As with our other open source repositories, we appreciate every contribution to this library.


# Arduino Client for MQTT

This library provides a client for doing simple publish/subscribe messaging with
Expand Down
11 changes: 6 additions & 5 deletions BSB_LAN/src/PubSubClient/library.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
{
"name": "PubSubClient",
"keywords": "ethernet, mqtt, m2m, iot",
"name": "TBPubSubClient",
"keywords": "ethernet, mqtt, m2m, iot, thingsboard, messages",
"description": "A client library for MQTT messaging. MQTT is a lightweight messaging protocol ideal for small devices. This library allows you to send and receive MQTT messages. It supports the latest MQTT 3.1.1 protocol and can be configured to use the older MQTT 3.1 if needed. It supports all Arduino Ethernet Client compatible hardware, including the Intel Galileo/Edison, ESP8266 and TI CC3000.",
"repository": {
"type": "git",
"url": "https://github.com/knolleary/pubsubclient.git"
"url": "https://github.com/thingsboard/pubsubclient.git"
},
"version": "2.8",
"version": "2.10.0",
"exclude": "tests",
"examples": "examples/*/*.ino",
"frameworks": "arduino",
"platforms": [
"atmelavr",
"espressif8266",
"espressif32"
"espressif32",
"rp2040"
]
}
8 changes: 4 additions & 4 deletions BSB_LAN/src/PubSubClient/library.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name=PubSubClient
version=2.8
author=Nick O'Leary <[email protected]>
maintainer=Nick O'Leary <[email protected]>
name=TBPubSubClient
version=2.10.0
author=ThingsBoard <[email protected]>
maintainer=ThingsBoard Team
sentence=A client library for MQTT messaging.
paragraph=MQTT is a lightweight messaging protocol ideal for small devices. This library allows you to send and receive MQTT messages. It supports the latest MQTT 3.1.1 protocol and can be configured to use the older MQTT 3.1 if needed. It supports all Arduino Ethernet Client compatible hardware, including the Intel Galileo/Edison, ESP8266 and TI CC3000.
category=Communication
Expand Down
Loading

0 comments on commit ebb5559

Please sign in to comment.