Need guidance regarding data handling with various variable types

I am working on a IOT project to publish sensor data via MQTT. I have following data from the ESP8266 device. All the values for variables will be changed dynamically by the code.

float voltage = 2.62;
const char* room = "livingroom";

const char* sensorType1 = "temperature"; const char* sensorType2 = "humidity"; const char* sensorType3 = "pressure"; const char* sensorType4 = "light";
int sensorValue1 = 25; int sensorValue2 = 50; int sensorValue3 = 1000; int sensorValue4 = 80; 
const char* statusType1 = "rssi"; statusType2 = "mode"; statusType3 = "ip"; statusType4 = "channel"; statusType5 = "sleeptime"; statusType6 = "uptime";
int deviceStatus1 = -67; int deviceStatus2 = 1; int deviceStatus3 = 8;  int deviceStatus4 = 7; int deviceStatus5 = 10; int deviceStatus6 = 12;

From data generated above by code I wanted to make one String named "sensorData" and publish it via MQTT like shown below:

{"room":"Livingroom","voltage":2.62,"temperature":25,"humidity":50,"pressure":1000,"light":80,"rssi":-67,"mode":1,"ip":8,"channel":7,"sleeptime":10,"uptime":12}

myBroker.publish(topic, (String)sensorData);

I do not know if it is possible to create such a string without using external library? if not what library can be used to achieve the goal?

Thank you for you help.

one approach is to sprintf() to format a sub-string for each value and use strcat() to append it to a larger final string

I tried using sprintf() but it did not work. Can you help me by showing how it can be done?

Thanks.

can you post what you tried?

I have following working with sprintf() seperately splitting MQTT messages but I wanted to combine all messages in one string:

sprintf(topic1, "%s%s%s%s", room, "/", "voltage", "");
        sprintf(topic2, "%s%s%s%s", room, "/", sensorType1, "");
        sprintf(topic3, "%s%s%s%s", room, "/", sensorType2, "");
        sprintf(topic4, "%s%s%s%s", room, "/", sensorType3, "");
        sprintf(topic5, "%s%s%s%s", room, "/", sensorType4, "");
  
        myBroker.publish(topic1, (String)voltage);
        myBroker.publish(topic2, (String)sensorValue1);
        myBroker.publish(topic3, (String)sensorValue2);
        myBroker.publish(topic4, (String)sensorValue3);
        myBroker.publish(topic5, (String)sensorValue4);
        
        sprintf(topic1, "%s%s%s%s", location, "/", "rssi", "");
        sprintf(topic2, "%s%s%s%s", location, "/", "mode", "");
        sprintf(topic3, "%s%s%s%s", location, "/", "ip", "");
        sprintf(topic4, "%s%s%s%s", location, "/", "channel", "");
        sprintf(topic5, "%s%s%s%s", location, "/", "sleeptime", "");
        sprintf(topic6, "%s%s%s%s", location, "/", "uptime", "");

My whole code is here: https://github.com/happytm/BatteryNode/blob/master/ProbeReceiver.ino

Thanks.

somewhat awkward, but systematic

#include <stdio.h>
#include <string.h>

float voltage = 2.62;
const char* room = "livingroom";
const char* sensorType1 = "temperature";
const char* sensorType2 = "humidity";
const char* sensorType3 = "pressure";
const char* sensorType4 = "light";

int sensorValue1 = 25;
int sensorValue2 = 50;
int sensorValue3 = 1000;
int sensorValue4 = 80;

const char* statusType1 = "rssi";

const char *statusType2 = "mode";
const char *statusType3 = "ip";
const char *statusType4 = "channel";
const char *statusType5 = "sleeptime";
const char *statusType6 = "uptime";

int deviceStatus1 = -67;
int deviceStatus2 = 1;
int deviceStatus3 = 8;
int deviceStatus4 = 7;
int deviceStatus5 = 10;
int deviceStatus6 = 12;

