Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

MQTTspB exercices and configuration

Tutorials → Tutorial | MQTT | How-to Guide | MQTT Tools Reference

This Tutorial Teaches you to:

  • Start the MQTT internal broker
  • Start the MQTT SpB Simulator
  • Set up publishers using Device channels
  • Set up collectors using  TagProvider Services

Prerequisites



Step 1: Set Up the MQTT Broker and Simulator

Start the Built-in Broker

  1. Navigate to Data Explorer → MQTT Tools
  2. Click Run Local Built-in Broker to start the broker
  3. Connect to the broker (connection will show as established)

Run the MQTT Publisher Simulator

  1. Start the MQTT Publisher Simulator
  2. Verify data is publishing - you'll see:
    • Cities (e.g., Barcelona)
    • Solar panels with properties:
      • Name
      • Power
      • Temperature
      • Real-time changing values

Step 2: Map and Publish Data Using Devices

Create Device Mapping

  1. Select a panel (e.g., Panel 1) from the simulator
  2. Click Map to the Solution
  3. Choose Publisher and New DeviceNode
  4. Confirm to create tags automatically

Configure Channel Settings

  1. Go to Devices → Channels
  2. Confirm if all the setting are as expected, including the Group ID from the broker.

Configure Node Settings

  1. Go to Devices → Nodes
  2. Confirm if all the setting are as expected, including the Node ID from the broker.

Configure Points

  1. Check Points section for mapped tags (e.g., PAN01)
  2. Verify the address structure:
    • Group ID: cities
    • Node ID: Barcelona
    • Device ID: Panel_1


Info
titleMQTT Spb Tag Structure

If you are creating tags manually, instead of mapping directly from the Data Explorer, It is important to note that when using MQTT SpB, all the tag must be in a structure. It means that you need to create a user type and use the root level of the user type in the communication.

Step 3: Collect Data Using Tag Provider

Create Tag Provider

  1. Navigate to Frameworks → Tag Provider Services
  2. Click Create New Tag Provider
  3. Select MQTT SparkPlug B Collector Connector
  4. Change the setting to connect to your broker
  5. Test the broker connection

Link to Asset Tree

  1. In Asset Tree, create a folder (e.g., "Barcelona")
  2. Link the tag provider to this folder
  3. Navigate the tree structure to see values

Access Asset Data

  1. Open Tools → Object Browser
  2. Navigate to your asset (e.g., Barcelona → Panel1 → Current)
  3. Copy the asset path using the top button
  4. Paste this path anywhere in the software to retrieve values

Step 4: Configure Runtime Publishing

Enable Local Tag Publishing

  1. In Runtime → Startup, before running the solution, check "Publish to MQTT Broker" option (Advanced settings)
  2. This automatically publishes public local tags to the broker

Create Local Tags (Optional)

  1. Go to Unified Namespace > Tags
  2. Create a new local tag (e.g., "LocalTagPower")
  3. Set visibility column to Public for read/write access
    • Public: Read and write access
    • Protected: Read-only access
    • Private: Tag not visible

Step 5: Run and Monitor the Solution

Start Runtime

  1. In Runtime → Startup, Click Run Startup
  2. Verify all connections are active

Monitor Data Flow

  1. Use Diagnostic Tools → Property Watch to monitor tag values
  2. Use Object Browser to:
    • Navigate to specific tags (e.g., Barcelona → Panel1 → Current)
    • Copy tag paths
    • Add tags to watch lists for testing

Verify Data Collection

  1. Check that asset values are updating in real-time
  2. Confirm data is flowing from:
    • Simulator → Broker
    • Broker → Device mappings
    • Tag Provider → Asset tree

Edge AI with ML.NET (Tutorial) demonstrates using ML.NET 4.0 for real-time anomaly detection on sensor data using Script Tasks.

Prerequisites:

  • Complete Scripts & Data Enrichment (Tutorial)
  • Tags for sensor monitoring
  • ML.NET 4.0 references (included in FrameworX)

In this page:

