All posts by vogler

Unity3D in WinCC Open Architecture

This article is about integrating your 3D Unity applications into WinCC Open Architecture SCADA HMI Web screens and exchanging property values. It only works with the WinCC Ultralight Web Client. Because the native Ui does not support WebGL in the WebView Widget. Update: It should also work in the native Ui, you just have to set the environment variable: QTWEBENGINE_CHROMIUM_FLAGS=–enable-gpu

With this free Unity Asset you can create a Unity application with an interface to the SCADA system in an easy way. Don’t be confused about the name of the Asset “WinCC Unified Custom Web Control”. This is because initially it was build to create Custom Web Controls for WinCC Unified only. But there is now also an option to create a build of your Unity application for WinCC Open Architecture.

First create and build your Unity Application as described in the documentation of the Asset. You may also watch this video.

Just at the end execute the menu item to create a WinCC Open Architecture application, instead of WinCC Unified.

Create and load the WebView

Then copy the ZIP file to your WinCC OA project into the folder “data\html” and unzip the ZIP (for example C:\WinCC_OA_Proj\Test\data\html\UnityCustomControl).

In this tutorial our application is named “UnityCustomControl”. You have to replace this with the name of your Unity application.

Then you must insert a WebView into your screen.

And then you must load the generated Unity application in the Initialize script of the widget.

main()
{
   this.loadSnippet("/data/html/UnityCustomControl/index.html");
}

In the Property Editor at the Extended tab be sure to set the “ulcClientSideWidget” to TRUE.

Set and receive property values

To send values from your WinCC Open Architecture to the Unity application you must use execJsFunction of the Webview and call the “setPropertyInUnity” function with the property and the value which you want to set. See the following example.

UnityCustomControl.execJsFunction("setPropertyInUnity", "target_shoulder_link", 10);

“UnityCustomControl” is the name of our Webview Widget! It’s up to you how you name it.

At the WebView there is an event “messageReceived”. There you will get all the messages which are sent from Unity to WinCC Open Architecture. See the example for the structure of the parameter. It is always a JSON document which contains the Name and the Value of the property which has been sent.

Receiving Property Values:
WCCOAui2:["messageReceived"][mapping 3 items
WCCOAui2:   "uuid" : 2
WCCOAui2:   "command" : "msgToCtrl"
WCCOAui2:   "params" : mapping 2 items
WCCOAui2:	   "Name" : "test_property"
WCCOAui2:	   "Value" : "Hello World 1"
WCCOAui2:]

The very first message does not have any “params”, this message comes when the initialization of Unity is done.

First Message:
WCCOAui2:["messageReceived"][mapping 2 items
WCCOAui2:   "uuid" : 1
WCCOAui2:   "command" : "msgToCtrl"
WCCOAui2:]

Start Ultralight Web Client

Start a Control Manager with the “webclient_http.ctl” script.

Then you can open the application in the browser with “https://localhost/data/ulc/start.html”.

Sending OPC UA Data with GraphQL to Unity…

In a simple setup I have tested to send 2000 value changes per second from an OPC UA server to Unity with GraphQL, the Open-Source Frankenstein Automation Gateway, and the GraphQL for Unity Asset. And it could go up to 10000 value changes per second…

I had one DotNet OPC UA server with a lot of simulated tags with random data. The .Net OPC UA server is the DotNet reference implementation from the OPCFoundation, which can be found here.

On top of that the Open-Source Frankenstein Automation Gateway for GraphQL was running. It is connected to the OPC UA server. It offers a GraphQL interface to the tags of one or more connected OPC UA servers.

In Unity I had used the GraphQL for OPC UA Asset to easily connect to the Gateway, browse the tags, and subscribe to the value changes of 100 tags.

Each tag was changed by the OPC UA server every 45ms. This ends up in a bit more than 2000 value changes per seconds, which were sent from the OPC UA server to the Unity Application.

Here we see the Application running 3 times on my Laptop with an Intel(R) Core(TM) i7-9850H CPU @ 2.60GHz. In the center you see the amount of incoming value changes per second (red number). Around that number, we see some of the values coming in from OPC UA. At the bottom we see the CPU load of the Frankenstein Automation Gateway (java program) and the OPC UA DotNet Server. Both were running on an old Intel(R) Core(TM) i3-6100U CPU @ 2.30GHz.

It was also possible to increase to load up to 10000 value changes per seconds! Sending 100 values every 10ms from OPC UA to Unity…

GraphQL for Unity and how to set headers in code…

If you use the GraqhQL for Unity asset and if you want to set the headers on HTTP or Websockets in the code, then you must use the the GraphQLHttp or GraphQLWebsockets class. At the base class “GraphQL” there is no “Headers” property!

public GraphQLHttp MyGraphQL;

