-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (30 loc) · 831 Bytes
/
index.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
const fastify = require('fastify')
const sockets = require('socket.io')
let five = require("johnny-five")
let board = new five.Board()
const app = fastify()
// Our state variable
let isLightOn = false
let io
const start = async () => {
await app.listen(8080)
io = sockets(app.server)
};
// Working with the photoresistor
board.on('ready', async function() {
await start()
// Assign the light detection to analog port 0 and set scanning to every couple secs
var light = new five.Light({
pin: "A0",
freq: 1000
});
io.on('connection', (socket) => {
console.log(`${socket.id} connected`)
// Get light data from photocell
light.on("change", function() {
console.log(this.level)
socket.emit('light:on', this.level
)
})
})
})