Difference between revisions of "OPC-UA example with Up Squared and Groove Kit"

From UpWiki
Jump to: navigation, search
(Integrating Node-RED programming tool)
(Creating your OPC-UA server on UP2 boardn)
 
Line 83: Line 83:
 
   sudo node AioA0.js //Get light sensor value.
 
   sudo node AioA0.js //Get light sensor value.
  
== Creating your OPC-UA server on UP2 boardn ==
+
== Creating your OPC-UA server on UP2 board ==
  
 
'''00. Set your OPC-UA server up'''
 
'''00. Set your OPC-UA server up'''

Latest revision as of 08:01, 9 July 2019

In this tutorial, you will be able to get started the guide for OPC-UA protocol demo to connect GrovePi+ kit sensors, over MRAA/UPM library, on UP2 boards.

Platform information

Model / Version: UP-APL01

PCB Model / Version: UP-APL01 A1.0

BIOS / Version: UPA1AM42

CPU Type: Intel(R) Atom(TM) Processor E3950 @ 1.60GHz

OS Install: Linux Ubuntu16.04.5, kernel4.15.0-37

Power Supply: AD36AM050600 5V / 6A adapter

Sensor HAT: GrovePi+ kit for Intel & UP family. V1.1

Testing GrovePi+ kit for MRAA/UPM library

The first step for our example is to validate the GrovePi+ on our UP2 board. We could use the board for setting the OPC-UA server later. It’s important to check the Grove version as we need the special hardware revision defined “for Intel & UP board kit”. Also, verify you have at least two Grove sensors, for example two input/output and analogic/digital sensors, to validate all possibilities on the Grove kit.

00. Set your UP2 board up for GrovePi+:

Here, we must install a Ubuntu 16.04 OS and follow the correct steps to install our UP family kernel 4.15. All the instructions are in the UP community:

  https://wiki.up-community.org/Ubuntu#Ubuntu_16.04_installation_and_configuration

The next step is to install all needed libraries including MRAA/UPM library and, NPM and NodeJS packages. Please, follow the simple instructions for MRAA/UPM library installation in the UP community:

  https://wiki.up-community.org/MRAA/UPM#Setup

Now, we have to install the GrovePi+ modules and repository as well, following the instructions in the UP community:

  https://wiki.up-community.org/GrovePi

Once we have all libraries and packages, we must to install the NodeJS modules necessary to run our Grove sensors over JavaScript language. For that step we only must run the next command to include them to the node_modules folder:

  npm install mraa
  npm install upm
  npm install node-grovepi

01. Python samples for Grove sensors

A quick test for your GrovePi+ kit is always good to be sure it’s working in the simplest way.

You could clone the IntelUP2Grove repository and run the typical samples like the blinking LED or the light sensor:

  https://github.com/joemcmanus/IntelUp2Grove
  git clone https://github.com/joemcmanus/IntelUp2Grove.git
  cd IntelUp2Grove/

Connect your LED socket to the D8 connector and run the script for blinking the LED:

  sudo python led.py 8 (n.8 is the pin defined D8 connector)

Connect your Light sensor to the A0 connector and run the script for getting light values:

  sudo python light.py

02. NodeJS samples for Grove sensorss

If the Python samples were working correctly, now we could adapt the JavaScript samples from libMRAA repository to access to the Grove Kit. That will be perfect to include the include the NodeJS samples to our OPC-UA server script which is running over LibMRAA as well.

We just need to add the GROVEPI sub-platfrom to our sample and set the correct pinout for each sensor. NOTE: Remember the GrovePi+ pinout starts for 512 as A0. For D8 you will need to add +8 to the 512-pin base, so for LED sample we will use the 520-pin. First at all clone the libmraa repository and go to the JavaScript examples:

  git clone https://github.com/intel-iot-devkit/mraa.git
  cd mraa/examples/javascript

For LED blinking sample:

  sudo nano Blink-IO.js

Add after mraa requirements:

  mraa.addSubplatform(mraa.GROVEPI, "0"); 

