Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Receive long MQTT payload #1590

Merged
merged 3 commits into from Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions esphome/components/mqtt/mqtt_client.cpp
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions esphome/components/mqtt/mqtt_client.h
Expand Up @@ -250,6 +250,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_;
Expand Down