// -----------------------------------------------------------------------------
char *
fmtStr (void)
{
    static char str [400] = {};
    char s [40];

    sprintf (s,  "\"%s\":\"%s\"", "room", room);    strcat (str, s);
    sprintf (s, ",\"%s\":\"%.2f\"", "voltage", voltage);    strcat (str, s);

    sprintf (s, ",\"%s\":\"%d\"", sensorType1, sensorValue1); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", sensorType2, sensorValue2); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", sensorType3, sensorValue3); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", sensorType4, sensorValue4); strcat (str, s);

    sprintf (s, ",\"%s\":\"%d\"", statusType1, deviceStatus1); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", statusType2, deviceStatus2); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", statusType3, deviceStatus3); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", statusType4, deviceStatus4); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", statusType5, deviceStatus5); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", statusType6, deviceStatus6); strcat (str, s);
}

// -----------------------------------------------------------------------------
int
main  ()
{
    printf ("%s\n", fmtStr ());
}

i would typically create a table with a (void*) ptr to the value or string, a lable (string) and a type (string, float, integer) and then have a function to create the sub-string from the table entry and append it to the final string.

Wow! I was trying different ways for last couple of days. This was very fast solution.

Thank you so much @gcjr.

for anyone using it with ESP8266 I ended up using it like below:

float voltage = 2.62;
const char* room = "livingroom";
const char* sensorType1 = "temperature";
const char* sensorType2 = "humidity";
const char* sensorType3 = "pressure";
const char* sensorType4 = "light";

int sensorValue1 = 25;
int sensorValue2 = 50;
int sensorValue3 = 1000;
int sensorValue4 = 80;

const char* statusType1 = "rssi";

const char *statusType2 = "mode";
const char *statusType3 = "ip";
const char *statusType4 = "channel";
const char *statusType5 = "sleeptime";
const char *statusType6 = "uptime";

int deviceStatus1 = -67;
int deviceStatus2 = 1;
int deviceStatus3 = 8;
int deviceStatus4 = 7;
int deviceStatus5 = 10;
int deviceStatus6 = 12;

// -----------------------------------------------------------------------------
void setup()
{
  Serial.begin(115200);
  Serial.println();
  Serial.println();


    static char str [400] = {};
    char s [40];

    sprintf (s,  "\"%s\":\"%s\"", "room", room);    strcat (str, s);
    sprintf (s, ",\"%s\":\"%.2f\"", "voltage", voltage);    strcat (str, s);

    sprintf (s, ",\"%s\":\"%d\"", sensorType1, sensorValue1); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", sensorType2, sensorValue2); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", sensorType3, sensorValue3); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", sensorType4, sensorValue4); strcat (str, s);

    sprintf (s, ",\"%s\":\"%d\"", statusType1, deviceStatus1); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", statusType2, deviceStatus2); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", statusType3, deviceStatus3); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", statusType4, deviceStatus4); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", statusType5, deviceStatus5); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", statusType6, deviceStatus6); strcat (str, s);
    
    Serial.println(str);
    
    delay(5000);

    

}  // end Setup.

void loop()
{
sensorData();
    
}

void sensorData()  {
  
    static char str [400] = {};
    char s [40];
    
    sprintf (s, "{"); strcat (str, s);
    sprintf (s, "\"%s\":\"%s\"", "room", room);    strcat (str, s);
    sprintf (s, ",\"%s\":\"%.2f\"", "voltage", voltage);    strcat (str, s);

    sprintf (s, ",\"%s\":\"%d\"", sensorType1, sensorValue1); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", sensorType2, sensorValue2); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", sensorType3, sensorValue3); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", sensorType4, sensorValue4); strcat (str, s);

    sprintf (s, ",\"%s\":\"%d\"", statusType1, deviceStatus1); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", statusType2, deviceStatus2); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", statusType3, deviceStatus3); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", statusType4, deviceStatus4); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", statusType5, deviceStatus5); strcat (str, s);
    sprintf (s, ",\"%s\":\"%d\"", statusType6, deviceStatus6); strcat (str, s);
    sprintf (s, "}"); strcat (str, s);
    
    Serial.println(str);
    
    delay(50000);
}