OPC-UA Example

This example-script should show you the usage of the OPC-UA-Functions and also the encoding from data into JSON-Objects. Before you want to run the script you need this custom OPC-UA-Server, which should run on the most Linux-Versions (tested on Ubuntu 14.04). This Server simulates an OPC-UA-Server in a Waterstation. The Waterstation consists of 2 Watertanks, 1 Coolingwaterpipe and 1 Wastewaterpipe all of the parts are monitored with a thermometer. The thermometers are represented in the Server-Addressspace as Nodes. Additional an ErrorNode in the Addressspace shows actual Errors of the Server and the Waterstation.

The following script will connect every minute to the given OPC-UA-Server and publish the values to the given TCP-Server. Also it checks the ErrorNode from the OPC-UA-Server if there is any problem. If there is an error the script sends automatically an E-Mail to the given E-Mail-Address.

To visualize the data in the cloud maybe check http://nodered.org/ and http://freeboard.io/. This runtime is built on Node.js therefore the encode-function converts the data to a JSON-Object.

With a simple TCP-Connection you can upload the data.

/* DESC: This script is an example, which simulates a Waterstation
 * it will observe the OPC-UA-Server, inform by email when an error occours 
 * and publishs the data from the machine to a TCP-Server
 * Copyright (C) 2012 NetModule AG
void usage() 
    printf("usage: testPlant1opcua.are <opcuaserver> <opcuaserverport> <tcpserver> <tcpport> <email>\n");
if (argc < 6) {
opcuaserver = "opc.tcp://";
opcuaserver = strcat(opcuaserver, argv[1]);
opcuaserver = strcat(opcuaserver, ":");
opcuaserver = strcat(opcuaserver, argv[2]);
tcpserver = argv[3];
tcpport = (int) argv[4];
email = argv[5];
    client_0 = nb_opcua_connect(opcuaserver);
    upload = nb_opcua_search(client_0, "Temp");
    error = nb_opcua_read(client_0, 1, 99999);
    sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock < 0) {
        print("unable to open socket\n");
    if (connect(sock, tcpserver, tcpport) < 0){
        printf("Could not connect to %s (port %d)\n", tcpserver, tcpport);
    msg = nb_encode_2json(upload);
    sent = send(sock, msg);
    if (sent == -1) {
        printf("Failed to send message\n");
    printf("Sent %d bytes\n", sent);
    if(error != 0){
        msg = strcat("At testPlant1 occured an error with errorcode ", %d, " please check this!!");
        ret = nb_mail_send(email, "testPlant1 error message", msg);
        if (ret == 0) {
            printf("Successfully sent E-Mail to %s\n", to);
        } else {
            printf("Unable to send E-Mail\n");

To simulate the changing temperature maybe you can connect to the OPC-UA-Server periodical with a script like the following to change the values.

client_0 = nb_opcua_connect("opc.tcp://.....:4841");		//fill in the Server-Address
ret = nb_opcua_read(client_0, 1, 99992);
if(ret >= 40){
   ret = nb_opcua_write(client_0, 1, 99990, 5);
   ret = nb_opcua_write(client_0, 1, 99991, 10); 
   ret = nb_opcua_write(client_0, 1, 99992, -9); 
   ret = nb_opcua_write(client_0, 1, 99993, 0); 
} else { 
   ret = nb_opcua_write(client_0, 1, 99990, ret+1); 
   ret = nb_opcua_write(client_0, 1, 99991, ret+2); 
   ret = nb_opcua_write(client_0, 1, 99992, ret+1); 
   ret = nb_opcua_write(client_0, 1, 99993, ret+4); 

If everything works like explained your visualization in the cloud could look like this. On the left side there are the actual temperaturevalues in the middle there is a history of the values and on the right side you can also see the actual weather of Winterthur.