First of all, I'm probably mixing up terms because I'm confused about this topic. Please don't be too harsh on me
I'm working on some code that involves text (a url) to be sent to pushover.net.
I adapted code from this topic
The code involves Strings, strings(?), char, a pointer and piecing those together.
I'm not asking how to fix this code specifically but I hope someone can point me to a place where I can learn about the basics of these datatypes and why/how to use them. I haven't been able to find a source that explains the basic/general idea. So if you have a suggestion, thanks in advance.
Now for my code. This obviously doesn't work but I'm posting it to illustrate what I'm struggling with.
To my surprise, I actually have this code working now. However, it doesn't feel like this is the "proper" way. So I'd still like to learn more on the subject.
If you want to use the String class, what you have written in post #2 is fine. If you want to go for C strings, then the following may be of interest to you.
char URL[80]; // Make sure this array is large enough.
sprintf(URL, "token=%s&user=%s&message=%s", apitoken, userkey, poMessage);
Of course the last line should be longer, but you get the idea.
It's a lot for a beginner to try and understand... I'll try my best to explain.
char... a single character 'a' or 'A' or '1'... for example char myChar = 'X';
string... more than one char (often referred to as a C-string in the Arduino world)... and needs to be terminated with a '\0' (null) character... so for example char word [] = "abcde";
The '\0' is added automatically so the string above is actually 6 chars long. There are a bunch of standard C functions to handle strings.
pointer... this is actually not a type of variable. All variables are stored in memory... a pointer "points" to the location in memory of the variable. So this can be used to access the variable rather than it's name. Sometimes this can be really useful... you don't have to pass the variable itself.. you just pass the pointer.
String... well that's a bit more complicated. It's actually not just a variable, but a whole Class. It's like a string (as above), but comes with a bunch of built in methods to do stuff with the string. A lot of people on this forum don't like Strings... they can fill the memory if not used carefully.
I've actually been programming Arduino's for e few years now but it's like I'm stuck on "beginners level". So far I have managed to use workarounds and quick-and-dirty code. I would like to try to move on by for example understanding this topic.
As an example why:
I posted I had the code working but I'm already running into the next thing. I would like to send the temperature that is read from another function. But this messy and ugly code doesn't work:
OK... this may start a holy war, but IMHO avoid using Strings and just use tried and tested C-strings (lowercase s... strings). Everything you can do with a String, you can do with a string... there are many standard C functions for manipulating strings. The implementation of the Arduino String class has dumbed down something that is actually not that hard to understand.
char URL[] = "a bunch of text";
poMessageLength = URL.length();
Note: A character array doesn't have a 'length' method. You could use C string functions: poMessageLength = strlen(URL);
In this case you could use the array size but you have to remember that the array includes the terminator. poMessageLength = (sizeof URL) -1;
For now I have "solved" the problems in my code by sticking with Strings (uppercase) and I got it working. The challenge is to do the same using strings (lowercase).
I have some reading to do and I'll have a go at it.
I'm not asking anyone to convert this code for me though hints are still welcome. The code below is like my starting point from now.
I haven't managed to convert my code yet, let's say it's work in progress.
I did read up on some topics though and it starts making sense.
For anyone finding this topic and struggling with the same issue, I found that the link I posted earlier is a nice starting point: The Evils of Arduino Strings