-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
49 lines (42 loc) · 1.09 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var comms = require('ncd-red-comm');
var MCP23008 = require('./index.js');
/*
* Allows use of a USB to I2C converter form ncd.io
*/
var port = '/dev/tty.usbserial-DN03Q7F9';
var serial = new comms.NcdSerial('/dev/tty.usbserial-DN03Q7F9', 115200);
var comm = new comms.NcdSerialI2C(serial, 0);
/*
* Use the native I2C port on the Raspberry Pi
*/
//var comm = new comms.NcdI2C(1);
/*
* Initialize as a 4-channel relay board
*/
// 1 = output, 2 = input
var config = {
io_1: 1,
io_2: 1,
io_3: 1,
io_4: 1,
io_5: 0,
io_6: 0,
io_7: 0,
io_8: 0
}
var relay_board = new MCP23008(0x20, config, comm);
var current_relay = 1;
function switch_relay(){
relay_board.get().then((status) => {
//channel_1 is the first argument to set the first GPIO
var ch = 'channel_'+current_relay;
//a value of 1 will turn on an output, 0 will turn it off
var update = status[ch] == 1 ? 0 : 1;
relay_board.set(ch, update).then(() => {
if(current_relay == 4) current_relay = 1;
else current_relay++;
switch_relay();
}).catch(console.log);
}).catch(console.log);
}
switch_relay();