knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.78k stars 1.46k forks source link

Feature request: subscribe_P #754

Open fsommer1968 opened 4 years ago

fsommer1968 commented 4 years ago

As topics can be long (e.g. with Bluemix ;-) ), a subscribe function that allows the topic to be loaded from PROGMEM (similar to publish_P) would be helpful. Please check if this is possible.

Erriez commented 3 years ago

I was looking for the same question. It is currently not implemented in the PubSubClient library, so I made a workaround outside the library:

const char MQTT_TOPIC_OUT[] PROGMEM = "outTopic";
const char MQTT_TOPIC_IN[] PROGMEM = "inTopic";

void mqttPublish()
{
    char bufferOut[sizeof(MQTT_TOPIC_OUT)];

    memcpy_P(bufferOut, MQTT_TOPIC_OUT, sizeof(MQTT_TOPIC_OUT));
    client.publish(bufferOut, payload);
}

void mqttSubscribe()
{
    char bufferIn[sizeof(MQTT_TOPIC_IN)];

    memcpy_P(bufferIn, MQTT_TOPIC_IN, sizeof(MQTT_TOPIC_IN));
    client.subscribe(bufferIn);
}

Same buffer copy can be done for payload.

Please note that publish_P() is currently broken. See issue here: https://github.com/knolleary/pubsubclient/issues/747