Accessing Field Data to Telegram Through IIOT and OPC UA
by prasanthk4 in Circuits > Electronics
1095 Views, 2 Favorites, 0 Comments
Accessing Field Data to Telegram Through IIOT and OPC UA

Accessing the Field data to a centralized platform is quite common, But the real problem exists at a point of communication quality instead its value. We have established systems to show and store live tag values but an instant alert for the quality of communication to user is still far away for industries. Look at an example for getting an alert of communication quality to an application like telegram or twitter or such applications that frequently using is sound good for instance response and reduced down time even if the user is not available at the plant. How this is possible? how can a PLC or SCADA have a communication to an android device that are working an extremely different platform? Is industrial controllers and software are able for platform independent communication? The answer is yes, The OPC UA enables this cross platform communication. here we are going to have communication between Modbus enabled PLC to telegram. Instead of physical PLC hardware I am using Modbus simulator that functions as a Modbus server(simulated PLC).
Required Software
1) Modsim
2) Virtual Serial Port
3) KepserverEX V6
4) Node-Red
5) Telegram
Methodology

Here Modsim will be configured to Modbus server which simulate Boolean values at an address of '00001' with Modbus RTU serial communication and is connected to COM1 port of the system. KepServer Will be configured to Modbus Client Which access data through Modbus driver as an OPC DA and is connected to COM2 port of the system. COM1 and COM2 are virtually interconnected by Virtual Serial Port application. If we are using Physical PLC then it is possible to use only one COM port and avoid Virtual Serial Port application to bind two COM ports as because of two application can't able to use same COM port at a time. KEPServer will convert OPC DA into OPC UA and deliver the same to Node-Red, Node-Red will Communicate with telegram by using Telegram Id over Internet.
Installation of Modsim
Download ModSim32.zip software from below given link or directly browse to http://www.win-tech.com/ website and find the above software. It will be a folder and do not need to install.
https://www.win-tech.com/demos/modsim32.zip
Modsim software will act as a simulated PLC with Modbus protocol, It has all Modbus address with simulating functionality over Analog and Boolean data types.
Installing Virtual Serial Port
Download virtual serial port, The download link is provided below
https://www.virtual-serial-port.org/downloads.html...
Install the software same as of normal software installation.
Installing KepserverEX
Go to the Kepserver website and download free demo software. The link is given below.
https://www.kepware.com/en-us/products/kepserverex...
While at installation choose "typical installation", and click next for each popup until finish the installation
Kepserver Demo has only continuous two hour runtime for its drivers, for example Modbus. Runtime will be automatically closed after two hour and the communication will be get discontinued. For testing and development purpose you can restart Kepserver runtime again and again each and every two hours. For an industrial use, purchase kepserver license and avoid time limitations. For runtime service restart, press 'Windows button+R' and type 'services.msc' then 'ok', find kepserver runtime service from the list and restart it.
Installation of UAExpert
Download UAExpert from the given below link and install it on same computer.
https://www.unified-automation.com/downloads/opc-u...
Why we are using UAExpert is of two reasons
1) It will be work as an OPC UA Client , we can check the KEPServer UA output at this software before implementing the project.
2) For Node-Red configuration we should have a node id of tag in the KEPWare that we are going to communicate through OPC UA, but we cannot see it on the KEPWare software, same time UAExpert is providing that parameter(It is not a parameter from UAExpert software, Without UAExpert the communication is possible, this software is being used only to know the Node id of the tag used in kepware.)
ModSim Configuration


Open ModeSim folder and and run ModSim32.exe file and follow below steps.
1) file-new
2) Do below configuration to get a Boolean output at device address of one and Modbus address of one
[Address:0001, Length:10, Device id:1, MODBUS point type: 01 coil status]
3) Connection-connect-port1 (This means ModSim is connected to COM1 of the computer)
4) Do below configuration and press ok to have a Modbus serial RTU communication
[Protocol: RTU, Baud rate:9600, Data bits:8, Stop bits:1, Parity:None]
5) click on 0001 - Simulation - put check mark on 'Enable' and do below configuration on simulation popup then press ok
[Simulation type:toggle, change interval(secs):1]
Now ModSim will start to simulate between '0' and '1' at an address of 0001 with a gap of 1second
Installation of Node-Red