Edit mrra GPIO pin for D8 GrovePi+:

  let myLed = new mraa.Gpio(520);
  sudo node Blink-IO.js //LED will blink

For Light sensor sample:

  sudo nano AioA0.js

Add after mraa requirements:

  mraa.addSubplatform(mraa.GROVEPI, "0"); 

Edit mrra GPIO pin for A0 GrovePi+:

  let myLed = new mraa.Gpio(512);
  sudo node AioA0.js //Get light sensor value.

Creating your OPC-UA server on UP2 board

00. Set your OPC-UA server up

For our UP2 server we could use the same system installed in the previous point.

If you are using a new UP2, we must install a Ubuntu 16.04 OS and follow the correct steps to install our UP family kernel 4.15. All the instructions are in the UP community:

  https://wiki.up-community.org/Ubuntu#Ubuntu_16.04_installation_and_configuration

The next step is to install all needed libraries including MRAA/UPM library and, NPM and NodeJS packages. Please, follow the simple instructions for MRAA/UPM library installation in the UP community:

  https://wiki.up-community.org/MRAA/UPM#Setup

Now, we have to install the GrovePi+ modules and repository as well, following the instructions in the UP community:

  https://wiki.up-community.org/GrovePi

01. Include Node-opcua library

We need to link the OPC-UA protocol package to the NodeJS modules. That means, we need to install the npm node package into our machine:

  npm install node-opcua

Also, we could install the OPC-UA examples:

  npm install node-opcua-samples

NOTE: If you find any error or fail during installation, please make sure your NodeJS version is equal or newer than 6.0. Even if you have problems to get the NodeJS 6 installed correctly, you can follow the next steps:

  • Create a new file in /etc/apt/sources.list.d/:
  sudo nano nodesource.list

Add the next lines into the file:

  deb https://deb.nodesource.com/node_6.x xenial main
  deb-src https://deb.nodesource.com/node_6.x xenial main
  • Download the GPG Signing Key from Nodesource for the repository:
  curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add -
  • Manually run:
  sudo apt update
  • Install updated nodejs package:
  sudo apt install nodejs

In the same way, you can install the node package from the repository source:

  https://github.com/node-opcua/node-opcua
  $ git clone https://github.com/node-opcua/node-opcua.git
  $ cd node-opcua
  $ npm install

02. Create your OPC-UA server script

Now, we are ready to create a first OPC-UA server example for NodeJS over MRAA library. A clean and popular way to do it is following a typical structure for a server:

  https://github.com/node-opcua/node-opcua/blob/master/documentation/creating_a_server.md

Here you can see, step by step, how to create the “skeleton” for a generic server.

First at all, let’s add our Node workspace for the server:

  $ mkdir myserver
  $ cd myserver
  $ npm init                      # create a package.json
  $ npm install node-opcua --save # add the node-opcua

Once we have your Node space created for our server, we must to install the NodeJS modules necessary to run our Grove sensors over JavaScript language. For that step we only have to run the next command to include them to the node_modules folder:

  npm install mraa
  npm install upm
  npm install node-grovepi

Let’s create our server script:

  • Declarations and requirements:

OPC UA 01.png

  • Define OPC-UA server:

OPC UA 02.png

  • Create server space where we define the variables controlled by server.

In this case, we define the variable to get the value from light sensor in GrovePi+ kit: OPC UA 03.png

  • Initialise the server with our space inside and start the end-point server to be available for clients:

OPC UA 04.png

Creating your OPC-UA client for UP2 board

00. Set your OPC-UA client

For our UP2 client sample, we must install a Ubuntu 16.04 OS and follow the correct steps to install our UP family kernel 4.15. All the instructions are in the UP community:

  https://wiki.up-community.org/Ubuntu#Ubuntu_16.04_installation_and_configuration

The next step is to install all needed libraries including MRAA/UPM library and, NPM and NodeJS packages: Please, follow the simple instructions for MRAA/UPM library installation in the UP community:

  https://wiki.up-community.org/MRAA/UPM#Setup

