Control the Sainsmart 16 relay USB HID device from node.js.
I'll be using this module to create an irrigation controller. Until I'm satisfied with its meatspace operation, this should be considered alpha software!
$ npm install sainsmart-16-hid
const Relay = ;let relay = ;// enable relays 0 and 4relay;// enable relays 14 and 15relay;// leave 14 & 15 enabled, but enable relay 5relay;// check to see if relay 14 was enabledMath & ~relaystate === 0;// orrelaystateArray14 === true;// turn off relay 14relay;
Keep in mind that constructing a Relay object will send a reset command to the module and set all relays to zero. This is so that your control module can begin in a known state in the event of a crash or restart. If you need to access the state of the usb device when set by another piece of software, consider another library.
Path is optional, and can be used to specify a specific device for node-hid. If not specified, the first Sainsmart 16-relay USB HID device found will be used.
16-bit integer representation of the state of the relays.
means relay 0 is enabled,
0b1000000000000000 means relay 15 is enabled, etc.
Another representation of the relay states, an array of booleans.
relaystateArray // relays 0, 4, and 15 are enabled> true false false false true false false false true false false false false false false true;
Sends a read command to the USB device.
callback(err, data) (required) will
include any error and data will be the 16-bit integer representation of the
state of the relays.
relay.stateArray will be updated when
this function completes. Note that you shouldn't need this function. Each time
set is called this information can be accessed from
Used to change the state of all the relays on the device. For example:
// turn on relays 0, 3, and 14. everything else off.relay;// turn everything off except relays 5 & 6relay;
Turn a specific relay on or off, without affecting the state of other relays.
relay;relay;relay;relaystateArray> false true false false false false false false false false false false false true false
All credit goes to mvines/relay for documenting the protocol for communicating with this device. I just made an API that fits my needs.