-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
79 lines (64 loc) · 2.33 KB
/
server.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var http = require('http');
var fs = require('fs');
const five = require("johnny-five");
const board = new five.Board();
// Loading the index file . html displayed to the client
var server = http.createServer(function(req, res) {
fs.readFile('./index.html', 'utf-8', function(error, content) {
res.writeHead(200, {"Content-Type": "text/html"});
res.end(content);
});
});
// Loading socket.io
var io = require('socket.io').listen(server).origins("localhost:8081");
var events = require('events');
var em = new events.EventEmitter();
board.on("ready", function() {
console.log("Ready!");
const relay1 = new five.Pin(2);
relay1.high();
const relay2 = new five.Pin(3);
relay2.high();
const relay3 = new five.Pin(4);
relay3.high();
const relay4 = new five.Pin(5);
relay4.high();
const sensorA0 = new five.Sensor({ pin: "A0", freq: 5000 });
const sensorA1 = new five.Sensor({ pin: "A1", freq: 5000 });
sensorA0.on('change', (_value) => {
em.emit('SensorA0', sensorA0.scaleTo(0, 100));
});
sensorA1.on('change', (_value) => {
em.emit('SensorA1', sensorA1.scaleTo(0, 10));
});
io.sockets.on('connection', function (socket) {
console.log('A client is connected!');
socket.on("Relay1", (status) => {
// console.log("Relay1 status: " + status);
if (status === true) relay1.high();
else if (status === false) relay1.low();
});
socket.on("Relay2", (status) => {
// console.log("Relay2 status: " + status);
if (status === true) relay2.high();
else if (status === false) relay2.low();
});
socket.on("Relay3", (status) => {
// console.log("Relay3 status: " + status);
if (status === true) relay3.high();
else if (status === false) relay3.low();
});
socket.on("Relay4", (status) => {
// console.log("Relay4 status: " + status);
if (status === true) relay4.high();
else if (status === false) relay4.low();
});
em.on("SensorA0", (value) => {
socket.emit("SensorA0", value);
});
em.on("SensorA1", (value) => {
socket.emit("SensorA1", value);
});
});
});
server.listen(8080);