Converting a byte to an int

Evening all,
I’m currently working on a MQTT project using the Huzzah ESP2886.

I can subscribe and publish to feeds using PubSubClient, but am having a little trouble with a byte.

My code grabs any changes to a feed as a byte*. Now I can print that byte to the serial one character at a time - fine. But I want to use the byte to effect changes in my project. For example if my byte has the value ‘50’ I want to convert the byte to a integer so that I can switch something on at a specific threshold.

I’ve googled around and am having trouble in finding a solution that works without an error message.

Here’s my current code that prints out each character to the serial.

Thanks for your help!

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]);
  }
}

llewmihs:
Evening all,
I’m currently working on a MQTT project using the Huzzah ESP2886.

I can subscribe and publish to feeds using PubSubClient, but am having a little trouble with a byte.

My code grabs any changes to a feed as a byte*. Now I can print that byte to the serial one character at a time - fine. But I want to use the byte to effect changes in my project. For example if my byte has the value ‘50’ I want to convert the byte to a integer so that I can switch something on at a specific threshold.

I’ve googled around and am having trouble in finding a solution that works without an error message.

Here’s my current code that prints out each character to the serial.

Thanks for your help!

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]);
  }
}

the function is bringing in a pointer to a byte:

byte* payload

one must de-reference the pointer to get the pointee:

byte myByte = *payload;

reads myByte = the value pointed to by payload

Thanks,

That's very useful - a second question. The data stored in the byte payload may be a string of text, e.g. 'hello'.

When I print the byte, instead of 'hello' i get : 104.

My code is now looking like this:

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

    byte myByte = *payload;
    Serial.println(myByte);
}

So I'm still unsure of how to then say compare it to a char in an if statement.

I want to do something like:

int choice = 50;

if (payload == text) { //do something } else{ //do something different }

When I print the byte, instead of 'hello' i get : 104.

'h' == 0x68 == 104

The data stored in the byte payload may be a string of text, e.g. 'hello'.

If you can fit "hello" into a single byte, you've managed something truly remarkable.

The function is returning a pointer to an array of bytes, rather than chars, because the payload is not limited to text. If you KNOW that the payload is text, add a NULL to the array pointed to, at the position defined by length, and then treat payload as though it was a NULL terminated character array (aka a string). You might need to lie to other functions, by using a cast.

PaulS: The function is returning expecting a pointer to an array of bytes, rather than chars, because the payload is not limited to text. If you KNOW that the payload is text, add a NULL to the array pointed to, at the position defined by length, and then treat payload as though it was a NULL terminated character array (aka a string). You might need to lie to other functions, by using a cast.

instead of:

if (payload == text) {

you have to write:

if (strcmp(payload, text) == 0) {

/Joe

BulldogLowell:

Actually, neither one of us is right. The function is called with...

you have to write:

Actually, that won't compile, since strcmp expects pointers to chars.

if(strcmp((char *)payload, "whatYouWantToCompareTo") == 0)