c# – Sparkplug B and metrics specification in MQTT
I finally found the solution!
If you send a NCMD message to the topic spBv1.0/ClientEU1/NCMD/27352352 with a SparkPlug B payload (not json payload, it should be SparkPlug B) as the following:
using System;
using System.Text;
using System.Threading.Tasks;
using MQTTnet;
using MQTTnet.Client;
using Google.Protobuf;
using Org.Eclipse.Tahu.Protobuf;
using Newtonsoft.Json;
......
var payload = new Payload
{
Timestamp = (ulong)DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
Seq = 0
};
payload.Metrics.Add(new Payload.Types.Metric
{
Name = "Node Control/Rebirth",
Alias = 1,
Datatype = 11,
BooleanValue = true
});
byte[] payloadBytes;
using (var stream = new System.IO.MemoryStream())
{
payload.WriteTo(stream);
payloadBytes = stream.ToArray();
}
var message = new MqttApplicationMessageBuilder()
.WithTopic("spBv1.0/CustomerA/NCMD/27352352")
.WithPayload(payloadBytes)
.WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce)
.WithRetainFlag(false)
.Build();
await mqttClient.PublishAsync(message);
then, you will get a response at spBv1.0/ClientEU1/DBIRTH/27352352/HART0 with full details of all your metrics like the following:
You need to listen to spBv1.0/ClientEU1/DBIRTH/27352352/HART0. I used MQTT Explorer while running the code, so it got the response from the broker:
{
"timestamp" : 1722607594070,
"metrics" : [
{
"name" : "Tag",
"alias" : 135,
"timestamp" : 1722607594070,
"dataType" : "String",
"properties" : {
"Quality" : {
"type" : "Int32",
"value" : 192
}
},
"value" : "SENSOR "
}, {
"name" : "Manufacturer ID",
"alias" : 136,
"timestamp" : 1722607594070,
"dataType" : "UInt8",
"properties" : {
"Quality" : {
"type" : "Int32",
"value" : 192
}
},
"value" : 42
},
{
"name" : "Temperature",
"alias" : 141,
"timestamp" : 1722607594070,
"dataType" : "Float",
"properties" : {
"engUnit" : {
"type" : "String",
"value" : "%"
},
"Quality" : {
"type" : "Int32",
"value" : 192
}
},
"value" : 86.20619
},
{
"name" : "Flow Value",
"alias" : 143,
"timestamp" : 1722607594070,
"dataType" : "Float",
"properties" : {
"engUnit" : {
"type" : "String",
"value" : "m"
},
"Quality" : {
"type" : "Int32",
"value" : 192
}
},
"value" : 12.930929
},
{
"name" : "Pressure Variable",
"alias" : 145,
"timestamp" : 1722607594070,
"dataType" : "Float",
"properties" : {
"engUnit" : {
"type" : "String",
"value" : "dB"
},
"Quality" : {
"type" : "Int32",
"value" : 192
}
},
"value" : 37.0
},
{
"name" : "Electron Variable",
"alias" : 147,
"timestamp" : 1722607594070,
"dataType" : "Float",
"properties" : {
"engUnit" : {
"type" : "String",
"value" : "m"
},
"Quality" : {
"type" : "Int32",
"value" : 192
}
},
"value" : 2.069071
}
],
"seq" : 1
}
Read more here: Source link