Installation of Node-Red is quite different than other windows based application installations. Here we have to use command prompt and PowerShell to complete the Node-Red installation. We have three steps to complete the Node-Red installation.
1. Install Node.js
Download the latest 14.15.5 LTS version of Node.js (at the time of this publish 14.15.5 is the latest LTS version) from the official Node.js home page. link is given here
Please ensure to download only LTS version instead of current version from website as because both are available at website.
Run the downloaded MSI file. Accept the defaults when installing. After installation completes, close any open command prompts and re-open the same.
Once installed, open command prompt and run the following command to ensure Node.js and npm are installed correctly.
node --version && npm --version
You should have a result similar to above image
2. Install Node-Red
Open command prompt and run following command to Install Node-Red as a global module.
npm install -g --unsafe-perm node-red
it will take a couple of minutes to complete installation through PowerShell (PowerShell will be opened automatically while at installation)
3. Run Node-RED
Open command prompt and run following command to run Node-Red once installation has been completed.
Node-Red
It will load all palette node and finally it will provide a web address, then open Google chrome and type the web address shown at the command prompt
here it is http://127.0.0.1:1880
Node-Red will open on the browser, Now you have to install New palette for Telegram and OPC UA as it was not available default. Do following steps to install telegram and OPC UA nodes.
For installing New Palette go to Menu - Manage Palette - Install
search 'node-red-contrib-telegrambot' and install it for telegram nodes, Search 'node-red-contrib-opcua' and install it for OPC UA nodes.
Virtual Serial Port Configuration

Simulated ModSim will act as a PLC and address0001 will perform as a tag and it is continuously changing its value between zero and one, Now our requirement is to connect this virtual PLC(ModSim) to KEPServer through Modbus serial RTU communication protocol. We have already configured Modbus serial RTU server configuration on ModSim software and connected to COM1 port. At next we will configure KEPServer as a Modbus serial RTU client to communicate Modsim to get value at an address 0001. Our plan is to connect KEPServer to COM2 port, as per our configuration ModSim will send data to COM1, The KEPServer would get data only even if COM1 have a connection to COM2, This will make possible by virtual serial port software. Virtual serial port will bind COM1 and COM2 Internally.
Open Virtual Serial Port software and add pair COM1 and COM2 (COM1 and COM2 are default selected by the software, So we have to just do only click on add pair button)
KEPServer Configuration




OPC Modbus driver Configuration
Here KEPServer will be act as an OPC Modbus Client to get data from ModSim, So we are going to make communication to a PLC(ModSim) having Modbus Protocol. So we should select Modbus serial driver from kepware and have to do necessary configuration for serial communication and Modbus parameters.
Open KEPServerEX6 Right click on the connectivity and add a new channel
Select Modbus SerialRTU driver and click next up to the end (some configuration you have to do in this section that has been given below, make rest of all are default)
[Physical Medium:COM Port, COM ID:2, Baud Rate:9600]
Add device on newly created channel, For this click on the channel, Give a device name and click next up to the end and keep all default.
Add a tag on newly created device, For this click on the device and give name and address of tag. Tag name will be your choice but tag address should be 000001 based on the tag address that have been in ModSim(in Modsim it will be 0001) set data type as boolean.
Click on the channel name - Serial Communications and set below configuration
[Data bits:8, Stop bits:1, Parity:None]
Go to runtime and click connect
click on Launch OPC Quick client from tools
Select your tag from list by Channel name.device name(Channel name and device name are the name you given)
Check the quality and value of tag at Quick client, Quality should be 'Good' and value should be toggling between zero and one.
OPC UA Configuration
Here KEPServer will act as an OPC UA Server to deliver data to Node-Red.
Type 'OPC UA Configuration' on windows search and open OPC UA Configuration app( This is the OPC UA Configuration app of KEPWare)
Double click on the url opc.tcp://127.0.0.1:49320 if it will be available on default otherwise click on add button and do below configuration then press ok
Network Adaptor: Localhost only, Port number: 49320, Security Policies: Basic256sha256 sign and encrypt
check whether enabled check box is marked for particular url, if not do it.
Click on trusted clients tab, default KEPServerEx UA Client is available, We will have to add UAExpert and Node-Red as trusted Clients but now leave it and click close button to exit this popup window. we will do that future.
UAExpert Configuration


Open UAExpert and click on the + button shown in the top bar and set OPC Server connection , for this click advanced option in the pop up window and then do below configuration on respective places, keep rest of all are default and then click ok
Endpoint Url: opc.tcp://127.0.0.1:49320 (127.0.0.1 is the ip address of Local host , 49320 is the port address of KEPServer UA)
Security Policy: Basic256sha256
Message Security Mode: Sign&Encrypt
Go to Menubar- Server - connect, now the UAExpert will start to act as a UA client of KEPServer ,But this will happen only even if we set UAExpert as a trusted client in OPCUA Configuration app of kepware. We had left that configuration in previous step, Now again open OPC UA configuration app and go to trusted clients tab, Now we can see UAExpert is added on the list but is not a trusted client , This is because UAExpert application has sent a request to KEPWare to be a trusted client when we tried to acces KEP UA from UAExpert application. Select this request and click on trust button to approve. In the upcoming steps the same will again do for Node-Red to be become a trusted client of KEPWare.
Now again go to UAExpet and connect to server, Now you can browse all the tags configured in the KEPServer. Go and browse our configured tag, once you click on our tag then you can see Attributes window on the right pan of the application, Note down the Nodeid of particular tag from there and it will be required at the time of Node-Red configuration. UAExpert is required only to get this Nodeid of tag , Once you got it ,You can close this UAExpert application.
Creation of Telegram Bot

Bots are simply Telegram accounts operated by software – not people – and they'll often have AI features. They can do anything – teach, play, search, broadcast, remind, connect, integrate with other services, or even pass commands to the Internet of Things.
Telegram is allowing to establish chats between two software, To do this we should have some telegram parameters, That are...
Bot-Name: Name of the bot (Free to choose any name)
Telegram Id: The id of your telegram application
Token: token is used to give an access to Node-Red
Open telegram and search for 'BotFather' - Enter into it and use command /newbot - Give Name to your bot - Give a user name to your bot. Note down the token given by BotFather. Use above bot name that you have given and token for telegram sender node configuration at Node-Red in upcoming step.
To know your telegram id, Open the Telegram application and search 'IoTwithus.com' and enter into that bot, then type /myid and enter, now you will get your telegram id as reply of that chat.
Node-Red Configuration


The most important and heart of this project is Node-Red, The basic knowledge about nodes and Node programming enables this project become true. The various nodes that has been used are described below
Timestamp: Helps to trigger a flow for a particular tag, We can decide how and when a flow has to be triggered. Here we would require an automatic triggering within a predefined time gap. Our plan is to trigger the flow every 5 seconds continuously. Double click on the Timestamp and do following configuration
[msg.payload: timestamp, msg.topic: copy the nodeid shown in UAEXpert and paste here as explained previous step, Inject once after: 1seconds, Repeat: Interval, Every: 1 seconds]
OPC UA Client: This node helps to configure Node-Red as a UA Client and give the provision to connect required UA Server. Double click on the OPC UA Client and do following configuration
Edit endpoint - [Endpoint: opc.tcp://127.0.0.1:49320, Security Policy: Basic256sha256, Security Mode: Sign&Encrypt]
[Action: Read, Certificate: None, use generated self-signed certificate, Name: KEPUA Client (it is free to use any name)]
Function: Function node is used to make custom build functions through programming, Here it is programmed to get the communication quality of the required tag. KEPServer will deliver only some codes to denote 'good' and 'bad' qualities, by program we should replace it to a human understandable messages that is going to become a message output at the telegram chat end. The program written in the function is given below.
// Program starts here
var i;
var j = context.get('i');
if (msg.statusCode == "0"){
i = "Good"
}
if (msg.statusCode == "2147483648"){
i = "Bad"
}
context.set('i',i);
if (i !=j){
msg.payload = {
chatId: '*********',
type:'message',
content: 'The quality of tag1 is '+ i
}
return msg;
}
// Program ends here
The ChatId given in the program is the Telegram Id of telegram application that I have used for this project (Here it is hided with star), it will be unique for different telegram instances installed in each users mobile phone. Telegram Id creation has been explained in the previous step. Find your telegram Id and replace that id at the place of star given in the above program.
Telegram Sender: This is our final node that we have used to send message to our telegram application. What data has got as an input to this node will send as a message to telegram chat. Open this node and do following configurations.
Edit Bot - [Bot-Name: Your Telegram bot name, Token: Your telegram token]
You should have a telegram bot to know the bot-name and token parameters. How to create a telegram bot and how to know the token? This has explained in the previous step detail. Use that token here. What is the token? token is used to give an access to our bot for HTTP API(Here it is Node-Red)
Deploy the nodes, The communication will start only if the Node-Red would become a trusted client of KEP UA Server. For this open OPC UA configuration app and go to trusted clients tab, Now we can see Node-Red is added on the list but is not a trusted client, This is because Node-Red application has sent a request to KEPWare to be a trusted client when we do deploy the Nodes. Select this request and click on trust button to approve.
Result: Go to Modsim and do connection connect and disconnect a number of times to make communication Bad and Good alternatively, You will get a telegram notification of communication quality every time when quality has been changed.