First: A short excourse about c-strings
In C a string is an array of characters. Such an array is defined with
char msg[10];
10
being the size of the array. Every valid c-string needs to be terminated with the null character \0
. This character is not printable, thus you don't see it normally. But the standard string functions (like printf()
, strlen()
, etc) use the null character to find the end of the string. So when handling strings as char
arrays, you absolutely always need to terminate the string with the null character. The standard functions are incrementing through the array, until they find the null character. If it is missing, they don't stop at the end of the array.
Needing the null character also means, that the above char
array can only hold 9 characters (since the 10th is the null character).
The standard functions for creating a string (like printf()
and its siblings) already terminate the string correctly with the null character.
You are obviously trying to send an ASCII encoded string. But sensorval
is not a string, it is of type int
(a number). In C/C++ one does not simply implicitly convert a number to a string (please insert the image of boromir from Lord of the Rings here in your head). You actually have to say the compiler how to convert the number to a string. Also sensorval
cannot itself hold that string (as it is an integer number). For that you need a char
array (often called c-string) for saving all the characters of your string.
So first we need to define the char
array to hold our string:
char msg[10];
This creates a char
array with 10 elements, each one can hold a character of your string. So you have space for 9 characters. If you need more, you can increase that number.
Now we need to build a string in msg
, that represents the value of sensorval
. For that we can use sprintf()
, which lets us format a string out of data and save it in a char
array.
sprintf(msg, "%d", sensorval);
The first parameter is the char
array to put the string in. The second parameter is called format-string. That is a string literal, that defines the format. %d
represents an integer value; it is a placeholder for the actual data and describes, how it should be formatted (you can read about the different format options in the C++ documentation if you search for printf()
). The following parameters (here only one) are the data values for the placeholders in the format string. Let's take an example: Assume sensorval
is 1000. Then msg
will be:
1000\0
The \0
being the null character.
So in your code you would replace
const char *msg = sensorval;
with
char msg[10];
sprintf(msg, "%d", sensorval);
What you have done in your code:
This line
const char *msg = sensorval;
will NOT create a string. You are defining msg
as a pointer to a constant character (a pointer is a variable, that holds the address another variable in the memory, use google terms like "C pointers" for more info). And then you set the pointer to sensorval
, which of course is not a valid pointer (valid meaning pointing to something reasonable here). It just points somewhere in the memory.
The example, that you got it from uses:
const char *msg = "hello";
This works, because the compiler takes all string literals (the strings enclosed by the double quotes) and puts them fixed into the memory, somewhat like a variable without a name. So here the compiler instructs the microcontroller so save the string "hello"
in the memory. With the above line we define a pointer to the place in the memory, where this string is saved.
So this works good for string literals, but not, if you first need to actually build the string.