Connectivity Software User's Guide and Reference
Getting Started with EasySparkplug under .NET Framework or .NET using Visual Studio
Rapid Toolkit for Sparkplug > Getting Started > Getting Started with Rapid Toolkit for Sparkplug under .NET Framework or .NET > Getting Started with EasySparkplug under .NET Framework or .NET using Visual Studio
In This Topic

In this Getting Started, you can create

Prerequisites

If you want to verify the version of .NET on your computer, use the dotnet --info command in the command prompt, and look for the Version field under ".NET runtimes installed:" in the generated output. Do not use dotnet --version, because this command only returns the version of the .NET command-line tools.

Sparkplug Edge Node in a Console Application

In this Getting Started procedure, we will create a Sparkplug edge node in C# or VB.NET. The edge node will be hosted in a console application running in .NET Framework or .NET 8, 9, and will publish a metric with dynamic random value

  1. Start the Visual Studio IDE (Integrated Development Environment).

  2. In this step, we will create a new .NET console application project.

    Select File -> New -> Project from the menu. The Create a new project dialog appears. In the language selection drop-down (initially set to All languages), select either C# or Visual Basic. Type Console App into the search box. Select either Console App (for .NET 8+) or Console App (.NET Framework).  Press the Next button. Go through the remaining steps of the wizard, making sure that you select a supported framework (see OPC Wizard Required .NET Runtimes).

  3. In this step, we will add a reference to the OpcLabs.EasySparkplug NuGet package.

    Switch to the Solution Explorer window, right-click on the project node (ConsoleAppn), and select Manage NuGet Packages... command. In the NuGet: project window, switch to the Browse tab, and type OpcLabs.EasySparkplug into the search box. Select the OpcLabs.EasySparkplug package in the package list in the left pane of the window. In the right pane of the window, verify or change the package version next to the Version label. The version should be "Latest stable 5.83.build" (where build is a build number). Press the Install button. If the Preview Changes dialog is displayed, press the Apply button. Wait until the packages are installed.

  4. In the Solution Explorer window of Visual Studio, open the Program.cs (or Program.vb, if your are developing in VB.NET) file, and add following code to the beginning of the file:

    using OpcLabs.EasySparkplug;
    
    Imports OpcLabs.EasySparkplug
    
  5. In Program.cs (or Program.vb, if your are developing in VB.NET), if there is a Main method, replace its body by the following code. If there is no Main method, just top-level statements, simply replace them all.

    var edgeNode = new EasySparkplugEdgeNode("mqtt://localhost", "easyGroup", "easySparkplugDemo");
    var random = new Random();
    edgeNode.Add(new SparkplugMetric("Metric1").ReadValueFunction(() => random.Next()));
    
    edgeNode.Start(); 
    Console.ReadLine(); 
    edgeNode.Stop();
    
    Dim EdgeNode As New EasySparkplugEdgeNode("mqtt://localhost", "easyGroup", "easySparkplugDemo")
    Dim Random As New Random()
    EdgeNode.Add(New SparkplugMetric("Metric1").ReadValueFunction(Function() Random.Next()))
    
    EdgeNode.Start()
    Console.ReadLine()
    EdgeNode.Stop()
    
  6. Select Debug -> Start Debugging (F5) from the menu, or press the corresponding button on the toolbar.   

    This will build and launch the program. The edge node is now running, and publishing the Sparkplug data to the MQTT broker running on "mqtt://localhost", port 1883. Sparkplug host applications (consumers) can connect to the MQTT broker and subscribe to the Sparkplug data produced by the edge node. You can verify it using any configurable Sparkplug host application, or use the Getting Started procedure to create a simple Sparkplug host application of your own; see further below for instructions.

    After you are done, press Enter to exit the program.

Edge Node Verification

When the Sparkplug edge node is running and connected to an MQTT broker, any Sparkplug host application (consumer) can connect to the MQTT broker and subscribe to the value of the metric we have defined in the edge node. The Sparkplug host application can connect to the MQTT broker on "mqtt://localhost" to get the data, unless you have changed the source code to use a different URL.

Verification with the SparkplugCmd Utility (recommended)

