I am trying to insert a string into a char with concatenation and not getting any love from the Arduino compiler. It is over my head. Here is the code:
error: invalid operands of types 'const char [6]' and 'const char*' to binary 'operator+'
char mqtt_door_open_topic[60] = "stat/"+strClient_id.c_str()+"/garage/door1/reedswitch";
^
I've search and tried several variations, but I just end up generating different errors. For instance, if I just use strClient_id instead of strClient_id.c_str(), I get this error:
error: array must be initialized with a brace-enclosed initializer
char mqtt_door_open_topic[60] = "stat/"+strClient_id+"/garage/door1/reedswitch";
^
doesn't define a String object but a character array (also called C string sometimes). On a character array you cannot use the "+" operator but you have to use the corresponding functions to manipulate it. Concatenating is done by the strcat function, you should use the strncat version.
I was not able to get done what I wanted with strcat or strncat. I could not find a way to concatenate char and Strings using these functions because they require the source and destination to be of type char and I could not figure out how to change a char to a String or vice versa. Very confusing. Then I found the toCharArray() method. It turned out to be the problem solver.
I can't do anything about that as far as I know. The wifi.macaddress() call returns a String. If Arduino had another library that give a wifi device's MAC address as a char array, I would love to hear about it.
I can't do anything about that as far as I know. The wifi.macaddress() call returns a String. If Arduino had another library that give a wifi device's MAC address as a char array, I would love to hear about it.
To be exact: The Arduino WiFi class doesn't have the macaddress() method so don't blame Arduino for someone else's fail.
Using the String class on the ESP8266 may be less of a problem than on the Arduino because the ESP8266 has much more RAM so wasting a few bytes for that class may be acceptable.