Connectivity Software User's Guide and Reference
Getting Started with EasySparkplug under new .NET using Visual Studio Code
Rapid Toolkit for Sparkplug > Getting Started > Getting Started with Rapid Toolkit for Sparkplug under .NET Framework or .NET > Getting Started with EasySparkplug under new .NET using Visual Studio Code
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#. 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 Visual Code.

  2. In the command palette box (View -> Command Palette), select the .NET: New Project... command.

  3. In the "Search for templates" edit box under "Create a new .NET project", select the "Console App" project type.

  4. In the Project Location dialog box, navigate to the location for your projects and then press the Select Folder button.

  5. In the edit box under "Name the new project", type HelloEdgeNode, and press Enter

  6. Finalize the project creation sequence. You can keep all defaults.

  7. Open the Terminal window (View -> Terminal).
  8. In the Terminal window, type the following:

    cd HelloEdgeNode
    dotnet add package OpcLabs.EasySparkplug .

    This command adds Rapid Toolkit for Sparkplug package reference to the project file.

    The above command actually adds a reference to the package corresponding always to the very latest Rapid Toolkit for Sparkplug version. If you want to be sure that you referencing a package for Rapid Toolkit for Sparkplug 2025.2, use dotnet add package OpcLabs.EasySparkplug --version 5.83 command instead.
  9. In the Explorer window, select the Program.cs file.

  10. In the text editor window, add following code to the beginning of the Program.cs file:

    using OpcLabs.EasySparkplug;
    
  11. In Program.cs, 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();
    
  12. Select Debug -> Start Debugging (F5) from the menu, or press the corresponding button on the toolbar.

    This will build and launch the program. The server is now running. 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.

  13. 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#. 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 Visual Code.

  2. In the command palette box (View -> Command Palette), select the .NET: New Project... command.

  3. In the "Search for templates" edit box under "Create a new .NET project", select the "Console App" project type.

  4. In the Project Location dialog box, navigate to the location for your projects and then press the Select Folder button.

  5. In the edit box under "Name the new project", type HelloApplication, and press Enter

  6. Finalize the project creation sequence. You can keep all defaults.

  7. Open the Terminal window (View -> Terminal).
  8. In the Terminal window, type the following:

    cd HelloApplication
    dotnet add package OpcLabs.EasySparkplug .

    This command adds Rapid Toolkit for Sparkplug package reference to the project file.

    The above command actually adds a reference to the package corresponding always to the very latest Rapid Toolkit for Sparkplug version. If you want to be sure that you referencing a package for Rapid Toolkit for Sparkplug 2025.2, use dotnet add package OpcLabs.EasySparkplug --version 5.83 command instead.
  9. In the Explorer window, select the Program.cs file.

  10. In the text editor window, add following code to the beginning of the Program.cs file:

    using OpcLabs.EasySparkplug;
    
  11. In Program.cs, 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(); 
    
  12. 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.

  13. 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