Im complety surprised that this is such a huge problem in C and really pissed off I allready tried many examples in google like strcat() or whatever but it does not work as intended.
Does anybody of you has an idea or working example?
(Im doing this so i dont have to change X lines when the IP changes and only have to change mID in code)
strcat() and its variants do indeed work. So, it would be best if you posted the code where you attempted to use it. Someone here can then point out what you did wrong.
PS -- Post your code with Code Tags. If you don't know what that means, read the forum guidelines first.
Avoid strcat() as it can easily overflow, particularly in your case where you are adding multiple strings.
Instead use strlcat() which protects against overflows and crashes.
see this sketch strlcpy_strlcat.ino
for examples of how to use
Or for simple robust code use Arduino Strings and avoid memory problems
char mID[3] ="17";
String shelly;
String shellyOff;
char ip[] = "http://192.168.0.1";
char relais[] = "/relay/0? HTTP/1.1";
char off[] = "/relay/0?turn=off HTTP/1.1";
shelly = ip;
shelly += mID;
shelly += relais;
// then shelly.c_str() will return the char[ ] holding the result
char* result = shelly.c_str();
shellyOff = ip;
shellyOff += mID;
shellyOff += off;
// then shellyOff.c_str() will return the char[ ] holding the result
char* resultOff = shellyOff.c_str();
OR
if you want to keep using char[ ]s like shelly[128] you can wrap them in a SafeString which protects against buffer overflows and has nice error messages.
#include "SafeString.h"
char mID[3] ="17";
char shelly[128];
char shellyOff[128];
char ip[] = "http://192.168.0.1";
char relais[] = "/relay/0? HTTP/1.1";
char off[] = "/relay/0?turn=off HTTP/1.1";
cSFA(sfShelly,shelly); // wrap shelly in a SafeString, which directly updates shelly[ ] char array
sfShelly = ip;
sfShelly += mID;
sfShelly += relais;
// then shelly[..] will be holding the result
Serial.println(shelly);
cSFA(sfShellyOff,shellyOff); // wrap shellyOff in a SafeString, which directly updates shellyOff[ ] char array
sfShellyOff = ip;
sfShellyOff += mID;
sfShellyOff += off;
// then shellyOff[..] will be holding the result
Serial.println(shellyOff);