Now, we have to install the GrovePi+ modules and repository as well, following the instructions in the UP community:

  https://wiki.up-community.org/GrovePi

01. Include Node-opcua library

We need to link the OPC-UA protocol package to the NodeJS modules. That means, we need to install the npm node package into our machine:

  npm install node-opcua

Also, we could install the OPC-UA examples:

  npm install node-opcua-samples

NOTE: If you find any error or fail during installation, please make sure your NodeJS version is equal or newer than 6.0. Even if you have problems to get the NodeJS 6 installed correctly, you can follow the next steps:

  • Create a new file in /etc/apt/sources.list.d/:
  sudo nano nodesource.list

Add the next lines into the file:

  deb https://deb.nodesource.com/node_6.x xenial main
  deb-src https://deb.nodesource.com/node_6.x xenial main
  • Download the GPG Signing Key from Nodesource for the repository:
  curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add -
  • Manually run:
  sudo apt update
  • Install updated nodejs package:
  sudo apt install nodejs

In the same way, you can install the node package from the repository source:

  https://github.com/node-opcua/node-opcua
  $ git clone https://github.com/node-opcua/node-opcua.git
  $ cd node-opcua
  $ npm install

02. Create your OPC-UA client script

Now, we are ready to create a first OPC-UA client example for NodeJS over MRAA library. A clean and popular way to do it is following a typical structure for a server:

  https://github.com/node-opcua/node-opcua/blob/master/documentation/creating_a_client.md

Here you can see, step by step, how to create the “skeleton” for a generic and simple client to communicate with our server via OPC-UA protocol. First at all, let’s add our Node workspace for the client:

  $ mkdir myclient
  $ cd myclient
  $ npm init                      # create a package.json
  $ npm install node-opcua --save # add the node-opcua
  $ npm install async --save

Once we have your Node space created for our client, we must to install the NodeJS modules necessary to communicate with the server. For that step we only must run the next command to include them to the node_modules folder:

  npm install mraa
  npm install upm

Let’s create our server script:

  • Declarations and requirements:

OPC UA 05.png

  • Define client and end-point server address:

OPC UA 06.png

  • Set the async session:
    • [Connect to server and Create session:]

OPC UA 07.png

    • [Browsing the root folder to list the nodes:]

OPC UA 08.png

    • Read a variable from server. We will receive the light value data from GrovePi+ kit:

OPC UA 09.png

    • Define a subscription for session parameters:

OPC UA 10.png

    • Close session:

OPC UA 11.png

    • Disconnect your client:

OPC UA 12.png

Testing your OPC-UA system sample

In this point, we have server and client integrated in two different machines. So now, it’s time to test the system. The main goal here is to get access to the GrovePi+ sensors from our client via OPC-UA to the server board running over integrated MRAA library.

We need to connect each UP2 boards to the network, via Ethernet. If we have the connection ready, then the server IP is necessary for the client to define where we must listen to. Once, you get the current IP, we add it to the client script in the defined end-point variable:

  const endpointUrl = "opc.tcp://<ServerIP>:<ServerPort>/<ServerPath>";

Now, let’s run the scripts from each directory in our machines:

For server, go to myserver directoty and run your server script:

  sudo node opcua-server.js 

You should see some output like that:

  MRAA Version: v1.9.0
  OPC-UA Server for UPSquared initialized
  Current GroveLight value:  712
  Server is now listening ... ( press CTRL+C to stop) 
  port  4334
  the primary server endpoint url is  opc.tcp://upsquared-UP-APL01:4334/MyUP2

From the client side, you have to go to myclient directory and run your client script:

  sudo node opcua_client.js

You should see some output like that:

  MRAA Version: v1.9.0
  connected !
  New session created!
  Session Browse:
  QualifiedName { namespaceIndex: 0, name: 'Objects' }
  QualifiedName { namespaceIndex: 0, name: 'Types' }
  QualifiedName { namespaceIndex: 0, name: 'Views' }
   GrovePi Light value =  DataValue:
    value:           Variant(Scalar<Float>, value: 566)
    statusCode:      Good (0x00000)
    serverTimestamp: null
    sourceTimestamp: null

  subscription started for 2 seconds - subscriptionId= 272184
  keepalive
  keepalive
  keepalive
  keepalive
   closing session
   session closed
  done!

