Skip to content

Commit

Permalink
Merge branch 'fix_mqtt_disconnect' into 'master'
Browse files Browse the repository at this point in the history
Fix mqtt disconnect

See merge request smartme.io/arancino/arancino-library!43
  • Loading branch information
andrea-83 committed Jun 23, 2023
2 parents 81f1f9d + ba41b4f commit 20e6531
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 41 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

#### v 3.1.1 - 2023.06.23
* Upd: Implemented will message for MQTT interface [#861myg408](https://app.clickup.com/t/861myg408)
* fix: fixed client reconnection MQTT interface [#861myg318](https://app.clickup.com/t/861myg318)

#### v 3.1.0 - 2023.04.06
* Upd: Implemented new Cortex protocol [#1jdx7ak](https://app.clickup.com/t/1jdx7ak)
* Add: Added support to ESP32 [#863gbd6ym](https://app.clickup.com/t/863gbd6ym)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Arancino Library allows to export/import data to/from the Linux environment usin
- [Reserved Keys](docs/RESERVED_KEYS.md)

## Compatibility list
The Arancino Library `3.1.0` officially supports the following Arancino Core versions:
The Arancino Library `3.1.1` officially supports the following Arancino Core versions:
|Board| Core Version |
|--|--|
| SAMD21 | 1.4.0 |
Expand Down
11 changes: 10 additions & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
> Arancino.set("test", mem);
> Arancino.free(mem);
> ```
>
> is possible in some application static memory reserved for ArancinoPacket is not enough. To change the value of the memory assignment edit the follow line in ArancinoDefinition.h:
> ```
> //MsgPack
> #define CMD_DOC_SIZE 512 //size for the message sent from the MCU
> #define RSP_DOC_SIZE 256 // size for message received on the MCU
> ```
> Currently the memory values ​​is sized to work with FreeRTOS enabled, if FreeRTOS is disabled is possible to increase these values.
>
### metadata
#### *void metadata(ArancinoMetadata data)* | ***DEPRECATED from Version 1.4.0***
Stores the metadata to use later during the `START` command in `begin` API.
Expand Down Expand Up @@ -561,4 +570,4 @@ void loop() {
Arancino.free(logLvl);
delay(2000);
}
```
```
2 changes: 1 addition & 1 deletion docs/VARS_DATA_STRUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,4 @@ The `BluetoothIface` class exploits UART-like communication over Bluetooth and B
```c++
void setBLESerial(Stream& bleUart);
```
A valid BLE stream should be passed as argument for the interface to properly work. Keep in mind that, depending on your implementation, you may need to check whether the exposed `Service` and `Characteristic` UUID match the ones contained in the Arancino module itself in the configutation file.
A valid BLE stream should be passed as argument for the interface to properly work. Keep in mind that, depending on your implementation, you may need to check whether the exposed `Service` and `Characteristic` UUID match the ones contained in the Arancino module itself in the configutation file.
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"type": "git",
"url": "https://github.com/smartmeio/arancino-library.git"
},
"version": "3.1.0",
"version": "3.1.1",
"authors": {
"name": "smartme.IO",
"url": "https://www.arancino.cc"
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Arancino
version=3.1.0
version=3.1.1
author=smartme.IO
maintainer=smartme.IO <[email protected]>
sentence=Enables communication between microcontroller and Arancino Module running mainly in Arancino boards.
Expand Down
87 changes: 51 additions & 36 deletions src/ArancinoInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ void MqttIface::ifaceBegin(){
setBufferSize(CMD_DOC_SIZE);

//+1 because of \n
Arancino.free(_inputTopic);
Arancino.free(_outputTopic);
Arancino.free(_serviceTopic);
_inputTopic = (char*)Arancino.calloc(strlen("arancino/cortex/") + strlen(_daemonID) + strlen(Arancino.id) + strlen("/rsp_to_mcu") + 2, sizeof(char));
_outputTopic = (char*)Arancino.calloc(strlen("arancino/cortex/") + strlen(_daemonID) + strlen(Arancino.id) + strlen("/cmd_from_mcu") + 2, sizeof(char));
_serviceTopic = (char*)Arancino.calloc(strlen("arancino/service/") + strlen(_daemonID) + 1, sizeof(char));
Expand All @@ -194,54 +197,65 @@ void MqttIface::ifaceBegin(){
strcpy(_serviceTopic, "arancino/service/");
strcat(_serviceTopic, _daemonID);

this->_reconnect();
if(this->_reconnect()){

this->subscribe(_serviceTopic); //arancino/service/dID
this->subscribe(_inputTopic);
this->subscribe(_serviceTopic); //arancino/service/dID
this->subscribe(_inputTopic);

char* discoverytopic = (char*)Arancino.calloc(strlen("arancino/discovery/") + strlen(_daemonID)+1, sizeof(char));
strcpy(discoverytopic, "arancino/discovery/");
strcat(discoverytopic, _daemonID);
this->publish(discoverytopic, Arancino.id);
Arancino.free(discoverytopic);
char discoverytopic[(strlen("arancino/discovery/") + strlen(_daemonID)+1)];
strcpy(discoverytopic, "arancino/discovery/");
strcat(discoverytopic, _daemonID);
this->publish(discoverytopic, Arancino.id);
/*char* discoverytopic = (char*)Arancino.calloc(strlen("arancino/discovery/") + strlen(_daemonID)+1, sizeof(char));
strcpy(discoverytopic, "arancino/discovery/");
strcat(discoverytopic, _daemonID);
this->publish(discoverytopic, Arancino.id);
Arancino.free(discoverytopic);*/

}
}

void MqttIface::sendArancinoCommand(JsonDocument& command){
if (!this->connected()) this->_reconnect();
if (!this->connected()){
/*Arancino.free(_inputTopic);
Arancino.free(_outputTopic);
Arancino.free(_serviceTopic);*/
this->ifaceBegin();
}

if (this->connected()){
if(this->beginPublish(_outputTopic, measureMsgPack(command),false)){
BufferingPrint bufferedClient(*this->_client, 64);
/*BufferingPrint bufferedClient(*(this->_client), 64);
serializeMsgPack(command, bufferedClient);
bufferedClient.flush();
this->endPublish();
return;
bufferedClient.flush();*/
serializeMsgPack(command, *(this->_client));
//return;
}
this->endPublish();
}

Arancino.println("Failed to send message.");
}

bool MqttIface::receiveArancinoResponse(JsonDocument& response){
if (!this->connected()) this->_reconnect();

bool error = true;

if (this->connected()){
unsigned long startMillis = millis();
do {
this->loop();
delay(10);
} while(!_newIncomingMessage && (millis() < startMillis + TIMEOUT));


if (_newIncomingMessage)
{
error = (bool)deserializeMsgPack(response, (const char*)this->_payload, this->_length);
_newIncomingMessage = false;
unsigned long startMillis = millis();
do {
if (!this->connected()){
/*Arancino.free(_inputTopic);
Arancino.free(_outputTopic);
Arancino.free(_serviceTopic);*/
this->ifaceBegin();
}
this->loop();
delay(10);
} while(!_newIncomingMessage && (millis() < startMillis + TIMEOUT));


return error;
if (_newIncomingMessage)
{
error = (bool)deserializeMsgPack(response, (const char*)this->_payload, this->_length);
_newIncomingMessage = false;
}

return error; // Return error if client is not connected
Expand Down Expand Up @@ -287,13 +301,14 @@ void MqttIface::setPort(int port){
}

bool MqttIface::_reconnect(){
if (!this->connect(Arancino.id, _username, _password)){
//If debug is enabled, tell the user that connection failed
Arancino.print("Connection failed. RC=");
Arancino.println(this->state());
}

return this->connected();
//force disconnect
this->disconnect();
char willtopic[(strlen("arancino/service/connection_status/")+strlen(_daemonID)+1+strlen(Arancino.id)+1)];
strcpy(willtopic, "arancino/service/connection_status/");
strcat(willtopic, _daemonID);
strcat(willtopic, "/");
strcat(willtopic, Arancino.id);
return this->connect(Arancino.id, _username, _password,willtopic,2,0,"offline");
}

/******** Bluetooth interface *********/
Expand Down

0 comments on commit 20e6531

Please sign in to comment.