void Start()
{        
    if (MyGraphQL != null)
    {
        MyGraphQL.Headers = new List<Header>()
        {
            new Header() { Key = "token", Value = "secret"}
        };
        ...

Then you can also execute a GraphQL query in the code.

MyGraphQL.ExecuteQuery("query { test }", null, (message) =>
{
    if (message.Type == MessageType.GQL_DATA)
    {
        Debug.Log("Data: " + message.Result.ToString());
    }
    else
    {
        Debug.Log("Error: " + message.Type);
    }
});
      
   

GraphQL on Unity and Result Event for Data and Errors…

The GraphQL for Unity Asset can be used to execute GraphQL queries in Unity. The result is set on properties of Unity objects and there is also a Unity Event on the GraphQL Query Object where user-defined function of a GameObject can be triggered every time when a queries returns data or an error.

public class Sample1 : MonoBehaviour
{
    public void ResultEvent(GraphQLResult result)
    {
        if (result.Errors.Count==0)
        {
            Debug.Log("Data: " + result.Data.ToString());
        }
        else
        {
            Debug.Log("Error: " + result.Errors.ToString());
        }
    }
}

Create a GameObject with this class as component and then you can drag and drop this GameObject to the Query GameObject and select the “ResultEvent” method. Every time when the query is executed then this function will be called with the result data (or error data).

GraphQL on Unity & Newtonsoft 12.0.0.0 Reference Error

The GraphQL for Unity Asset uses the Newtonsoft version which is built in newer version of Unity (2020+). If you get an error like this:

Assembly 'Library/ScriptAssemblies/Assembly-CSharp.dll' will not be loaded due to errors: Reference has errors 'GraphQL'.
 
Assembly 'Assets/GraphQL/Libs/GraphQL.dll' will not be loaded due to errors: 

GraphQL references strong named Newtonsoft.Json Assembly references: 12.0.0.0 Found in project: 12.0.1.0.

Assembly Version Validation can be disabled in Player Settings "Assembly Version Validation"
  • Then you can try to remove NewtonSoft dependency from your project – if you have a dependency. And just use the NewtonSoft version which is already built in and shipped with Unity 2020+. If you use an older version of Unity then you can try to copy the NewtonSoft DLL from 2020 and use this in your older Unity version.
  • Or you can go to Project Settings -> Player -> Other Settings and deactivate “Assembly Version Validation”
  • Install the newtonsoft package from Unity. Go into the package manager and click “+” and then select “Add package from git URL” and fill in “com.unity.nuget.newtonsoft-json”.

Dockerfile for Python 3.9 with OpenCV, MediaPipe, TensorFlow Lite and Coral Edge TPU

Dockerfile

FROM python:3.9-slim
RUN apt-get update && apt -y install curl gnupg libgl1-mesa-glx libglib2.0-0 && rm -rf /var/lib/apt/lists/*
RUN echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | tee /etc/apt/sources.list.d/coral-edgetpu.list 
RUN curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - && apt-get update && apt-get install -y python3-tflite-runtime && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY requirements.txt /
RUN pip install -r /requirements.txt
COPY * /app/

Requirements.txt

opencv-python
mediapipe

GraphQL for Unity and execute a GraphQL Query in Unity with C# Code

With GraphQL for Unity you can execute GraphQL Queries in a Unity way with GameObjects. But with the asset you can also execute queries in Unity with C# code.

Here is a simple example:

public GraphQL Connection;

public void ScriptQuery()
    {
        var query = "query($Token: String!) { doit($Token) { result } } ";
        var args = new JObject
        {
            { "Token", "123" }
        };
        Connection.ExecuteQuery(query, args, (result) =>
        {
            Debug.Log(result.Result.ToString());
        });
    }

Link the Connection variable to your GraphQL GameObject where the connection is set.

Note: the result callback function is called asynchronous and it is not executed in the Game-loop.

A bit more complex example how the args variable could look like. This is just an example how you can create a JSON object in C# in a convenient way.

JObject args = new JObject
        {
            ["UpdateList"] = new JArray
            {
                new JObject
                {
                    ["id"] = 28,
                    ["updateAction"] = "EDIT",
                    ["status"] = "Present"
                },
                new JObject
                {
                    ["id"] = 29,
                    ["updateAction"] = "EDIT",
                    ["status"] = "Absent"
                },
                new JObject
                {
                    ["id"] = 30,
                    ["updateAction"] = "EDIT",
                    ["status"] = "Present"
                }
            }
        };

Industrial Data in the Graph Database Neo4j…

The Frankenstein Automation Gateway now also supports to write OPC UA values to the graph database Neo4j.

At startup it can also write the OPC UA node structure into the graph database, so that the basic model of the OPC UA server is mirrored to the graph database. For that you have to add the “Schemas” section in the config file (see an example configuration file below). There you can choose which RootNodes (and all sub nodes) of your OPC UA systems should be mirrored to the graph database.

Once you have the (simplified) OPC UA information model in the graph database, you can add on top of that your own knowledge graph data and create relations to OPC UA nodes of your machines to enrich the semantic data of the OPC UA model.

With that model you can leverage the power of your Knowledge Graphs in combination with live data from your machines and use Cypher queries to get the knowledge out of the graph.

Here we see an example of the OPC UA server from the SCADA System WinCC Open Architecture. The first level of nodes below the “Objects” node represent Datapoint-Types (e.g. PUMP1) followed by the Datapoint-Instances (e.g.: PumpNr) and below that we see the datapoint elements (e.g. value => speed). An datapoint element is an OPC UA variable where we also see the current value from the SCADA system.

Example Gateway configuration file:

Database:
  Logger:
    - Id: neo4j
      Enabled: true
      Type: Neo4j
      Url: bolt://nuc1.rocworks.local:7687
      Username: "neo4j"
      Password: "manager"
      Schemas:
        - System: opc1  # Replicate node structure to the graph database
          RootNodes:
            - "ns=2;s=Demo"  # This node and everything below this node
        - System: winccoa1  # Replicate the nodes starting from "i=85" (Objects) node
      WriteParameters:
        BlockSize: 1000
      Logging:
        - Topic: opc/opc1/path/Objects/Demo/SimulationMass/SimulationMass_Float/+
        - Topic: opc/opc1/path/Objects/Demo/SimulationMass/SimulationMass_Double/+
        - Topic: opc/opc1/path/Objects/Demo/SimulationMass/SimulationMass_Int16/+
        - Topic: opc/winccoa1/path/Objects/PUMP1/#
        - Topic: opc/winccoa1/path/Objects/ExampleDP_Int/#