OutsourcedGuru / opcua-node

An OPC-UA node for serving up a 3D printer interface.
MIT License
3 stars 1 forks source link

doubt regarding node id #2

Open ayonaphilipose opened 5 years ago

ayonaphilipose commented 5 years ago

I have created opcua node. now I am able to get free memory from node ns=1;s=free_memory. what is the node id for getting temperature values?

OutsourcedGuru commented 5 years ago

The version you see here is mostly just a proof-of-concept and a starter point so that others might add more functionality. The OPC-UA specifications are huge and there are several data models which have been adopted by different groups.

Each node should have its own ID of course. A minimal implementation might look like what's described here (click the "Nano Embedded Device 2017 Server Profile" link on the left and then review the "Core 2017 Server Facet" section).

For all this to be useful, you'll also need an OPC-UA client which I'll show below. It might need slight adjustments for your own setup:

var async =                                 require('async');
var opcua =                                 require('node-opcua');
var client =                                new opcua.OPCUAClient();
var macBook =                               'macbook.local';
var port =                                  4334;
var endpointUrl =                           'opc.tcp://' + macBook + ':' + port.toString() + '/UA/octopi';
var the_session =                           null;
async.series([
    // ------------------------------------------------
    // 1. Connect to an OPC UA node
    // ------------------------------------------------
    function(callback)  {
        client.connect(endpointUrl, function(err) {
            if (err) {console.log(' cannot connect to endpoint :', endpointUrl);}
            else     {console.log('connected');}
            callback(err);
        });
    },

    // ------------------------------------------------
    // 2. Create a session
    // ------------------------------------------------
    function(callback) {
        client.createSession( function(err, session) {
            if (!err) {the_session = session;}
            callback(err);
        });
    },

    // ------------------------------------------------
    // 3. Browse
    // ------------------------------------------------
    function(callback) {
        the_session.browse('RootFolder', function(err, browse_result) {
            if (!err) {
                browse_result.references.forEach(function(reference) {
                    console.log( reference.browseName);
                });
            }
            callback(err);
        });
    },

    // ------------------------------------------------
    // 4. Read a variable
    // ------------------------------------------------
    function(callback) {
        the_session.readVariableValue('ns=1;s=free_memory', function(err, dataValue) {
            if (!err) {
                console.log(' memory = ', dataValue.toString());
                console.log('   In other words, the available RAM is now ' +
                    (Math.round(parseFloat(dataValue.value.value) * 100, 2)).toString() + '%')
            }
            callback(err);
        })
    },

    // ------------------------------------------------
    // 5. Close the session
    // ------------------------------------------------
    function(callback) {
        console.log(' closing session');
        the_session.close(function(err){

            console.log(' session closed');
            callback();
        });
    },
],  function(err) {
    if (err) {console.log(' failure ', err);}
    else     {console.log('done')}
    client.disconnect(function(){});
}); // async.series()
ayonaphilipose commented 5 years ago

Thank u. can u tell me how to create a new node at server side to read temperature of 3D printer?

OutsourcedGuru commented 5 years ago

Think of this part of the existing code as the basic building block for adding a new OPC-UA node:

        /**
         * returns the percentage of free memory on the running machine
         * @return {double}
         */
        function available_memory() {
            // var value = process.memoryUsage().heapUsed / 1000000;
            var percentageMemUsed = os.freemem() / os.totalmem() * 100.0;
            return percentageMemUsed;
        } // function available_memory()
        server.engine.addressSpace.addVariable({
            componentOf: device,
            nodeId:      'ns=1;s=free_memory', // a string nodeID
            browseName:  'FreeMemory',
            dataType:    'Double',    
            value: { get: function () {
                return new opcua.Variant({dataType: opcua.DataType.Double, value: available_memory() });
              }
            }
        }); // server.engine.addressSpace.addVariable()

The second function is the API part that sends a packet of information back to the client. Note that it calls available_memory() to know what data is to be sent.

In the case where you're trying to return the temperature of the first hotend, for example, you might create a second node, name it something appropriate and create a function will queries the printer for its temperature. At this point, you want to notice that one of the project's dependencies is my own octo-client software which is well-described here.