Closed huster-songtao closed 3 months ago
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.
Board:XIAO ESP32C3
The mqtt.loop() always print false.
[MQTT] if available bytes on the network is available, close connection. [MQTT] mqtt.loop() == false. [MQTT] connecting mqtt...
mqtt.loop() should be true after it connected.