Last active
July 12, 2018 01:11
-
-
Save Grayda/5581263f41da6bddec84cd7c25965e0a to your computer and use it in GitHub Desktop.
Control 5v dcttech.com relay using node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var HID = require('node-hid'); | |
var _ = require("lodash") | |
var device | |
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() { | |
// 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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment