forked from meerasndr/arduino-webcontrolled-led
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
69 lines (55 loc) · 1.4 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
const express = require('express');
const app = express();
const cors = require('cors');
app.use(cors())
const { Board, Led } = require("johnny-five");
const board = new Board();
// let isBlinking = false;
let blink, stopblink, blinkTimes;
const msgStatus = {
msg : '',
isBlinking : false
}
app.get('/blink/:times', (req, res) => {
console.log(req.params)
// res.send("Hellow world!");
msgStatus.msg = 'Começando a festa...';
msgStatus.isBlinking = true;
res.json(msgStatus);
blinkTimes(Number(req.params.times))
// blink()
})
app.get('/stopblink', (req, res) => {
// res.send("Bye!!")
// res.json({msg: 'Bye', status: isBlinking})
msgStatus.msg = 'Desligando';
res.json(msgStatus);
stopblink()
})
app.listen(3000, () => {
console.log("Server has started and is listening on port 3000")
})
board.on("ready", () => {
console.log('Board ready')
const led = new Led(13);
let timesRun = 0;
blinkTimes = (cycles) => {
let interval = setInterval(() => {
led.toggle()
timesRun += 1;
console.log('pisca');
isBlinking = true;
if (timesRun === (cycles*2) ) {
clearInterval(interval);
timesRun = 0;
}
},1000)
}
blink = () => {
led.blink(1000);
}
stopblink = () => {
led.stop()
led.off()
}
});