Help converting payload w/ data buffer to payload w/ simple string?

7,377 views
Skip to first unread message

Pedro Mendes

unread,
Oct 2, 2015, 7:39:58 AM10/2/15
to Node-RED
Hi Everyone,

This must be a simple thing but i can't seem to do it properly...

PROBLEM

- I have the following mqtt message coming from an arduino

10/2/2015, 12:21:57 PMLight Sensor
home/sensors/light/last : msg : object
{ "topic": "home/sensors/light/last", "payload": { "type": "Buffer", "data": [ 49, 53, 50, 0 ] }, "qos": 0, "retain": false, "_topic": "home/sensors/light/last", "_msgid": "c228dec1.3dd72" }

- With payload

"payload": { "type": "Buffer", "data": [ 49, 53, 50, 0 ] }

- I want to convert the payload to a simple string

"payload": "3333"



I have tried converting it using a function, templates, etc, etc but with no success. The string seems to be converted without issues when doing node.log(msg.payload) for example.
Also when concatenating the payload to a string containing an SQL query (inside a function block), the buffer payload doesn't work and the string payload works perfect, although the logged result seems fine once more.


Please if you know how to do this, let me know, your help would be much appreciated! 

Nicholas O'Leary

unread,
Oct 2, 2015, 11:41:17 AM10/2/15
to Node-RED Mailing LIst
Pedro,

do you expect that exact buffer (49,53,50,0) to be a representation of the string "3333" ? Or have you picked that string at random as an example?

My guess is the buffer represents ASCII, in which case, 49,53,50,0 would map to a null-terminated "15;" - is that what you expect?

It may be helpful to see the arduino code to understand how you are encoding the string.

Nick

--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dave C-J

unread,
Oct 2, 2015, 12:11:35 PM10/2/15
to node...@googlegroups.com
If you really are getting a  msg.payload of { "type": "Buffer", "data": [ 49, 53, 50, 0 ] }

then a simple function
    msg.payload = msg.payload.data.toString();
    return msg;
should attempt to coerce the data (Buffer) to a String and return only that.

Pedro Mendes

unread,
Oct 2, 2015, 11:50:32 PM10/2/15
to Node-RED
Thanks for your quick replies! 

@Nick the value 15 is actually correct, the 3333 was just an example.

And here's my arduino code for the MQTT sendMessage function:

int sendMessage(const char* _topic, const char* _payload) {
      MQTT
::Message message;


      message
.qos = MQTT::QOS0;
      message
.retained = false;
      message
.dup = false;
      message
.payload = (void*)_payload;
      message
.payloadlen = strlen(_payload)+1;
     
int rc = client.publish(_topic, message);


       
return rc;
}

Maybe this could be somehow optimised so the string arrives clearly (i.e. no Buffer)? 

@Dave i'll give that a try as soon as I have access to the machines again. I did try 

msg.payload = msg.payload.toString();
    return msg;

And it prints out fine but gives out errors when concatenating the SQL query.

Nicholas O'Leary

unread,
Oct 3, 2015, 2:18:55 AM10/3/15
to Node-RED Mailing LIst
Pedro, it is arriving as a Buffer because you have the '0' on the end. If you publish the string without the null character, the mqtt node will parse it as a utf-8 string as you want.

If you remove the +1 where you set message.payloadlen then it should just work.

Nick

Pedro Mendes

unread,
Oct 3, 2015, 9:57:23 AM10/3/15
to Node-RED
Thanks Nick! That did it ;)

Javier González Ferreyra

unread,
Jun 28, 2016, 2:50:07 PM6/28/16
to Node-RED
Yes, it worked. removing the \0 from the string .
Thanks for the info, spent several hours looking for the solution...  THANKS

ghe...@me.com

unread,
Jun 28, 2016, 6:20:32 PM6/28/16
to Node-RED
My suggestion as relative nooby is to use the great contrib jason node.
It is very easy to use and will extract efficiently elements from the jason data
In the node you select which system you use from jasonselect jason path, etc
then enter a simple search string. I use the jason path item. You can pre test your search by going to
http://jsonpath.com and you will be able to easily work out what to do
Reply all
Reply to author
Forward
0 new messages