If you have the SparkplugCmd utility (e.g. if you have installed Rapid Toolkit for Sparkplug using the Connectivity Software Setup program, or obtained the SparkplugCmd separately e.g. through ClickOnce deployment), you can use it for verification of the created Sparkplug edge node as follows: 

  1. Start the SparkplugCmd utility (use the -i option to enter the interactive mode).
  2. At the SparkplugCmd> prompt, enter the following command:
     
            consumer mqtt://localhost subscribePayload easyGroup easySparkplugDemo
    
    
  3. Observe the metric value changes in the payloads displayed in the console output.
  4. The consumer command will terminate automatically after 1 minute, or you can stop it by pressing X on the keyboard.

Verification with Rapid Toolkit for Sparkplug Application Console Demo

The Rapid Toolkit for Sparkplug Application Console Demo is a simple Sparkplug host application which subscribes to edge node and device metrics on the given Sparkplug edge node, and displays the incoming data on the console. Its source code is included with the product in the Examples solution, and you can build it yourself and run. If you are on Windows, the pre-built application is also installed using the Setup program, and can be started from the Launcher program; you will find it under  Rapid Toolkit for Sparkplug -> Demos (.NET) -> Sparkplug Application Console Demo.

The Sparkplug Application Console Demo is pre-configured to connect to the MQTT broker on "mqtt://localhost", and use Sparkplug group easyGroup and edge node easySparkplugDemo. If you need to change any of this, these parameters can be given on the command line; the MQTT broker address is the first parameter.

Verification with the Getting Started Consumer

The Getting Started procedure for creating a Sparkplug consumer (host application) is designed in such a way that it can be used to observe the data produced by the Sparkplug edge node Getting Started program, without any modification. Just remember to run the two programs in parallel.

Verification with Other Sparkplug Host Applications

If you have other Sparkplug host application that you want to use for verification, it should allow you to browse for the available Sparkplug data, or enter the configuration manually. The default MQTT broker address is "mqtt://localhost", the edge node that we have defined is located in the Sparkplug group with ID easyGroup, and the edge node ID is easySparkplugDemo. The metric name is Metric1.

Security Notice

For simplicity in configuration, the Sparkplug component created in this Getting Started uses an insecure connection, and does not use any authentication. A production Sparkplug component and/or its configuration should be secured appropriately. For more information, see Security in Sparkplug.

Sparkplug Consumer in a Console Application

In this Getting Started procedure, we will create a Sparkplug host application (consumer) in C# or VB.NET. The consumer will be hosted in a console application running in .NET Framework or .NET 8, 9, and will display the data published by the edge node

  1. Start the Visual Studio IDE (Integrated Development Environment).

  2. In this step, we will create a new .NET console application project.

    Select File -> New -> Project from the menu. The Create a new project dialog appears. In the language selection drop-down (initially set to All languages), select either C# or Visual Basic. Type Console App into the search box. Select either Console App (for .NET 8+) or Console App (.NET Framework).  Press the Next button. Go through the remaining steps of the wizard, making sure that you select a supported framework (see OPC Wizard Required .NET Runtimes).

  3. In this step, we will add a reference to the OpcLabs.EasySparkplug NuGet package.

    Switch to the Solution Explorer window, right-click on the project node (ConsoleAppn), and select Manage NuGet Packages... command. In the NuGet: project window, switch to the Browse tab, and type OpcLabs.EasySparkplug into the search box. Select the OpcLabs.EasySparkplug package in the package list in the left pane of the window. In the right pane of the window, verify or change the package version next to the Version label. The version should be "Latest stable 5.83.build" (where build is a build number). Press the Install button. If the Preview Changes dialog is displayed, press the Apply button. Wait until the packages are installed.

  4. In the Solution Explorer window of Visual Studio, open the Program.cs (or Program.vb, if your are developing in VB.NET) file, and add following code to the beginning of the file:

    using OpcLabs.EasySparkplug;
    
    Imports OpcLabs.EasySparkplug
    
  5. In Program.cs (or Program.vb, if your are developing in VB.NET), if there is a Main method, replace its body by the following code. If there is no Main method, just top-level statements, simply replace them all.

    var consumer = new EasySparkplugConsumer("mqtt://localhost");
    consumer.SubscribeEdgeNodeMetric("easyGroup", "easySparkplugDemo", "#",    (sender, eventArgs) => Console.WriteLine(eventArgs));
    Console.ReadLine(); 
    
    Dim Consumer As New EasySparkplugConsumer("mqtt://localhost")
    Consumer.SubscribeEdgeNodeMetric("easyGroup", "easySparkplugDemo", "#",    Sub(Sender, EventArgs)Console.WriteLine(eventArgs))
    Console.ReadLine()
    
  6. Select Debug -> Start Debugging (F5) from the menu, or press the corresponding button on the toolbar.   

    This will build and launch the program. The host application is now running, and subscribing to Sparkplug data from the MQTT broker on "mqtt://localhost", port 1883. It will display any metrics published by edge node ID "easySparkplugDemo" in the group "easyGroup". You can verify it by starting a Sparkplug edge node (hardware or software) that will publish the Sparkplug data using these parameters, or use the Getting Started procedure to create a simple Sparkplug edge node of your own; see earlier in this article for instructions on how to create such an edge node.

    After you are done, press Enter to exit the program.

