Short recipes for connecting an MTConnect.NET agent or client to third-party systems.
Every MQTT recipe uses the MQTT relay module (mqtt-relay); see that page for the full key reference and Configure the MQTT Relay for a step-by-step walkthrough.
InfluxDB
Uses MTConnectHttpClient to read from an MTConnect agent and write each observation to an InfluxDB bucket.
The example covers plain value observations only; extend it for Conditions, DataSets, Tables, TimeSeries and Assets.
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Writes;
using MTConnect.Clients;
using MTConnect.Observations;
using MTConnect.Streams;
var influxServer = "https://eastus-1.azure.cloud2.influxdata.com";
var influxToken = "<INFLUXDB_API_TOKEN>";
var influxOrganization = "example";
// Set up the InfluxDB client
var influxClient = new InfluxDBClient(influxServer, influxToken);
// Set up the MTConnect client (over HTTP)
var mtconnectClient = new MTConnectHttpClient("http://localhost:5000");
mtconnectClient.CurrentReceived += ObservationsReceived;
mtconnectClient.SampleReceived += ObservationsReceived;
mtconnectClient.Start();
Console.ReadLine();
mtconnectClient.Stop();
void ObservationsReceived(object sender, IStreamsResponseDocument response)
{
using (var write = influxClient.GetWriteApi())
{
var records = new List<PointData>();
foreach (var observation in response.GetObservations())
{
var key = $"{observation.DeviceUuid}.{observation.DataItemId}";
records.Add(PointData.Measurement(key)
.Field("value", observation.GetValue(ValueKeys.Result))
.Timestamp(observation.Timestamp, WritePrecision.Us));
}
// Write the records to the "MTConnect.Streams" bucket
write.WritePoints(records, "MTConnect.Streams", influxOrganization);
}
}
MQTT to AWS IoT
Publishes the agent's documents directly to AWS IoT Core.
- Getting started with AWS IoT Core — AWS documentation for setting up the service and creating a Thing.
- Video walkthrough — step by step (little or no audio).
- MQTT Explorer — a desktop client for browsing the broker's topics.
Notes:
- AWS IoT requires mutual TLS.
Create the Thing's certificate in the AWS IoT console and download the device certificate, the private key and the Amazon root CA (
AmazonRootCA1.pem) to the machine running the agent. The example below expects them in acertsdirectory next to the agent. - AWS IoT allows at most 7 forward slashes in a topic name.
With the default
topicStructure: Document, topics are<topicPrefix>/Current/<deviceUuid>(andProbe,Sample,Asset/<deviceUuid>/<assetId>), so the limit is only reached with a longtopicPrefix.topicStructure: Entityadds more levels (<topicPrefix>/Devices/<deviceUuid>/Observations/<dataItemId>), so keep the prefix to one or two levels when using it.
modules:
- mqtt-relay:
server: a1b2c3d4e5f6g7-ats.iot.us-east-1.amazonaws.com
port: 8883
clientId: mtconnect-agent-01
tls:
pem:
certificateAuthority: certs/AmazonRootCA1.pem
certificatePath: certs/agent-certificate.pem.crt
privateKeyPath: certs/agent-private.pem.key
AWS Greengrass Moquette Broker
Publishes to the MQTT 3.1.1 broker (Moquette) running on an AWS IoT Greengrass core device.
- Getting started with AWS IoT Greengrass V2 — install the Greengrass core.
- MQTT 3.1.1 broker (Moquette) — the broker component.
In the deployment configuration for the aws.greengrass.clientdevices.mqtt.Moquette component, enable the TLS port:
"moquette": {
"ssl_port": "8883"
}
Then point the relay at the core device.
Set clientId to the name of the AWS Thing associated with the core device, and use that Thing's certificates:
modules:
- mqtt-relay:
server: localhost
port: 8883
clientId: mtconnect-agent-01
tls:
pem:
certificateAuthority: certs/AmazonRootCA1.pem
certificatePath: certs/agent-certificate.pem.crt
privateKeyPath: certs/agent-private.pem.key
AWS Greengrass MQTT Bridge
Forwards the relay's messages from the local Moquette broker on the Greengrass core to AWS IoT Core.
- MQTT bridge — the bridge component.
Set up the agent against the local broker first (AWS Greengrass Moquette broker) and the AWS IoT side as in MQTT to AWS IoT.
Then, in the deployment configuration for the aws.greengrass.clientdevices.mqtt.Bridge component, map the relay's topics (the default topicPrefix is MTConnect) to IoT Core:
"mqttTopicMapping": {
"MTConnectIotCoreMapping": {
"topic": "MTConnect/#",
"targetTopicPrefix": "mtconnect-agent-01/",
"source": "LocalMqtt",
"target": "IotCore"
}
}
MQTT to HiveMQ
Publishes to a HiveMQ broker (for example HiveMQ Cloud) with username and password over TLS.
- HiveMQ getting started
- MQTT Explorer — a desktop client for browsing the broker's topics.
modules:
- mqtt-relay:
server: a1b2c3d4e5f6.s1.eu.hivemq.cloud
port: 8883
username: exampleuser
password: examplepassword
useTls: true
Create a Self-Signed Certificate
For development, OpenSSL can generate a self-signed certificate and key for the agent's HTTPS or MQTTS endpoints:
openssl req -x509 -newkey rsa:2048 -keyout mtconnect.key -out mtconnect.crt -days 365 \
-subj "/CN=localhost" -addext "subjectAltName=DNS:localhost,IP:127.0.0.1"
Add -nodes to leave the private key unencrypted; otherwise set the passphrase in the pem block's privateKeyPassword.
Reference the files from a tls block:
tls:
pem:
certificatePath: mtconnect.crt
privateKeyPath: mtconnect.key
Clients must trust the certificate (or skip validation) for the handshake to succeed; see MQTT TLS Handshake Failures.