-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuadRelay.js
61 lines (48 loc) · 1.46 KB
/
QuadRelay.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
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Created by prawda on 12.01.2015.
*/
var Q = require('q');
var Tinkerforge = require('tinkerforge');
// connection variables
var HOST = 'localhost';
var PORT = 4223;
var UID = 'mUn';
var delay = 1000; // relay delay in ms
var ipcon = new Tinkerforge.IPConnection(); // Create IP connection
var iqr = new Tinkerforge.BrickletIndustrialQuadRelay(UID, ipcon); // Create device object
exports.flop = flopRelais;
// controlling variables
var connected = false;
// Connect to brickd
ipcon.connect(HOST, PORT,
function(error) {
console.log('Error: '+error);
}
);
// Don't do stuff on device before ipcon is connected
ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED,
function(connectReason) {
connected = true;
}
);
function disconnect() {
ipcon.disconnect();
connected = false;
};
var queue = Q.Promise(function(resolve, reject, notify) {
resolve();
});
function flopRelais(id) {
var shitzens = function() {
return Q.Promise(function(resolve, reject, notify) {
//close and open relais (1=0001, 2=0010, 4=0100, 8=1000)
var relais = {1:1, 2:2, 3:4, 4:8};
iqr.setMonoflop(relais[id], relais[id], delay);
console.log("flop "+relais[id]+"!");
// resolve after delay
//setTimeout(function() { resolve(); }, delay+10);
setTimeout(resolve, delay+10);
});
};
queue = queue.then(shitzens).fail(function(err) { console.log( err )});
}