Host Application Verification

When the Sparkplug host application is running and connected to an MQTT broker, it will show metric data published to the same broker by properly configured Sparkplug edge node. The Sparkplug edge node should connect to the MQTT broker on "mqtt://localhost" to publish the data, unless you have changed the source code to use a different URL.

Verification with the SparkplugCmd Utility (recommended)

If you have the SparkplugCmd utility (e.g. if you have installed Rapid Toolkit for Sparkplug using the Connectivity Software Setup program, or obtained the SparkplugCmd separately e.g. through ClickOnce deployment), you can use it for verification of the created Sparkplug consumer (host application) as follows:

  1. Start the SparkplugCmd utility (use the -i option to enter the interactive mode).
  2. At the SparkplugCmd> prompt, enter the following command:
                     
            edgeNode mqtt://localhost easyGroup easySparkplugDemo start
                            
    
  3. Observe the metric data displayed by the Getting Started consumer (host application) program.
  4. Stop the edgeNode command  by pressing X on the keyboard.

Verification with Rapid Toolkit for Sparkplug Edge Node Console Demo

The Rapid Toolkit for Sparkplug Edge Node Console Demo is a simple Sparkplug edge node with simulated metrics and devices. Its source code is incldued with the product in the Examples solution, and you can build it yourself and run. If you are on Windows, the pre-built edge node is also installed using the Setup program, can cen be started from the Launcher program; you will find it under  Rapid Toolkit for Sparkplug -> Demos (.NET) -> Sparkplug Edge Node Console Demo.

The Sparkplug Edge Node Console Demo is pre-configured to connect to the MQTT broker on "mqtt://localhost", and use Sparkplug group easyGroup and edge node easySparkplugDemo. If you need to change any of this, these parameters can be given on the command line; the MQTT broker address is the first parameter.

Verification with the Getting Started Edge Node

The Getting Started procedure for creating a Sparkplug edge node is designed in such a way that it publishes the data that can be observed by the Sparkplug consumer Getting Started program, without any modification. Just remember to run the two programs in parallel.

Verification with Other Sparkplug Edge Node Components

If you have other Sparkplug edge node (software or hardware) that you want to use for verification, it needs to be configured with following Sparkplug identification data: Use MQTT broker address "mqtt://localhost", Sparkplug group with ID easyGroup, and the edge node ID easySparkplugDemo. You can use any metric name(s), because the Getting Started consumer (host application) displays all metrics on the edge node.

Security Notice

For simplicity in configuration, the Sparkplug component created in this Getting Started uses an insecure connection, and does not use any authentication. A production Sparkplug component and/or its configuration should be secured appropriately. For more information, see Security in Sparkplug.

 

Sparkplug is a trademark of Eclipse Foundation, Inc. "MQTT" is a trademark of the OASIS Open standards consortium. Other related terms are trademarks of their respective owners. Any use of these terms on this site is for descriptive purposes only and does not imply any sponsorship, endorsement or affiliation.

See Also

Knowledge Base