Send a VSCP Event from node.js

Here a very simple script that sends a temperature event to the VSCP daemon over the TCP/IP interface. The scripts is listed at the end of this document. The scripts is available in the source tree (/src/vscp/samples/node_js/connect_tcpip_server.js) here

You have a receive event sample here

To test it we can open a TCP/IP session to the server

telnet localhost 9598 

after logging in with

user admin<CR><LF
pass secret<CR><LF
rcvloop<CR><LF>

After doing this you will see a window that is filled with

+OK

showing that the link is open but that no events has been received yet.

we can also open one of the websocket sample pages

http://localhost:8080/testws/simpletext.html 

which looks like this before any events are received over the websocket interface.

Now we test the script and issue

node connect_tcpip_server.js localhost 9598 admin secret -17

the resulting output from the script will be

and the telnet session will show the event

and also the websocket impletext sample

and the websocket connected thermometers look likes this

and if we look at the Google gauge sample instead and send +57 degrees to it as it does not like negative values it will look like this.

You may wounder what we send here? The send command look like this

send 0,10,6,0,0,-,138,0,57

where

  • 0 This is the 'head' nothing we bother about now
  • 10 is the code for the VSCP measurement class CLASS1.MEASUREMENT
  • 6 is the VSCP type which says this is a temperature measurement. CLASS1.MEASUREMENT,Type=6
  • 0 This is the timestamp for the event. The TCP/IP interface will give it a meaningful value.
  • 0 This is the obid which is used by the VSCP dameon.
  • 138 This tells that the data that follows is on an integer format and comes from sensor 2 and is gas the unit “degrees Celius”
  • 0, 57 is the data.

If you want to know more about the VSCP measurement event data coding it is described here. It's not as hard as it first looks.

Here is the code. Happy hacking.

// Example
// =======
// Connect to a remote VSCP daemon and send a temperature event that defaults to
// 25 degrees Celsius but can be set between -128 and 127.
//
// You can read about all the commands in the VCSP TCP/IP interface here
// http://www.vscp.org/docs/vscpd/doku.php?id=vscp_daemon_tcp_ip_protocol_description
//
// Arguments is 'host' 'port' 'user' 'password' 'temperature'
// which defaults to '127.0.0.1' '9598' 'admin' 'secret' '25'
//
// Part of VSCP & Friends - http://www.vscp.org
// akhe@grodansparadis.com, Grodans Paradis AB, http://www.paradiseofthefrog.com
//
var net = require('net');

// State machine
// 0 = Not connected
// 1 = Connected - Username sent
// 2 = Connected - Password sent
// 3 = Connected - Credentials OK

var STATE_NOT_CONNECTED = 0;
var STATE_USERNAME = 1;
var STATE_PASSWORD = 2;
var STATE_CONNECTED = 3;
var nState = STATE_NOT_CONNECTED;

// Get arguments
var HOST = ( typeof process.argv[2] !== 'undefined' ) ? process.argv[2] : '127.0.0.1';
var PORT = ( typeof process.argv[3] !== 'undefined' ) ? process.argv[3] : 9598;
var USER = ( typeof process.argv[4] !== 'undefined' ) ? process.argv[4] : "admin";
var PASSWORD = ( typeof process.argv[5] !== 'undefined' ) ? process.argv[5] : "secret";
var TEMPERATURE = ( typeof process.argv[6] !== 'undefined' ) ? process.argv[6] : 25;

var bDebug = true;  // Set to true to get some console logging

// Create a socket
var client = new net.Socket();

var timeoutConnection = setTimeout( fnTimeout, 3000 );

// Connect to host
client.connect(PORT, HOST );

///////////////////////////////////////////////////////////////////////////////
// Receive data
//

client.on('connect', function() {
    console.log('Connected to "' + HOST + '" on port "' + PORT +'"' );
});


///////////////////////////////////////////////////////////////////////////////
// Receive data
//

client.on('data', function(data) {
    var strReceiveData = data.toString();
    var reg1 = "\+OK Welcome to the VSCP daemon";

    if ( bDebug ) console.log('DATA: ' + data); // Log received data

    if ( STATE_NOT_CONNECTED == nState ) {
        if ( ( -1 != strReceiveData.search("\\+OK Welcome to the VSCP daemon") ) &&
            ( -1 != strReceiveData.search("\\+OK - Success") ) ) {

            if ( bDebug ) console.log('Welcome message received.');

            // OK we have a successful login message
            client.write('user ' + USER + "\r\n" );

            nState = STATE_USERNAME;
        }
    }
    else if ( STATE_USERNAME == nState ) {
        if ( ( -1 != strReceiveData.search('\\+OK') ) ) {
            if ( bDebug ) console.log('Username accepted.');
            nState = STATE_PASSWORD;
            client.write('pass ' + PASSWORD + "\r\n" );
        }
        else {
            nState = STATE_NOT_CONNECTED;
        }
    }
    else if ( STATE_PASSWORD == nState ) {
        if ( ( -1 != strReceiveData.search('\\+OK') ) ) {

            if ( bDebug ) console.log('Password accepted - we can do work.');

            // OK We are connected
            nState = STATE_CONNECTED;

            // OK we managed before timeout
            clearTimeout( global.timeoutConnection );

            // Send temperature event
            if ( bDebug ) console.log('Sending temperature event to VSCP daemon.');
            client.write('send 0,10,6,0,0,-,138,0,' + TEMPERATURE + '\r\n');
        }
        else {
            nState = STATE_NOT_CONNECTED;
        }
    }
    else if ( STATE_CONNECTED == nState ) {
        if ( ( -1 != strReceiveData.search('\\+OK') ) ) {
            console.log('Command OK');
            client.destroy();
        }
        else {
            console.log('Command Error');
            client.destroy();
        }
    }
});

///////////////////////////////////////////////////////////////////////////////
// Close
//

client.on('close', function() {
    console.log('Connection closed');
});

///////////////////////////////////////////////////////////////////////////////
// Close
//

client.on('error', function() {
    console.log('Connection error');
});


var fnTimeout = function(){
    console.log("Unable to connect");
    client.destroy();
}
 
howto/node_js_send_event.txt · Last modified: 2014/10/18 15:04 (external edit)
[unknown button type]
 
Except where otherwise noted, content on this wiki is licensed under the following license: Public Domain
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki