I have a sketch with some constant values depending on the board.
For testing purposes this value would be "Testboard".
When I'm done testing I want to change "Testing" into for example "Boat".
A few of those sketch lines look like this:
WiFi.setHostname("Testboard");
message = "ESP32C6 Testboard has restarted";
mqtt_client.publish("Testboard/Webasto/status", "ON", true);
if (mqtttopic == "Testboard/AllSensorUpdateFreq") {
Is there a kind of "#define" which can be used to accomplish this?
Yep, looked at that option but as I have 43 lines where the board name should be changed it would enlarge the sketch quite a bit.
If there's no other option I would rather use the find&replace method.
Thanks!
Sorry @J-M-L, guess your answer was posted while I was answering @robtillaart and for some reason I missed your post.
Your suggestion works!
WiFi.setHostname("Testboard");
message = "ESP32C6 Testboard has restarted";
mqtt_client.publish("Testboard/Webasto/status", "ON", true);
if (mqtttopic == "Testboard/AllSensorUpdateFreq") {
is now:
WiFi.setHostname(BOARDNAME);
message = "ESP32C6 " BOARDNAME " has restarted";
mqtt_client.publish(BOARDNAME "/Webasto/status", "ON", true);
if (mqtttopic == BOARDNAME "/AllSensorUpdateFreq") {
I tried a combination of "#define " and "strcat()" before without success.
Always came back with an error but your solution is simple and works perfectly.
Thanks all for helping
It's an old trick ā the original C compiler written by Dennis Ritchie at Bell Labs already supported adjacent string literal concatenation in the early 1970s.
One key technical reason was to allow the preprocessor to generate strings in pieces as you required.
#define NAME "UART"
printf("Device: " NAME);
This mechanism avoids runtime concatenation and produces a single static string object.
Another important early motivation was source formatting. C did not originally allow multi-line string literals directly, so programmers could write
"long text part 1 "
"part 2 "
"part 3"
instead of using escape sequences or complex continuation rules.