Correct way of Handling MQTT payload / byte*

From every thing that I have read Strings is very bad so I am doing my best to get away from them :slight_smile:

So what is the Best/Correct way to handle byte* info?
Scenario: When I receive a payload from an MQTT topic I would like to compare the payload ( The payload must be a 1 or 5 ) and then according to that payload the do the next function.
by creating String from the payload I have managed to get it going but then I came across atoi - will using this function be the correct way?

Here is my working code so far :

/*
 Basic MQTT example

 This sketch demonstrates the basic capabilities of the library.
 It connects to an MQTT server then:
  - publishes "hello world" to the topic "outTopic"
  - subscribes to the topic "inTopic", printing out any messages
    it receives. NB - it assumes the received payloads are strings not binary

 It will reconnect to the server if the connection is lost using a blocking
 reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
 achieve the same result without blocking the main loop.
 
*/

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 6, 99);
IPAddress server(192, 168, 6, 30);

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  byte value = atoi((char*)payload);
  Serial.print(F("atoi:"));
  Serial.println(value);
if (value >0){                   // if this is 0 then there was TEXT in the payload before the number
   if (value == 5){
  Serial.println("Payload 5 Recieved");
 }else if (value == 1){
  Serial.println("Payload 1 Recieved");
 }
  
}


  
}

EthernetClient ethClient;
PubSubClient client(ethClient);

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("arduinoClient")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outTopic","hello world test");
      // ... and resubscribe
      client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup()
{
  Serial.begin(57600);

  client.setServer(server, 1883);
  client.setCallback(callback);

  Ethernet.begin(mac, ip);
  // Allow the hardware to sort itself out
  delay(1500);
}

void loop()
{
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}

memcmp() would let you compare a memory block with another one

If you know what’s in the buffer is actually a cString (so null terminated) you can use cstrings functions and other cstdlib functions such as atoi()

by creating String from the payload I have managed to get it going but then I came across atoi - will using this function be the correct way?

It's probably not wrong. If you know that the payload is a single digit number, you may choose the shorter version:

  uint8_t value = payload[0] - '0';