Hello y'all. So i have this program that reads bytes from flash memory on startup. Things i am storing on flash are mqtt credentials. I sucessfully wrote 170 bytes of info to flash and i painstakingly made sure every byte was correct. Now when I pull the bytes from flash, I am having a hard time making the data useful. I can get them into the String type fine, but the MQTT library I use requires a char * for the credentials.
Ive messed around all last night and today and I just cant seem to convert bytes to char . No matter what I try, the ESP board crashes when i print the char to serial. Take this sample code for example. it does exactly what my large program does to me....
char *someString = 0;
byte someByteArray[3];
void setup()
{
Serial.begin(115200);
while (!Serial){};
for (int i=0; i < 3; i++)
someByteArray[i] = 0;
someByteArray[0]=97;
someByteArray[1]=98;
someByteArray[2]=99;
for (int i = 0; i < 5; i++)
Serial.print(someByteArray[i]);
Serial.println("");
for (int i = 0; i < 5; i++)
someString = someString + char(someByteArray[i]);
Serial.println(someString);
}
void loop(){}
I get this for serial output. It gets through the loop of printing the someByteArray to serial but crashes before it prints the char* to serial.
// define a pointer to char, and point it at address 0 (where important things live....)
char *someString = 0;
// define a 3 byte array
byte someByteArray[3];
void setup()
{
Serial.begin(115200);
while (!Serial){};
// set all three bytes of someByteArray to zero
for (int i=0; i < 3; i++)
someByteArray[i] = 0;
// then immediately set them to 97, 98 and 99
someByteArray[0]=97;
someByteArray[1]=98;
someByteArray[2]=99;
// now print the values of the three bytes in someByteArray, plus WHATEVER is after it in memory
for (int i = 0; i < 5; i++)
Serial.print(someByteArray[i]);
Serial.println("");
// now set someString to some more or less useless random values
// someString will be set to 97, then 195, then 294, then two UNKNOWN
// values whatever is in memory after someByteArray
for (int i = 0; i < 5; i++)
someString = someString + char(someByteArray[i]);
// now try to print whatever memory someString is pointing to, and nobody know what is actually there
Serial.println(someString);
}
void loop(){}
In short, you are pretty much randomly changing the value of a pointer, but never actually pointing it to anything that remotely resembles a printable string.
Pointers must be initialized to point to valid memory address before you attempt to use them
Do not try to read or write or write 5 bytes from/to a 3-byte array
It's really not clear what you were trying to accomplish, but this code does nothing useful, and crashes because you are trying to read memory you don't "own".
Try explaining what it is you're actually trying to do.
someString = someString + char(someByteArray[i]);Well here you are treating a char* as if it's a String, but it's not a 'String'. You can not 'just add to it' .
A char* is a pointer to a series of bytes for which you have declared memory space when you've decle=ared the variable.char *someString = 0;in your case you've declared 1 byte (plus a null terminator that gets conveniently added automatically) If you write beyond that size, you will be writing to addresses that belong to other variable & pointers. If you declare a space of sufficient size
char *someString = " ";[ // or
char someString[10] = 0;/code]You can write to locations that you have reserved (still not by simply adding to the c-string)
If you just want to add to a 'String' and then convert the result to a c-string afterwards, that is possible but not recommended.
OP just declared a pointer to address zero, which in most cases mean "nothing".
Oh yes i'm sorry i got confused withchar *someString = "\0";Anyway a pointer to address zero is definitely not something you want to write anything to.