Closed joeyjojo747 closed 8 months ago
The payload data is specified by the length parameter.
My brute force snippet looks like
// Please note that you should always verify if the message's topic is the one you expect. // For example: if (memcmp(topic, "myCustomTopic") == 0) { ... }
char payload_data[MQQT_SENSOR_DATA_LENGTH_MAX];
strncpy(payload_data, (const char *)payload, length);
// terminate the payload_data as strncpy does not.
payload_data[length] = '\0';
Serial.print("Payload length = ");
Serial.print(length);
Serial.print(", Payload Data = ");
Serial.println(payload_data);
@joeyjojo747 The payload is a byte array without a null terminator at the end. You can't just treat it as string without any conversion.
char str[length + 1];
strncat(str, static_cast<const char*>(payload_data), length);
Serial.println(str);
I have successfully subscribed to a topic and can send payloads from HA. However, when trying to print the payload I get extra characters. See below examples of publishing 4 payload; 45, 1000, 3000, hello boys. onMqttMessage code below also --------- --------- --------- Serial monitor--------- --------- --------- New message on topic: aha/esp32_servoControl_TEST/servoAngle-HA Data: 45oTest/cmd_t New message on topic: aha/esp32_servoControl_TEST/servoAngle-HA Data: 100Test/cmd_t New message on topic: aha/esp32_servoControl_TEST/servoAngle-HA Data: 3000est/cmd_t New message on topic: aha/esp32_servoControl_TEST/servoAngle-HA Data: hello boysd_t
`void onMqttMessage(const char topic, const uint8_t payload, uint16_t length) { // This callback is called when message from MQTT broker is received. // Please note that you should always verify if the message's topic is the one you expect. // For example: if (memcmp(topic, "myCustomTopic") == 0) { ... }
}
void onMqttConnected() { //see setup() mqtt.onMessage(onMqttConnected); Serial.println("------ void onMqttConnected ------"); Serial.println("Connected to the broker!");
}`