Sending Binary / HEX data to Socket?

1,840 views
Skip to first unread message

Carl Hussey

unread,
Mar 8, 2017, 11:21:35 PM3/8/17
to Node-RED

I was just introduced to Node-Red after asking around for some suggestions on an IoT setup. I have a piece of javascript code that is sending data to a web socket. The code that it is sending is in a HEX format and is sent to the web socket.

I am trying to replicate this using node-red and I am having some trouble figuring out which node to use for sending the data.


Vanilla Javascript:

function connectToSocket() {

    // Try to connect to the socket
    try {

        // Create our socket connection
        connection = new WebSocket('ws://' + gatewayIP + ':8000');
        connection.binaryType = "arraybuffer";

        // Failed to  create the socket connection  
    } catch (e) {

        // Log error message
        logMessage('Failed to connect to socket');
        return;

    }

}

connection.send('\x02\x00\x01\x04\x26\x2D');
I have tried sending this as a string and json object as msg.payload but it is not triggering the device as I expect it to such as when I run the normal JS function in a browser.


What would be an appropriate format to send this hex string in?

Dave C-J

unread,
Mar 9, 2017, 2:29:38 AM3/9/17
to node...@googlegroups.com
Also posted on also, answered there.

Dave C-J

unread,
Mar 9, 2017, 3:13:57 AM3/9/17
to node...@googlegroups.com
For posterity - the equivalent would be an inject node followed by a function node followed by the websocket you already have. With the function node containing. 
    msg.payload = new Buffer([0x02, 0x00, 0x01, 0x04, 0x26, 0x2D]);  
    return msg;
Reply all
Reply to author
Forward
0 new messages