Comments (1)
bool MQTTClient::loop() {
if (!this->connected()) {
Serial.println("[MQTT] return immediately if not connected.");
return false;
}
// [MQTT] get available bytes on the network: 0
int available = this->netClient->available();
// Serial.print("[MQTT] get available bytes on the network: ");
// Serial.println(available);
// yield if data is available
if (available > 0) {
this->_lastError = lwmqtt_yield(&this->client, available, this->timeout);
if (this->_lastError != LWMQTT_SUCCESS) {
Serial.println("[MQTT] if available bytes on the network is available, close connection.");
this->close();
switch(this->_lastError)
{
case LWMQTT_SUCCESS: // = 0,
Serial.println("[MQTT] LWMQTT_SUCCESS.");
break;
case LWMQTT_BUFFER_TOO_SHORT: // = -1,
Serial.println("[MQTT] LWMQTT_BUFFER_TOO_SHORT.");
break;
case LWMQTT_VARNUM_OVERFLOW: // = -2,
Serial.println("[MQTT] LWMQTT_VARNUM_OVERFLOW.");
break;
case LWMQTT_NETWORK_FAILED_CONNECT: // = -3,
Serial.println("[MQTT] LWMQTT_NETWORK_FAILED_CONNECT.");
break;
case LWMQTT_NETWORK_TIMEOUT: // = -4,
Serial.println("[MQTT] LWMQTT_NETWORK_TIMEOUT.");
break;
case LWMQTT_NETWORK_FAILED_READ: // = -5,
Serial.println("[MQTT] LWMQTT_NETWORK_FAILED_READ.");
break;
case LWMQTT_NETWORK_FAILED_WRITE: // = -6,
Serial.println("[MQTT] LWMQTT_NETWORK_FAILED_WRITE.");
break;
case LWMQTT_REMAINING_LENGTH_OVERFLOW: // = -7,
Serial.println("[MQTT] LWMQTT_REMAINING_LENGTH_OVERFLOW.");
break;
case LWMQTT_REMAINING_LENGTH_MISMATCH: // = -8,
Serial.println("[MQTT] LWMQTT_REMAINING_LENGTH_MISMATCH.");
break;
case LWMQTT_MISSING_OR_WRONG_PACKET: // = -9,
Serial.println("[MQTT] LWMQTT_MISSING_OR_WRONG_PACKET.");
break;
case LWMQTT_CONNECTION_DENIED: // = -10,
Serial.println("[MQTT] LWMQTT_CONNECTION_DENIED.");
break;
case LWMQTT_FAILED_SUBSCRIPTION: // = -11,
Serial.println("[MQTT] LWMQTT_FAILED_SUBSCRIPTION.");
break;
case LWMQTT_SUBACK_ARRAY_OVERFLOW: // = -12,
Serial.println("[MQTT] LWMQTT_SUBACK_ARRAY_OVERFLOW.");
break;
case LWMQTT_PONG_TIMEOUT: // = -13,
Serial.println("[MQTT] LWMQTT_PONG_TIMEOUT.");
break;
}
return false;
}
}
// keep the connection alive
this->_lastError = lwmqtt_keep_alive(&this->client, this->timeout);
if (this->_lastError != LWMQTT_SUCCESS) {
Serial.println("[MQTT] keep the connection alive, close connection.");
this->close();
switch(this->_lastError)
{
case LWMQTT_SUCCESS: // = 0,
Serial.println("[MQTT] LWMQTT_SUCCESS.");
break;
case LWMQTT_BUFFER_TOO_SHORT: // = -1,
Serial.println("[MQTT] LWMQTT_BUFFER_TOO_SHORT.");
break;
case LWMQTT_VARNUM_OVERFLOW: // = -2,
Serial.println("[MQTT] LWMQTT_VARNUM_OVERFLOW.");
break;
case LWMQTT_NETWORK_FAILED_CONNECT: // = -3,
Serial.println("[MQTT] LWMQTT_NETWORK_FAILED_CONNECT.");
break;
case LWMQTT_NETWORK_TIMEOUT: // = -4,
Serial.println("[MQTT] LWMQTT_NETWORK_TIMEOUT.");
break;
case LWMQTT_NETWORK_FAILED_READ: // = -5,
Serial.println("[MQTT] LWMQTT_NETWORK_FAILED_READ.");
break;
case LWMQTT_NETWORK_FAILED_WRITE: // = -6,
Serial.println("[MQTT] LWMQTT_NETWORK_FAILED_WRITE.");
break;
case LWMQTT_REMAINING_LENGTH_OVERFLOW: // = -7,
Serial.println("[MQTT] LWMQTT_REMAINING_LENGTH_OVERFLOW.");
break;
case LWMQTT_REMAINING_LENGTH_MISMATCH: // = -8,
Serial.println("[MQTT] LWMQTT_REMAINING_LENGTH_MISMATCH.");
break;
case LWMQTT_MISSING_OR_WRONG_PACKET: // = -9,
Serial.println("[MQTT] LWMQTT_MISSING_OR_WRONG_PACKET.");
break;
case LWMQTT_CONNECTION_DENIED: // = -10,
Serial.println("[MQTT] LWMQTT_CONNECTION_DENIED.");
break;
case LWMQTT_FAILED_SUBSCRIPTION: // = -11,
Serial.println("[MQTT] LWMQTT_FAILED_SUBSCRIPTION.");
break;
case LWMQTT_SUBACK_ARRAY_OVERFLOW: // = -12,
Serial.println("[MQTT] LWMQTT_SUBACK_ARRAY_OVERFLOW.");
break;
case LWMQTT_PONG_TIMEOUT: // = -13,
Serial.println("[MQTT] LWMQTT_PONG_TIMEOUT.");
break;
}
return false;
}
return true;
}
I got the error: LWMQTT_BUFFER_TOO_SHORT
so
// Publish message
String payload;
serializeJson(doc, payload);// {"clientid":"84:FC:E6:00:89:18","D10":"LOW"}
// bool publish(const String &topic, const String &payload)
Serial.print("[MQTT] payload.length() = ");
Serial.println(payload.length());
I got logs below:
[MQTT] Payload:{"id":"ESP32C364E83389CDC0","D0":false,"D1":false,"D2":false,"D3":false,"D4":false,"D5":false,"D6":false,"D7":true,"D8":false,"D9":true,"D10":true}
[MQTT] payload.length() = 147
I know, the buffer is too short, because the default value is 128,
MQTTClient mqtt;
change to
MQTTClient mqtt(256);
so the bug above is fixed.
from arduino-mqtt.
Related Issues (20)
- Issue with latest lwmqtt and the separation of the header and the payload HOT 4
- MQTT reconnection when not disconnected. ESP8266 HOT 6
- Unreliable behaviour with reconnections HOT 3
- MQTT HOT 1
- Websockets support HOT 3
- MQTT SSL connection issue with QoS1 and QoS2 messages HOT 1
- DCU: cannot connect to local MQTT broker HOT 1
- This library uses some posix functions that are not provided for Uno R4 HOT 3
- Subscribe query HOT 1
- Release version `v2.5.1` different of master version HOT 1
- 0
- Error connect MQTT SSL/TLS with module CBU use platform libretiny
- RP2040 seems to work fine HOT 2
- payload length / reconnect HOT 1
- Random error in MQTTClient::loop() function HOT 2
- Cosmetics only in MQTTClient.h / MQTTClient(...)
- Wrong version in README.md?
- ESP32 PSRAM for MQTT buffer?
- MQTT5 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arduino-mqtt.