-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclient.js
56 lines (45 loc) · 1.13 KB
/
client.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
/*
* Setup
*/
//1. specify domain and port of your socket.io server
var socket = require('socket.io-client')('http://localhost:5040');
//2. create instance johnny-five Arduino board.
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
/*
* Define your application below
*/
// 1. initialize your hardware
var led = new five.Led(13);
var button = new five.Button(8);
// 2. Create socket message receiver
socket.on('led', function(data){
if(data.command === 'on'){
led.on();
}else if(data.command === 'off'){
led.off();
}
});
// 3. Create socket message emitter
button.on('press', function() {
socket.emit('press', {pin:8});
});
/*
* Socket connection logger
* Nice to console log when socket connection is lost/alive
*/
socket.on('connect', function(){
console.log('Socket Connected');
});
socket.on('disconnect', function(){
console.log('Socket Disconnected !');
});
/*
* REPL
* You can specify command to use from node REPL. Nice for debugging.
*/
this.repl.inject({
blink: function(){led.blink();}
});
});