cyijun / ESP32MQTTClient

A thread-safe MQTT client for Arduino ESP32xx, based on ESP-IDF MQTT component. | Need help for adapting the latest mqtt component.
MIT License
15 stars 2 forks source link

Request for adding support for binary payload publishing in ESP32MQTTClient #10

Open kyour-cn opened 3 months ago

kyour-cn commented 3 months ago

Currently, the ESP32MQTTClient library only provides a publish method that accepts String type for both topic and payload. While this is suitable for text-based messages, it poses a problem when trying to publish binary data, such as image or sensor data, as it leads to potential data corruption and incorrect interpretation.

To address this issue, I kindly request the addition of a new publish method or an enhancement to the existing one to support binary payloads. This new method should accept a pointer to the binary data along with its length, allowing for the direct transmission of binary content over the MQTT protocol.

By doing so, the library will become more versatile and suitable for a wider range of applications that require the transmission of non-textual data.

Thank you for considering this enhancement to the ESP32MQTTClient library. Your efforts in creating and maintaining this library are highly appreciated, and I look forward to seeing this feature implemented in a future version.

Best regards, Kyour

kyour-cn commented 3 months ago

I have a code that passes a simple test.

bool ESP32MQTTClient::publishBinary(const char* topic, const uint8_t* payload, size_t payload_len, int qos, bool retain)
{
    // Do not try to publish if MQTT is not connected.
    if (!isConnected())
    {
        if (_enableSerialLogs)
            log_i("MQTT! Trying to publish when disconnected, skipping.");

        return false;
    }

    bool success = false;
    if (esp_mqtt_client_publish(_mqtt_client, topic, reinterpret_cast<const char*>(payload), payload_len, qos, retain) != -1)
    {
        success = true;
    }

    if (_enableSerialLogs)
    {
        if (success)
            log_i("MQTT << [%s] (binary data of size %zu)", topic, payload_len);
        else
            log_i("MQTT! publish failed");
    }

    return success;
}
cyijun commented 3 months ago

谢谢建议,会增加发布二进制消息的方法,添加publish的重载方法就可以了,不需要另外起名一个发布二进制的方法

kyour-cn commented 2 months ago

害,我还特地让gpt生成的issues,结果都是中国人啊😁