Table of Contents
maxLevel2
minLevel2
indent10px
excludeSteps
stylenone

Scripts → Tutorial | Concept | How-to Guide | Reference

Step 1: Create Monitoring Tags

  1. Navigate to Unified Namespace → Tags
  2. Create tags:
    • Tag.SensorValue (Double) - Current reading
    • Tag.AnomalyScore (Double) - Detection score
    • Tag.IsAnomaly (Boolean) - Alert flag
    • Tag.Threshold (Double) - Detection threshold (default: 0.3)

Step 2: Create ML Detection Task

  1. Go to Scripts → Tasks
  2. Create task: AnomalyDetector
  3. Set trigger: Period = 1000ms
  4. Add code:

csharp

// Simple spike detection using ML.NET
using Microsoft.ML;
using Microsoft.ML.Data;

// Static ML context (initialized once)
if (@Tag.MLContext == null)
{
    @Tag.MLContext = new MLContext(seed: 0);
    @Tag.DetectionEngine = InitializeDetector();
}

// Data class for ML model
public class SensorData
{
    public float Value { get; set; }
}

public class AnomalyPrediction
{
    [VectorType(3)]
    public double[] Prediction { get; set; }
}

// Initialize detector (runs once)
private ITransformer InitializeDetector()
{
    var dataView = @Tag.MLContext.Data.LoadFromEnumerable(new List<SensorData>());
    
    var pipeline = @Tag.MLContext.Transforms
        .DetectSpikeBySsa(
            outputColumnName: "Prediction",
            inputColumnName: "Value",
            confidence: 95,
            pvalueHistoryLength: 30,
            trainingWindowSize: 90,
            seasonalityWindowSize: 30);
    
    return pipeline.Fit(dataView);
}

// Detection logic (runs every second)
var currentValue = (float)@Tag.SensorValue;

var data = new SensorData { Value = currentValue };
var prediction = @Tag.DetectionEngine.Transform(
    @Tag.MLContext.Data.LoadFromEnumerable(new[] { data }));

var result = @Tag.MLContext.Data
    .CreateEnumerable<AnomalyPrediction>(prediction, false)
    .First();

// Update tags with results
@Tag.AnomalyScore = result.Prediction[0];  // Spike score
@Tag.IsAnomaly = result.Prediction[0] > @Tag.Threshold;

// Log anomalies
if (@Tag.IsAnomaly)
{
    @Alarm.GlobalSettings.AuditTrail.AddCustomMessage(
        $"Anomaly detected: Sensor={currentValue:F2}, Score={result.Prediction[0]:F3}");
}

Step 3: Create Simple Simulator

  1. Create task: SensorSimulator
  2. Set trigger: Period = 500ms
  3. Add simulation code:

csharp

// Simulate normal sensor data with occasional spikes
Random rand = new Random();
double baseValue = 50.0;
double noise = rand.NextDouble() * 5 - 2.5;

// Inject anomaly occasionally (5% chance)
if (rand.NextDouble() < 0.05)
{
    @Tag.SensorValue = baseValue + (rand.NextDouble() * 30 + 20);  // Spike
}
else
{
    @Tag.SensorValue = baseValue + noise;  // Normal variation
}

Step 4: Create Monitoring Display

  1. Create display with:
    • Trend chart for SensorValue
    • Indicator for IsAnomaly
    • Text display for AnomalyScore
    • Threshold adjustment slider

Step 5: Test ML Detection

  1. Start runtime
  2. Observe sensor simulation
  3. Watch for anomaly detection
  4. Adjust threshold as needed
  5. Check audit trail for logged anomalies

Next Steps

  • Advanced MCP Tools → Complex multi-tool scenarios
  • ML.NET Models → Regression and classification
  • Edge Computing → Deploy to field devices
These tutorials provide simple, practical starting points for both MCP Tools and ML.NET integration, focusing on real industrial scenarios while keeping complexity minimal for learning purposes.

In this section...

Page Tree
root@parent
spaces93DRAF