user61413: I am doing some device serial data read and publishing values to MQTT. Using ESP8266 for this.
Currenty I request serial device by sending a command and parse all byte values individually, how to do this in better way ?
each byte parse is done in such way:
float Heat = (float)data[38];


  sprintf(mqtt_topic, "%s/%s", mqtt_topic_base, "Temp"); mqtt_client.publish(mqtt_topic, String(Temp).c_str());

float Power = (float)data[39];
......
..... and etc...


user31105: In principle it is ok to use your way, but a simple improvement is to change the magic numbers (38, 39) to constants or define:
const int offset_heat = 38;

…

float Heat = (float)data[offset_heat];

or a define:
#define OFFSET_HEAT 38

…

float Heat = (float)data[OFFSET_HEAT];

If all values are consecutive you can keep a variable to read the values:
int offset = 38; // Normally starts at 0 assuming you read values from the start

float heat = (float) readByte(&offset);

…

int readByte(uint8_t* offset)
{
    int value = data[*offset];
    (*offset)++;
    return value;
}

This increases the offset after reading the value.
Some remarks:

It's a convention not to use capitals for local variables (so change Heat to heat)
You don't use the Heat variable in sprintf, maybe this is intentional.


61413: <p>I am doing some device serial data read and publishing values to MQTT. Using ESP8266 for this.</p>

<p>Currenty I request serial device by sending a command and parse all byte values individually, how to do this in better way ?</p>

<p>each byte parse is done in such way:</p>

<pre><code>float Heat = (float)data[38];


  sprintf(mqtt_topic, "%s/%s", mqtt_topic_base, "Temp"); mqtt_client.publish(mqtt_topic, String(Temp).c_str());

float Power = (float)data[39];
......
..... and etc...
</code></pre>
 31105: <p>In principle it is ok to use your way, but a simple improvement is to change the magic numbers (38, 39) to constants or define:</p>

<pre><code>const int offset_heat = 38;

…

float Heat = (float)data[offset_heat];
</code></pre>

<p>or a define:</p>

<pre><code>#define OFFSET_HEAT 38

…

float Heat = (float)data[OFFSET_HEAT];
</code></pre>

<p>If all values are consecutive you can keep a variable to read the values:</p>

<pre><code>int offset = 38; // Normally starts at 0 assuming you read values from the start

float heat = (float) readByte(&amp;offset);

…

int readByte(uint8_t* offset)
{
    int value = data[*offset];
    (*offset)++;
    return value;
}
</code></pre>

<p>This increases the offset after reading the value.</p>

<p>Some remarks:</p>

<ul>
<li>It's a convention not to use capitals for local variables (so change Heat to heat)</li>
<li>You don't use the Heat variable in <code>sprintf</code>, maybe this is intentional.</li>
</ul>
