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) {
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 returningexpecting 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.