How to deal with a very long string without const declaration

Hi all,

I plan to send/publish datas (which collected from Arduino) to a MQTT broker. The MQTT library I used is knolleary's PubSubClient. It works well so far with few brokers I've tested like Mosquitto test broker, IBM IoT Platform, and ActiveMQ. My Arduino can publish datas successfully to those brokers.

But the actual broker in the end that I have to connect is needed Oauth authorization, which means an authorized password has to be provided within the connection. I have written a java program at my local computer which can generate the authorized password and connect to that designated broker.

Now I just hard code the generated password to my Arduino sketch, which came out to be a very long string... despite hard code, the random generated password is still that big size... (about 332 characters)
So I got two questions here.

#1
If one very long string is declared, why the serial output will show some unreadable characters like this:

QXV0aG9yaXphdGlvbjogT0F1dGggb2F1dGhfY29uc3VtZXJfa2V5PSJlbGluMjAxNS5Qcm9qZWN0MSIsIG9hdXRoX3NpZ25hdHVyZ�!�QXV0aG9yaXphdGlvbjogT0F1dGggb2F1dGhfY29uc3VtZXJfa2V5PSJlbGluMjAxNS5Qcm9qZWN0MSIsIG9hdXRoX3NpZ25hdHVyZ�!�QXV0aG9yaXphdGlvbjogT0F1dGggb2F1dGhfY29uc3VtZXJfa2V5PSJlbGluMjAxNS5Qcm9qe9/e99o$t�5hdHVyZ�!�QXV0a

Below is the long string (about 332 characters) and print code.

String password = "QXV0aG9yaXphdGlvbjogT0F1dGggb2F1dGhfY29uc3VtZXJfa2V5PSJlbGluMjAxNS5Qcm9qZWN0MSIsIG9hdXRoX3NpZ25hdHVyZV9tZXRob2Q9IkhNQUMtU0hBMSIsIG9hdXRoX3NpZ25hdHVyZT0iSlZCSnEwJTJCdGJYZE1rcFJZenVIeEJkT1VDcW8lM0QiLCBvYXV0aF90aW1lc3RhbXA9IjE0MzgwNzExNDYiLCBvYXV0aF9ub25jZT0iMTQzODA3MTE0NiIsIG9hdXRoX3ZlcnNpb249IjEuMCI7Q2xpZW50LVZlcnNpb246IFN0YWdlQlE=";

Serial.println(password);

#2
I guess the root cause of #1 may be that serial output is restricted from Arduino's RAM size. In my case, I use Arduino UNO REV3which only has 2KB RAM. I checked PROGMEM which declares the string as a const string. But I cannot deal with const string, it must be a string that can be modified. So, how can I handle a very long string without const declaration?

If one very long string is declared, why the serial output will show some unreadable characters like this:

Even though the variable type is char (I'm guessing because, in spite of the fact that there are read-me-before-posting-here topics, you didn't read them or you didn't think they applied to you (they do!)), the value does not have to represent a printable character.

Below is the long string (about 332 characters) and print code.

Rubbish. That is a String instance, wrapping a long string. Quit pissing away resources wrapping a fixed length array of chars.

I guess the root cause of #1 may be that serial output is restricted from Arduino's RAM size.

It's silly to guess. That is not even close to the reason.

But I cannot deal with const string, it must be a string that can be modified.

Why? Are you planning on having the Arduino generate that string?

Even with only 2048 bytes of memory, you CAN dedicate 332 of them for a specific purpose. That's probably not necessary, but we don't yet know.

nice question. maybe use datalogger sd card ? could be the solution #1 dont use uno #2 update mega..

You do have rights to delete this post.

No, he doesn't. 8)

However, you have the right the read the "please read these guidelines before posting" posts, and apply what you read there.

Hi PaulS,

I apologized for cross-posting, and not thinking too much posting a question here.. wasting your time. Thanks giving me such a punch that I'd better realize I deserved it.. Thanks for your suggestions anyway.

And yes, I have to make Arduino to randomly generate a big string. So using PROGMEM is not suitable for this. By your saying I learned that 332 bytes is totally OK with a 2048 bytes RAM. Otherwise I probably have to extend the RAM size on Arduino.

Otherwise I probably have to extend the RAM size on Arduino.

The only way to do that is to buy a different Arduino.

Thanks.