Skip to content

Instantly share code, notes, and snippets.

@Grayda
Last active July 12, 2018 01:11
Show Gist options
  • Save Grayda/5581263f41da6bddec84cd7c25965e0a to your computer and use it in GitHub Desktop.
Save Grayda/5581263f41da6bddec84cd7c25965e0a to your computer and use it in GitHub Desktop.

Revisions

  1. Grayda revised this gist Jul 12, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion usbrelay.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ var _ = require("lodash")

    var device

    setup()
    device = setup()
    setInterval(function() {
    setState(!getState())
    console.log("Relay enabled? " + getState())
  2. Grayda revised this gist Jul 9, 2018. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions usbrelay.js
    Original file line number Diff line number Diff line change
    @@ -26,10 +26,15 @@ function setup() {
    }

    function getState() {
    // Byte 8 = State. 3 = on, 0 = off
    return device.getFeatureReport(0x00, 0x10)[8] !== 0 ? true : false
    }

    function setState(state) {
    // Byte 0 = Report ID
    // Byte 1 = State
    // Byte 2 = Relay
    // Bytes 3-8 = Padding
    var ON = [0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
    var OFF = [0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
    device.sendFeatureReport(state ? ON : OFF)
  3. Grayda created this gist Jul 9, 2018.
    36 changes: 36 additions & 0 deletions usbrelay.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    var HID = require('node-hid');
    var _ = require("lodash")

    var device

    setup()
    setInterval(function() {
    setState(!getState())
    console.log("Relay enabled? " + getState())
    }, 1000)

    function setup() {
    relay = _.find(HID.devices(), function(item) {
    return item.product.indexOf("USBRelay") != -1
    })

    if (relay) {
    console.log("Found relay!")
    return new HID.HID(relay.path);
    } else {
    console.log("No relay found!")
    return false
    }


    }

    function getState() {
    return device.getFeatureReport(0x00, 0x10)[8] !== 0 ? true : false
    }

    function setState(state) {
    var ON = [0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
    var OFF = [0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
    device.sendFeatureReport(state ? ON : OFF)
    }