I have successfully set up Node-RED and it is receiving data from multiple PLC systems.
Here is an example of the payload:
{"d":779,"dt":3,"ts":"2018-04-05T19:54:12.930758Z","q":18}
The fields breakdown as follows:
d=779 dt=3 ts=2018-04-05T19:54:12.930758Z q=18
I've been trying to parse the data, particularly focusing on extracting the "d" field. I've experimented with functions using splice and split methods.
Is there a simple solution to this problem? Could the issue lie in the choice of the node process?
This code snippet was my initial attempt at extracting the first field, but it's not working correctly as it doesn't account for padding and needs to start at the colon and end at the comma.
var str = msg.payload;
var th = str.slice(5,-1);
th = th.split(",");
msg.payload[0] = parseFloat(th[0]);
return [ msg.payload[0]];
The MQTT Buffer looks like this:
[123,34,100,34,58,51,48,54,8,44,34,100,116,34,58,2,44,34,ts,34,58,4,8,8-" "8:" .8,,,8].decode('hex')]
The zero at the end seems to be causing problems:
I'm trying to remove it using this function:
var data = msg.payload;
console.log("payload data: length = " + data.length);
data = data.trim();
console.log("trimmed data: length = " + data.length);
msg.payload = JSON.parse(data);
return msg;
However, now I'm encountering a TypeError: data.trim is not a function
The flow diagram is displayed below:
View Image HereThank you, Kevin