On the HW part, we will add additional LED. 220Ω resistor should be put between the digital output on PIN8 and positive contact of LED. In this case, we use PIN13 and PIN8.
On the software part we have to add additional PIN8 at the initialization:
The following modified code will switch on and off new LED according to the passed parameter in the request, 1 or 3 will switch LEDs on, 0 and 2 will switch the LEDs off:
if (operator == 0) {
console.log("Putting led to OFF");
board.digitalWrite(13, board.LOW);
}
if (operator == 1) {
console.log("Putting led ON");
board.digitalWrite(13, board.HIGH);
}
if (operator == 2) {
console.log("Putting led OFF");
board.digitalWrite(8, board.LOW);
}
if (operator == 3) {
console.log("Putting led ON");
board.digitalWrite(8, board.HIGH);
}
Therefore, 1 will turn on first LED and 3 will turn on second led. 0 and 2 switch them OFF respectively.
In order to turn second led ON you should put in the address line: http://192.168.254.128:8080/3 (with your IP)
and press ENTER:
The code for example02.js is printed on the next lines, you should enter your computer IP address:
var http = require("http");
var firmata = require("firmata");
var board = new firmata.Board("/dev/ttyACM0", function(){ // ACM Abstract Control Model for serial communication with Arduino (could be USB)
console.log("Connecting to Arduino");
console.log("Activation of Pin 8");
board.pinMode(8, board.MODES.OUTPUT); // Configures the specified pin to behave either as an input or an output.
console.log("Activation of Pin 13");
board.pinMode(13, board.MODES.OUTPUT); // Configures the specified pin to behave either as an input or an output.
});
http.createServer(function(req, res){ // http.createServer([requestListener]) | The requestListener is a function which is automatically added to the 'request' event.
var parts = req.url.split("/"), // split request url on "/" character
operator = parseInt(parts[1],10); // 10 is radix - decimal notation; the base in mathematical numeral systems (from 2 to 36)
if (operator == 0) {
console.log("Putting led to OFF");
board.digitalWrite(13, board.LOW);
}
if (operator == 1) {
console.log("Putting led ON");
board.digitalWrite(13, board.HIGH);
}
if (operator == 2) {
console.log("Putting led OFF");
board.digitalWrite(8, board.LOW);
}
if (operator == 3) {
console.log("Putting led ON");
board.digitalWrite(8, board.HIGH);
}
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("For test write into browser address line: http://172.16.22.248:8080/1 \n");
res.end("The value of operator: " + operator);
}).listen(8080, "172.16.22.248");