I am trying to set up function to send a JSON Obect to MQTT Broker using sparkplug B.
The function is:
var jsonObject = msg.payload;
// Convert JSON object to string
var jsonString = JSON.stringify(jsonObject);
var sparkplugBMessage = {
“metrics”: [
{
“name”:“RECIPE”,
“type”:12,
“value”: jsonString
}
]
};
// Set the stringified JSON as the new payload
msg.payload = sparkplugBMessage; //msg.js=jsonObject //msg.jsstring=jsonString
// Return the modified message
return msg;
There is a little more going on here than what is in your post here.
The last part of the debug is what makes me think that… the error is that you cant convert some types (like ints) to upper case, so hence you are getting the TypeError typeString.toUpperCase is not a function.
The .toUpperCase command is not in your post, so must be elsewhere in your code?
The type error is related to the setting the data type in the node-red-contrib-sparkplug-plus node.
If i use the node with a data type of int or float it the message goes to the broker with no problem. The sparkplub b spec has the data type of 12 (string) and 16 (data set). I have tried both data types with with issues. I have tried to stringify the json and send it as a string.
Essensially the type errror is coming from the contrib-sparkplug-plus node. I not sure why,
I’m wondering if it goes straight from this function node to the sparkplug node, and specifically which spB node are you using, the “device” node or the “out” node?
Also, is there a reason you are defining the type by the “type” property in the metrics? Have you tried removing that line?
Another thing to try would be to use the built-in dynamic metric definitions that you can see half way down on the node package page.
You asked: Also, is there a reason you are defining the type by the “type” property in the metrics? Have you tried removing that line?
Removing the type property in metrics removed the error. I am having a hard time understanding the where and when to use the type property. Is there a “preferred node” to communicate with broker ie should I be using the “out” node instead of the “device” node?
Makes sense now. I misread / misunderstood the specs for the node and how to set the type. Next step is to install the Cirrus Snowflake Connector to have the MQTT data autopopulate into Snowflake and establish two way dynamic commication between our data warehouse and the epic.
Looking further into the datatype, it should only be used when initially creating the tag, updating the value goes to that existing tag so there’s no need to specify it each time.
Also just for future reference for anyone that comes to this thread in the future, there’s a list all the types and their associated integer code in section 14.2 of the Sparkplug Specification Rev 2.2