I try to convert a numer of bytes containing ascii to a string to display them on a oled display.
The bytes come from a MQTT reciever.
The string is not being null-terminated but print a few extra characters.
void callback(char* topic, byte *payload, unsigned int length) {
//String stringOne[length];
//Serial.println("-------new message from broker-----");
Serial.print("channel: ");
Serial.print(topic);
Serial.print(" - data: ");
Serial.write(payload, length);
Serial.print(" - length: ");
Serial.println(length);
String stringOne = (char*)payload;
stringOne += '\0';
display.drawString(0, 10, "Instelling: "+stringOne);
Serial.println("Instelling: "+stringOne);
//}
}
the output I get is:
channel: ketel/modulationLevel - data: 0.00 - length: 4
Instelling: 0.00a122
channel: th/pv - data: 19.44 - length: 5
Instelling: 19.44ationLevel
I tried to put a \0 at the end of the string but that does not work obviously.
What am I doing wrong?