Extract the contents of the payload as a string

Below is the modified code from the void callback() routine in the library PubSubClient, which is an MQTT library.

I use the void callback() to extract the topic as a string from
void callback(char* topic, byte* payload, unsigned int length)

I can extract the char* topic sucessfully.
eg “groundfloor/livingroom/temperature_DS18B20”
as shown by the various topics I pull out between lines 15 and 32.

I am however unable to extract the contents of the payload as a string.
The contents of payload is either a number of format “00.00” or “yes” or “no”.
Does anyone know how to extract the contents of payload as a string?

All my numerous comments below show I have tried various things.

This is the complete contents of void callback.

void callback(char* topic, byte* payload, unsigned int length) {


  tft.setTextSize(2);
    
  char *cstring = (char *) payload;
  cstring[length] = '\0';
  

  int strcomparison = strcmp(topic, "groundfloor/livingroom/temperature_DS18B20");
    if (strcomparison == 0) { Serial.print("livingroom\t\t\t");  tft.setCursor(5, 27); tft.print("livingrm (deg C)    "); } 
  
      strcomparison = strcmp(topic, "groundfloor/livingroom/halogenheating_on_off");
    if (strcomparison == 0) { Serial.print("livingroom/halogenheating\t"); tft.setCursor(5, 63); tft.print("livingrm/halheating ");  } 
  
      strcomparison = strcmp(topic, "groundfloor/bedroom4/temperature_DS18B20");
    if (strcomparison == 0) { Serial.print("bedroom4\t\t\t");  tft.setCursor(5, 99);  tft.print("bedrm4 (deg C)      ");  } 
  
      strcomparison = strcmp(topic, "groundfloor/bedroom4/halogenheating_on_off");
    if (strcomparison == 0) { Serial.print("bedroom4/halogenheating\t\t");  tft.setCursor(5, 135);   tft.print("bedrm4/halheating   "); } 

      strcomparison = strcmp(topic, "groundfloor/centralheating_on_off");
    if (strcomparison == 0) { Serial.print("centralheating\t"); tft.setCursor(5, 171); tft.print("centralheating      ");  } 
  

   
  for (int i=0;i<length;i++) {
    
    Serial.print((char)payload[i]);
    


    
    //tft.print((char)payload[i]);

    //payload[length] = '\0';
    //String strPayload = String((char*)payload);
    //topic.remove(7);       // Remove last 7 characters from groundfloor/bedroom4/halogenheating_on_off
    //strPayload.remove(4, 30); // remove first 22 characters from groundfloor/bedroom4/halogenheating_on_off
    //tft.print(strPayload);


  
    //strcomparison = strcmp(topic, "groundfloor/livingroom/temperature_DS18B20");
    //if (strcomparison == 0) { Serial.print("livingroom\t\t\t");  tft.setCursor(5, 27); tft.print("livingrm (deg C)    "); } 

    //strcomparison = strcmp(topic, "groundfloor/bedroom4/temperature_DS18B20");
    //if (strcomparison == 0) { Serial.print("bedroom4\t\t\t");  tft.setCursor(5, 45);  tft.print("bedrm4 (deg C)      ");  } 
 

   
    //tft.print((char)payload[i]);

    //char *cstring = (byte *) payload;
    //cstring[length] = '\0';

    // String string2 = "";
    // string2 = ((char)payload[i]);

    
    //strcomparison = strcmp(string2, "On");
    
    
    //if (strcomparison == 0)
    //                          { tft.print("On "); } 
    //                     else { tft.print((char)payload[i]);  } 

                           
    //Serial.print(string2);
    //if (string2 == "On")
    //                            { tft.print("On "); } 
    //                       else { tft.print((char)payload[i]);  } 




 char message[21];
  if (strcmp(topic,"groundfloor/bedroom4/halogenheating_on_off")==0) {     strncpy(message, (char *)payload, 20);   }
  if (strcmp(message,"On ")==0) {    Serial.println("On");    delay(50);           }
  if (strcmp(message,"Off")==0) {    Serial.println("Off");   message[0] = '\0';   }




   // if ((char)payload[0] == 'On') 
   //                             {   } 
   //                             else 
   //                             {   }



   /////   result += String(payload[i]);
   //    if (i < 5)
   //      result += ':';







    tft.print((char)payload[i]);  
    
    //Serial.println(string2);
      
    //tft.print(numz);

    
    }


    /////-- result.remove(0, 3); // remove first 22 characters from groundfloor/bedroom4/halogenheating_on_off
    //return result;
    /////-- Serial.println(result);

    /////////////////////// strcomparison = strcmp(payload, "On");
    ///////////////////////   if (strcomparison == 0) { Serial.print("On");  } 
  
    
    //Serial.write(12);

    Serial.println();
    tft.println();    tft.println();

}

Here is the routine/ function with all the potentially confusing comments cut out.
If you want to see what I have tried see post above.

This compiles with the sketch just fine, I just can’t get the payload out.

The bit that does not do what I would like it to do is between lines 32 and 35.

void callback(char* topic, byte* payload, unsigned int length) {


  tft.setTextSize(2);
    
  char *cstring = (char *) payload;
  cstring[length] = '\0';
  

  int strcomparison = strcmp(topic, "groundfloor/livingroom/temperature_DS18B20");
    if (strcomparison == 0) { Serial.print("livingroom\t\t\t");  tft.setCursor(5, 27); tft.print("livingrm (deg C)    "); } 
  
      strcomparison = strcmp(topic, "groundfloor/livingroom/halogenheating_on_off");
    if (strcomparison == 0) { Serial.print("livingroom/halogenheating\t"); tft.setCursor(5, 63); tft.print("livingrm/halheating ");  } 
  
      strcomparison = strcmp(topic, "groundfloor/bedroom4/temperature_DS18B20");
    if (strcomparison == 0) { Serial.print("bedroom4\t\t\t");  tft.setCursor(5, 99);  tft.print("bedrm4 (deg C)      ");  } 
  
      strcomparison = strcmp(topic, "groundfloor/bedroom4/halogenheating_on_off");
    if (strcomparison == 0) { Serial.print("bedroom4/halogenheating\t\t");  tft.setCursor(5, 135);   tft.print("bedrm4/halheating   "); } 

      strcomparison = strcmp(topic, "groundfloor/centralheating_on_off");
    if (strcomparison == 0) { Serial.print("centralheating\t"); tft.setCursor(5, 171); tft.print("centralheating      ");  } 
  

   
  for (int i=0;i<length;i++) {
    
  Serial.print((char)payload[i]);
    

  char message[21];
  if (strcmp(topic,"groundfloor/bedroom4/halogenheating_on_off")==0) {     strncpy(message, (char *)payload, 20);   }
  if (strcmp(message,"On ")==0) {    Serial.println("On");    delay(50);           }
  if (strcmp(message,"Off")==0) {    Serial.println("Off");   message[0] = '\0';   }


    tft.print((char)payload[i]);  
    
    }



    Serial.println();
    tft.println();    tft.println();

}
char *cstring = (char *) payload;
cstring[length] = '\0';

How do you know you can overwrite that byte at index length of the parameter? Why is the code not indented properly? What does your for loop print for the payload? Don't share snippets... we can't help much...

Where do you actually try to use the data that cstring points to? Why would you not print the data, to KNOW what you are dealing with?