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:
- Getting Started with OPC Router
- Connect an OPC DA server to Microsoft SQL Server
- Connect an MQTT data source to InfluxDB Cloud
- Perform multiple calculations within OPC Router (this post)
- Run an OPC Router application with Linux and Docker
- Build a process data simulator with Python, CSV and OPC Router
- Explore basic RESTful web services within OPC Router
- Build a Telegram basic interaction within OPC Router
- Build an SMS basic interaction within OPC Router
- Build a WhatsApp basic interaction within OPC Router
- Build a Teams basic interaction within OPC Router
- Explore advanced RESTful web services within OPC Router
- 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”.
- 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:
- 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):
- At this point, define a remote MQTT broker using a Raspberry Pi and press the “Check connection” button to test connectivity. Finally, press the “OK” button twice to save changes and close the window.
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):
- Watch an empty design panel so you can start building your template:
- Also, create the code that identifies the MPow variable:
- 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.
- Drag and drop these three blocks as shown here:
- Insert the MQTT trigger block:
- Make sure to create template variable “ds_mqttb_connection” for MQTT links:
- Edit the MQTT trigger block and make sure it looks like this:
- Insert the Calculator block:
- Double click to start editing the Calculator Transfer Object:
- Firstly, we’re going to add two numbers. To do just that, press the “New” button to start inserting placeholders “A” and “B”:
- 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.