Created
August 26, 2022 02:52
-
-
Save bendytree/7a1c85c1c508bcdf3e6a5ae826a6ad00 to your computer and use it in GitHub Desktop.
NOYITO USB Control Relay in NodeJS
This file contains hidden or 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
const HID = require('node-hid'); | |
class Relay { | |
constructor() { | |
const devices = HID.devices(); | |
const deviceInfo = devices.find(d => d.product === 'USBRelay8'); | |
this.device = new HID.HID(deviceInfo.path); | |
} | |
set(n, on) { | |
const send = Array(9).fill(0x0); | |
send[1] = on ? 0xff : 0xfd; | |
send[2] = n; // n is 1-8 | |
this.device.sendFeatureReport(send); | |
} | |
setAll(on) { | |
const send = Array(9).fill(0x0); | |
send[1] = on ? 0xfe : 0xfc; | |
this.device.sendFeatureReport(send); | |
} | |
disconnect() { | |
this.device.close(); | |
} | |
} | |
// Example usage | |
const relay = new Relay(); | |
relay.setAll(false); | |
relay.set(1, true); // 1 is the first relay, 8 is the last |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment