chars bytes strings and woes

Hi all. I am creating a wireless sensor device. The idea is that its a generic sketch that makes it unique based on the mac address of the device. Ive used a previous project that is successfully making use of the pubsubclient library to sent data from a BME280 sensor to a local MQTT server. So really I know that this does already work, but getting the mac address is where my lack of knowledge is tripping me up.

According to the info here

I will be receiving a byte array.

Id like to concat my other variables with this. in order to get something like clientName + macAddress (bob_00:11:22:33:44:55)

But char, char*, String etc. This is where I dont know enough. Im using char* mostly for the other items as you can see, and it works, but Ill be honest, I dont really know why Im using that over char[] other than it works, I havent tried changing it and I dont know if there is a benefit this way)

Other than the I assume I could have a char[] of the correct size and fill it with all the two char* items maybe?

Ive created a function using strcpy as you can see which works fine assuming everything passed in is the right type, which the mac address isnt.
Ive tried a few variations of the mac data type but I think I just to to convert it correctly. I think this is simple but beyond my knowledge.

I should also mention, Im only compiling this at the minute and havent tried to load it onto any device yet.

Any advice appreciated
Thanks

/*to do list

   set deepsleep time correctly

   ensure mac address is used to keep IDs unique
*/

/* Board: wemos D1 mini ( LOLIN D1 R2 & Mini)
   Upload: 921600
   CPU frequency:80Mhz
   Flash 4MB
   Debug port: disabled
   Debug Level: none
   lwlp variant: v2 lower memory
   vtables: flash
   exception: legacy
   erase flash: sketch and wifi settings
   ssl: all ciphers
*/

//pubsubclient.h file edited, max packet size from 128 to 256

#include <BME280I2C.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <ArduinoJson.h>

const char* wifi_ssid             = "ssid";
const char* wifi_password         = "password";
const char* mqtt_server           = "192.168.0.100";
const char* mqtt_user             = "esp";
const char* mqtt_password         = "password";
char* mqtt_publish_topic          = "home/temperature/";
char* clientName                  = "espSensor_";
byte  macAdd[6];
char* clientNameMac;
char* topicNameMac;
float temp(NAN), hum(NAN), pres(NAN);
float tempCalibration = 0;

unsigned long DEEP_SLEEP_TIME = 50000;  //set this value after testing
int PORT_SPEED = 9600;

BME280I2C::Settings settings(
  BME280::OSR_X1,
  BME280::OSR_X1,
  BME280::OSR_X1,
  BME280::Mode_Forced,
  BME280::StandbyTime_1000ms,
  //  BME280::Filter_Off,
  BME280::Filter_4,
  BME280::SpiEnable_False,
  0x76 // I2C address
);

BME280I2C bme(settings);

WiFiClient espClient;
PubSubClient client(mqtt_server, 1883, espClient);

void setup() {
  Serial.begin(PORT_SPEED);
  Serial.println("");
  Serial.print("Serial port opened @ ");
  Serial.println(PORT_SPEED);
  //SDA = D2 GPIO4, SCL = D1 GPIO5
  Wire.begin();
  Serial.println("i2c started");
  bool state = bme.begin();
  delay(1000);
  int BME_retries = 0;
  while (!state && BME_retries < 5) {
    Serial.println("unable to connect to BME1 sensor at 0x76");
    delay(5000);
    BME_retries++;
  }
  if (WiFi.status() != WL_CONNECTED) {
    setup_wifi();
  }
  Serial.println("connecting to mqtt server");
  client.setServer(mqtt_server, 1883);
  delay(10);
  macAdd = WiFi.macAddress();
  clientNameMac = concatFunction(clientName, macAdd, clientNameMac);
  topicNameMac  = concatFunction(mqtt_publish_topic, macAdd, topicNameMac);
  if (client.connect(clientNameMac, mqtt_user, mqtt_password)) {
    Serial.print("connected to MQTT server");
    //prepare mqtt payload
    Serial.println("");
    Serial.println("preparing MQTT payload...");
    Serial.println("");

    const size_t capacity = JSON_OBJECT_SIZE(3);
    DynamicJsonDocument doc(capacity);

    doc["temperature"] = temp;
    doc["humidity"]    = hum;
    doc["pressure"]    = pres;

    char buffer[capacity];
    serializeJson(doc, buffer, sizeof(buffer));

    sendValues(buffer, topicNameMac);

  } else {
    Serial.println("unable to connect to MQTT server");
    ESP.deepSleep(DEEP_SLEEP_TIME);
  }
  delay(1000);  //requires some time for MQTT publish
  ESP.deepSleep(DEEP_SLEEP_TIME);
}

void setup_wifi() {
  int wifi_retry_count = 0;
  Serial.println("setup wifi");
  WiFi.begin(wifi_ssid, wifi_password);
  delay(10);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
    wifi_retry_count++;
    if (wifi_retry_count > 60) {
      Serial.println("unable to connect to WIFI...!!!");
      ESP.deepSleep(DEEP_SLEEP_TIME);
    }
  }
  Serial.println("connected to WIFI");
}

char* concatFunction(char* firstChar, char* secondChar, char* resultChar) {
  strcpy(resultChar, firstChar);
  strcat(resultChar, secondChar);
  return resultChar;
}

float getValues() {
  Serial.println("getting sensor values");
  //get values from sensor
  BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
  BME280::PresUnit presUnit(BME280::PresUnit_Pa);

  bme.read(pres, temp, hum, tempUnit, presUnit);
  temp = temp + tempCalibration;
  pres = pres / 100;
  Serial.print("temp ");
  Serial.print(temp);
  Serial.print(" - ");
  Serial.print("pressure ");
  Serial.print(pres);
  Serial.print(" - ");
  Serial.print("humidity ");
  Serial.println(hum);
}

void sendValues(char* str, char* topic) {
  if (WiFi.status() == WL_CONNECTED && client.connected()) {
    if (client.publish(topic, str)) {
      Serial.println("publish succsessful");
    } else {
      Serial.println("connected to MQTT but publish was unsuccsessful");
    }
    delay(10);
  } else {
    Serial.println("not connected to wifi or MQTT server");
  }
}

void loop() {

}

I dont really know why Im using that over char[] other than it works,

They are the same.
byte  macAdd[6];if you want to convert these 6 bytes to a visual representation in HEX, you will have to create a c-string (char-array / char *) of sufficient size, that's where char-arrays come in handy, so you can define the size before actually filling them up.

char* clientNameMac;

//

clientNameMac = concatFunction(clientName, macAdd, clientNameMac);

//

char* concatFunction(char* firstChar, char* secondChar, char* resultChar) {
  strcpy(resultChar, firstChar);
  strcpy(resultChar, secondChar);
  return resultChar;
}

I would say this is only working by accident. 'clientNameMac' is not big enough to hold the information, and your function is probably overwriting other data.
hence i suggest you do char clientNameMac[20];  // whatever space you should need

Thanks, yes I realised I used strcpy twice rather than strcpy and strcat which I have changed and will test.

As for the byte to char.... this actually compiled. Is there any reason that this is a bad way to do it?

byte macByte[6];
char macAdd[6];

WiFi.macAddress(macByte);
  for (int i = 0; i < sizeof(macByte); i++) {
    macAdd[i] = (char)macByte[i];
  }

Would this give me the mac as expected or would it be in ascii ?

If thats all good, then I will just need to ensure that when putting that on the end of the existing variable, it actually does it properly and not just overwrites it or something.

As for the byte to char.... this actually compiled. Is there any reason that this is a bad way to do it?

Would this give me the mac as expected or would it be in ascii ?

It would be in Ascii.. 6 bytes becomes 6 bytes (a char is actually 'unsigned byte' ) and i am fairly sure you want it in HEX format, also you've forgotten about the 'null-terminator' so even if you'd want it in Ascii, it should be char macAdd[7];and your final line should be macAdd[7] = '\0';but as i said you, you probably want it in HEX, and ideally with ':' in between values. (i don't own a MAC so i am not fully into heir addresses)

realised I used strcpy twice rather than strcpy and strcat

Oh i didn't even see that, the problem is actually more that you are copying data to an area that the pointer is pointing to, but you are copying more than the space that has been reserved for it. strcpy doesn't check, and adds a 'null-terminator' at the end, but whatever data was right after the space that you did reserve may be overwritten.
Do a full printout of all the c-strings you declared and you may see some strange results. If other variables get overwritten strange effects and possible crashes are in waiting.

Brilliant advice. Ill do some tinkering. Thanks