-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample08.js
47 lines (41 loc) · 1.28 KB
/
example08.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
var http = require("http").createServer(handler);
var io = require("socket.io").listen(http);
var fs = require("fs");
var firmata = require("firmata");
var board = new firmata.Board("/dev/ttyACM0", function(){
console.log("Enabling analog Pin 0");
board.pinMode(0, board.MODES.ANALOG);
console.log("Enabling analog Pin 1");
board.pinMode(1, board.MODES.ANALOG);
});
function handler(req, res) {
fs.readFile(__dirname + "/example08.html",
function(err, data) {
if (err) {
res.writeHea(500, {"Content-Type": "text/plain"});
return res.end("Error loading html page.");
}
res.writeHead(200);
res.end(data);
})
}
var desiredValue = 0;
var actualValue = 0; // variable for seccond potentiometer
http.listen(8080);
io.sockets.on("connection", function(socket) {
board.analogRead(0, function(value){
desiredValue = value; // continuous read of analog pin 0
});
board.analogRead(1, function(value) {
actualValue = value; // continuous read of pin A1
});
socket.emit("messageToClient", "Server connected, board ready.");
setInterval(sendValues, 40, socket); // na 40ms we send message to client
});
function sendValues (socket) {
socket.emit("clientReadValues",
{
"desiredValue": desiredValue,
"actualValue": actualValue
});
};