And again, from the server side you will see the requests to the server to get the variable value:

  MRAA Version: v1.9.0
  OPC-UA Server for UPSquared initialized
  Current GroveLight value:  620
  Server is now listening ... ( press CTRL+C to stop) 
  port  4334
    the primary server endpoint url is  opc.tcp://upsquared-UP-APL01:4334/MyUP2
  Current GroveLight value:  570
  Current GroveLight value:  566

Now, your system is ready and validated to included complex variables and improve your server and client machine.

Integrating Node-RED programming tool

00. Install Node-RED library

Use npm to install node-red and a helper utility called node-red-admin globally:

  sudo npm install -g --unsafe-perm node-red node-red-admin

After installation, we need to define the port used by Node-RED. The default port is always 1880:

  sudo ufw allow 1880

Now, we are ready to lauch the application and start the programming:

  node-red

Then, you will see the Internet address to access to your Node-RED interface:

  http://<IPadress>:1880

Now you must take the link address and access from your browser: OPC UA 13.png

01. Programming a first simple flow sample

Node-RED allows you to wire together nodes to create flows which carry out your programming task. Messages pass between nodes, moving from input nodes through processing nodes to output nodes.

There are three main types of nodes:

Input nodes allow you to input data into a Node-RED application or “flow”. They have at least one output endpoint. You use input nodes to connect data from other services or to manually input data into a flow using the inject node.

Output nodes allow you to send data outside of a Node-RED flow. They have a single input endpoint. You use output nodes to send data to other services or to use the debug node to output to the debug pane.

Processing nodes allow you to process data. They have an input endpoint and one or more output endpoints. They allow you to transform the data type (e.g. json, csv, xml) nodes, use the data to trigger a message nodes and to write custom code that uses the data received (e.g. function node).

Let’s start with a node to inject some information into the flow, wired to a debug node to see the output from the flow as a debug message. Once you have that running, you’ll build it up to the full Hello World flow. OPC UA 14.png

Add the first node that actually does something, an inject node. The inject node is used to generate input into a flow and is one of the first nodes in the node palette under input. You’ll also notice that the inject node (now named timestamp) has a blue dot top right and a grey square centre right. The blue dot indicates that the node hasn’t been deployed since it was last changed; the grey square is the output point for the node. This is where you attach ‘wires’ that route output message from the inject node to the next node in the flow.

To get a sense for the inject node and the whole flow deployment process, let’s add a debug node to see what happens, wire them together and then deploy the flow and test it.

Then you’ll wire the two nodes together. To do that, click on the grey output point for the inject node and, holding the mouse button down, drag towards the debug node. An orange wire appears, which you can then attach to the grey input point on the debug node. You will send the current timestamp to the debug node for display in the debug pane. OPC UA 15.png

Click the deploy button in the Node-RED window (top right). You’ll see a pop-up saying the flow has been successfully deployed.

Now, before you try the flow, make sure the the debug tab is selected on the right pane. Then click on the left tab on the inject node and look at what appears in the debug pane.

As you can see, the inject node, when clicked, generates a timestamp, which is converted to a message and sent along the output wire, which is delivered to the debug node as an input message. The debug node’s default behaviour is to show any message it receives, which it does in the debug pane on the right.

You’ll edit the inject node to deliver a text message rather than a timestamp. To do that, select the inject node in the flow and double-click it. OPC UA 16.png

Once you’ve made the change, click ok to save the changes and take a look at the flow. You’ll see the blue dot has appeared on the inject node to indicate that you have undeployed changes.

Click the deploy button again to resolve that and then go ahead and click the tab on the inject node. If you look at the debug output you’ll see that instead of a timestamp, your text has been delivered as a message to the debug node, which displays it as usual.