Skip to main content
Added link to referenced answer and then put code into full context.
Source Link
sa_leinad
  • 3.2k
  • 2
  • 24
  • 53

Marco's answerMarco's answer worked for me, but worked better with:

payload[length] = 0;
String recv_payload = String(( char *) payload);

Therefore, the full code would be:

void mqtt_callback(char* topic, byte* payload, unsigned int length) {
  String topicStr = topic; 
  payload[length] = 0;
  String recv_payload = String(( char *) payload);

  Serial.println( "mqtt_callback - message arrived - topic [" + topicStr + 
                  "] payload [" + recv_payload + "]" );
}

Marco's answer worked for me, but worked better with:

payload[length] = 0;
String recv_payload = String(( char *) payload);

Marco's answer worked for me, but worked better with:

payload[length] = 0;
String recv_payload = String(( char *) payload);

Therefore, the full code would be:

void mqtt_callback(char* topic, byte* payload, unsigned int length) {
  String topicStr = topic; 
  payload[length] = 0;
  String recv_payload = String(( char *) payload);

  Serial.println( "mqtt_callback - message arrived - topic [" + topicStr + 
                  "] payload [" + recv_payload + "]" );
}
Source Link

Marco's answer worked for me, but worked better with:

payload[length] = 0;
String recv_payload = String(( char *) payload);