Skip to content

OPC Router Tutorial Calculator

Following the third part of the OPC Router tutorial, I’m presenting how to perform multiple calculations using the “Calculator” object. Remember, we’re configuring and running a project for “Papín”, a fictional potato bread factory.

Why an OPC Router tutorial with a Calculator?

Because “learning by doing” with examples is fun. Also, this particular functionality can bring us an interesting use case: the advantage of changing our collected data by applying arithmetic and logical operations when needed (i.e. engineering unit conversion). See below a list of posts that complete this tutorial:

  1. Getting Started with OPC Router
  2. Connect an OPC DA server to Microsoft SQL Server
  3. Connect an MQTT data source to InfluxDB Cloud
  4. Perform multiple calculations within OPC Router (this post)
  5. Run an OPC Router application with Linux and Docker
  6. Build a process data simulator with Python, CSV and OPC Router
  7. Explore basic RESTful web services within OPC Router
  8. Build a Telegram basic interaction within OPC Router
  9. Build an SMS basic interaction within OPC Router
  10. Build a WhatsApp basic interaction within OPC Router
  11. Build a Teams basic interaction within OPC Router
  12. Explore advanced RESTful web services within OPC Router
  13. Run an OPC Router application with Portainer

Working with OPC Router, MQTT and a Calculator

Knowing your Plugins

  • To begin with, the configuration of OPC Router starts with your licensed plugins. There are several categories such as “Cloud / IoT”, “Topfloor”, “Shopfloor”, “Storage”, “Advanced”, “Messaging” and “Printer”.
opc router tutorial calculator 1
opc router tutorial calculator 2
  • Make sure to notice MQTT.

Knowing your Transfer Objects

  • At this point, we’re going to need four (4) transfer objects to solve our use case:
opc router tutorial calculator 3
  • Make sure to notice Variables, Constant Values, MQTT trigger, and Calculator.

Configuring your Plugins

  • Following identification, OPC Router needs to configure every licensed plugin. Let’s start creating a new plug in instance with MQTT (double click on the object):
opc router tutorial calculator 4
opc router tutorial calculator 5
opc router tutorial calculator 6
opc router tutorial calculator 7
opc router tutorial calculator 8

Creating your Fourth Template

  • In similar fashion to the third part of the tutorial, a template is the correct approach once again. Let’s create a new Calculator template MPow within CALC folder (VSD motor power):
opc router tutorial calculator 9
opc router tutorial calculator 10
  • Watch an empty design panel so you can start building your template:
opc router tutorial calculator 11
  • Also, create the code that identifies the MPow variable:
opc router tutorial calculator 12
  • Additionally, create the global variable that will track the calculation. Make sure to set “Data Type” to “Untyped” given that it will be used for several different operations.
opc router tutorial calculator 13
  • Drag and drop these three blocks as shown here:
opc router tutorial calculator 14
  • Insert the MQTT trigger block:
opc router tutorial calculator 15
opc router tutorial calculator 16
  • Make sure to create template variable “ds_mqttb_connection” for MQTT links:
opc router tutorial calculator 17
  • Edit the MQTT trigger block and make sure it looks like this:
opc router tutorial calculator 18
opc router tutorial calculator 19
  • Insert the Calculator block:
opc router tutorial calculator 20
opc router tutorial calculator 21
  • Double click to start editing the Calculator Transfer Object:
opc router tutorial calculator 22
  • Firstly, we’re going to add two numbers. To do just that, press the “New” button to start inserting placeholders “A” and “B”:
opc router tutorial calculator 23
opc router tutorial calculator 24
opc router tutorial calculator 25
  • Secondly, press the “Further” button to browse all functions available:
  • Select “SUM” and press the “OK” button to continue.
  • Then, make sure the “Formula” field is SUM(A,B):
  • Press the “Test” button to start inspecting the function result:
  • Enter any placeholder values (for instance, 15 and 6) and check the correct result for the sum operation (21). Finally, press the “OK” button twice to save changes and close the window.
  • Confirm the configured Calculator block:
  • Make sure to link blocks as shown here to complete your fourth template:

Working with Instances from Templates

  • With your fourth template, a new instance is required now. Start creating it using the contextual menu:
  • Check the new instance:
  • In the design panel, double click to check value in template variable according to actual MQTT link:

Compile, Save and Publish your OPC Router Project Configuration with MQTT and a Calculator

  • At this time, you need to compile, save and publish the connection you just created by clicking the “Go productive” button:
  • Meanwhile, the project is saved and published.
  • Press the “Yes” button to start the OPC Router service and run your application.

Publish and Subscribe to MQTT Data

From a Raspberry Pi, publish and subscribe to some MQTT data to test our first calculation:

$ mosquitto_pub -h 192.168.1.188 -t "vsd_motor_p" -m 15 -d
  • Notice the expected result (15 + 6 = 21).

Exploring More Functions

Are you ready to explore and try more functions? Well, I am 🙂

  • Firstly, stop the OPC Router service:
  • Go back to the “MPow” template and double click to the Calculator block to start editing it:
  • At this point, you can do one of two things: press the “Further” button to explore listed functions or type a valid formula yourself in the “Formula” field (this could include the use of template variables).

Multiplication

  • Type A*B in the “Formula” field and press the “Test” button to start checking:
  • Enter any placeholder values (for instance, 6 and 15) and check the correct result for the multiplication operation (90). Finally, press the “OK” button twice to save changes and close the window.
  • Repeat the section “Compile, Save and Publish your OPC Router Project Configuration with MQTT and a Calculator” to apply changes.
  • Publish and subscribe to the same MQTT data to test our second calculation:
$ mosquitto_pub -h 192.168.1.188 -t "vsd_motor_p" -m 15 -d
  • Notice the expected result (6 * 15 = 90).

Division

  • Type A/B in the “Formula” field and press the “Test” button to start checking:
  • Enter any placeholder values (for instance, 6 and 15) and check the correct result for the division operation (0.4). Finally, press the “OK” button twice to save changes and close the window.
  • Repeat the section “Compile, Save and Publish your OPC Router Project Configuration with MQTT and a Calculator” to apply changes.
  • Publish and subscribe to the same MQTT data to test our third calculation:
$ mosquitto_pub -h 192.168.1.188 -t "vsd_motor_p" -m 15 -d
  • Notice the expected result (6 / 15 = 0.4).

Average

  • Press the “Further” button, select AVERAGE and press the “OK” button:
  • Make sure to type AVERAGE(A,B) in the “Formula” field and press the “Test” button to start checking:
  • Enter any placeholder values (for instance, 6 and 15) and check the correct result for the average operation (10.5). Finally, press the “OK” button twice to save changes and close the window.
  • Compile, save, publish and subscribe to the same MQTT data.
  • Notice the expected result ((6 + 15) / 2 = 10.5).

Concatenate

  • Press the “Further” button, select CONCATENATE and press the “OK” button:
  • Make sure to type CONCATENATE(A,B) in the “Formula” field and press the “Test” button to start checking:
  • Enter any placeholder values (for instance, 6 and 15) and check the correct result for the concatenation operation (615). Finally, press the “OK” button twice to save changes and close the window.
  • Compile, save, publish and subscribe to the same MQTT data.
  • Notice the expected result (615).

Conditional

  • Press the “Further” button, select IF and press the “OK” button:
  • Acknowlege that you need three parts: a logical test (i.e. A greater than B or A > B), a value if the logical test is True (i.e. 100) and a value if the logical test is False (i.e. -100).
  • Make sure to type IF(A>B,100,-100) in the “Formula” field and press the “Test” button to start checking:
  • Enter any placeholder values (for instance, 6 and 15) and check the correct result for the conditional operation (-100 or logical test is False because A is not greater than B). Finally, press the “OK” button twice to save changes and close the window.
  • Compile, save, publish and subscribe to the same MQTT data.
  • Notice the expected result (-100).

Did you enjoy this OPC Router tutorial with a Calculator? I’d love to hear from you.

Stay tuned and follow part 5 of this tutorial! 😎

For other articles like this one in English, click here. To see them in Spanish, click here.

Share this post