Receive long MQTT payload (#1590)

This commit is contained in:
Gareth Cooper 2021-04-08 13:26:34 +01:00 committed by GitHub
parent eaf9735eda
commit e6f8e73705
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View File

@ -25,9 +25,17 @@ void MQTTClientComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up MQTT...");
this->mqtt_client_.onMessage([this](char *topic, char *payload, AsyncMqttClientMessageProperties properties,
size_t len, size_t index, size_t total) {
std::string payload_s(payload, len);
std::string topic_s(topic);
this->on_message(topic_s, payload_s);
if (index == 0)
this->payload_buffer_.reserve(total);
// append new payload, may contain incomplete MQTT message
this->payload_buffer_.append(payload, len);
// MQTT fully received
if (len + index == total) {
this->on_message(topic, this->payload_buffer_);
this->payload_buffer_.clear();
}
});
this->mqtt_client_.onDisconnect([this](AsyncMqttClientDisconnectReason reason) {
this->state_ = MQTT_CLIENT_DISCONNECTED;

View File

@ -259,6 +259,7 @@ class MQTTClientComponent : public Component {
};
std::string topic_prefix_{};
MQTTMessage log_message_;
std::string payload_buffer_;
int log_level_{ESPHOME_LOG_LEVEL};
std::vector<MQTTSubscription> subscriptions_;