Closed salvq closed 4 years ago
Does the disconnection happen instantly after sending the message? I have a similar problem where I send a message with QoS 1 and even though the client is disconnected (error code should be -9 or something similar) returns with error code 0. My code is very similar to yours, but I am using an ESP32 board
Yes, it does. I have not debugged more.
I changed to different MQTT client and works perfect with QoS 1, no more freeze or any other issues.
I do not remember exactly the situation however I have had several different scenarios where error codes were not showing status that I expect to show and therefore I would not rely on conditioning with the error codes.
@salvq Great info, thanks
Hi everyone! I just checked this with a ESP32 board, the latest Arduino IDE and latest ESP32 board plugin. QoS1 and QoS2 work as expected with "broker.shiftr.io" and "mqtt.eclipse.org". Despite the slow network I'm currently using, I see no freezes or delays. Hence, the issues you're describing must be caused by the underlying network stack of the board.
Unfortunately, I do not have access to a MRK GSM 1400 to debug this myself. Could you run one of the basic examples sketches (plus modifications for the GSM) and test if QoS 1 and 2 are working? At best also against the "broker.shiftr.io" and "mqtt.eclipse.org" brokers.
@diegoamayaw If the client is disconnected it won't publish the message and just return. You'r essentially reading the error value of the last command that did do something, in this case client.disconnect()
.
@256dpi Thanks for the comment. So let me get this straight, if I send a message while disconnected I won't get the error code of the sent message, but rather the error code of the last executed command, which was client.disconnect()
?
Yes, that's because lastError
returns just the last encountered LWMQTT error (hence the prefix "last"). The idea is to always check the returned bool
to see if the commands was successful and then check the client.lastError
for further info.
Here the current code for publish
:
bool MQTTClient::publish(const char topic[], const char payload[], int length, bool retained, int qos) {
// return immediately if not connected
if (!this->connected()) {
return false;
}
// prepare message
lwmqtt_message_t message = lwmqtt_default_message;
message.payload = (uint8_t *)payload;
message.payload_len = (size_t)length;
message.retained = retained;
message.qos = lwmqtt_qos_t(qos);
// publish message
this->_lastError = lwmqtt_publish(&this->client, lwmqtt_string(topic), message, this->timeout);
if (this->_lastError != LWMQTT_SUCCESS) {
// close connection
this->close();
return false;
}
return true;
}
In the future I might redesign that into a general error API that always sets an appropriate error.
@256dpi Thanks for the info, it's all much clearer now
@256dpi there has been an issue with power distribution in MKRGSM 1400 board, issue. Therefore this issue with QoS1 I have been facing might be related to that. Let me try this when I get the new replaced board...
I guess this issue is related to #188. Are you also calling client.publish
in the messageCallback
? If so, this will potentially deadlock the client with QoS > 0 and sometimes also even with QoS == 0.
Closing for this now. Please reopen if problem persists.
I have been doing some tests using QoS1 instead of QoS0 and getting boards freeze after 1 msg sent to broker.
I am using Arduino MKR GSM 1400.
If
condition does not help either, same result.Is there any settings wrong ? Msg sizes, buffers, timing etc. ? What to change to debug more ?
Thanks
Code
QoS0: no issue sending / receiving
QoS1: 1st msg sent, received by broker but boards hangs up / freeze, no more msg