diff --git a/Adafruit_MQTT.cpp b/Adafruit_MQTT.cpp index 9cb0898..8d6beda 100644 --- a/Adafruit_MQTT.cpp +++ b/Adafruit_MQTT.cpp @@ -490,6 +490,7 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) { return len; } +// as per http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718040 uint8_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic, const char *data, uint8_t qos) { uint8_t *p = packet; @@ -499,6 +500,9 @@ uint8_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic, // fill in packet[1] last p+=2; + // topic comes before packet identifier + p = stringprint_P(p, topic); + // add packet identifier. used for checking PUBACK in QOS > 0 if(qos > 0) { p[0] = (packet_id_counter >> 8) & 0xFF; @@ -509,8 +513,6 @@ uint8_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic, packet_id_counter++; } - p = stringprint_P(p, topic); - memcpy(p, data, strlen(data)); p+=strlen(data); len = p - packet;