In this example we will use digital PIN2 as an input. The electronic schematic is shown in the following figure. We have added 10k Ohm resistor as the pull-down resistor, which is connected between GND and PIN2:
Breadboard view is shown in the following figure. The push button is added which puts +5V to the digital PIN2:
We attach additional input PIN2:
console.log("Enabling Push Button on pin 2");
board.pinMode(2, board.MODES.INPUT);
The code that reads the pin value and send it to client:
board.digitalRead(2, function(value) {
if (value == 0) {
console.log("LED off");
board.digitalWrite(13, board.LOW);
sendValueViaWebSocket("0");
}
if (value == 1) {
console.log("LED on");
board.digitalWrite(13, board.HIGH);
sendValueViaWebSocket("1");
}
On client side we first add the div to display messages in the html:
<div id="print1"></div>
State that we will declare strict mode in order to use classes:
<script type="text/javascript">
"use strict"; // in order to use clasess
Define the variable for divElement:
var divElement = document.getElementById("print1"); // variable for div object where the values will be printed (logged)
and then add function to write to div:
function log(msg) {
divElement.innerHTML += "<div>" + msg + "</div>"; // we print it to div
}
On message we print the value to div:
ws.onmessage = function (event) {
log(event.data);
}
We also add the message on disconnect:
ws.onclose = function (value) {
log("Connection closed.");
}
The results of the messages in the GUI are shown in the next figure:
var http = require("http").createServer(handler);
var fs = require("fs"); // variable for file system
var firmata = require("firmata");
const WebSocket = require('ws');
const wss = new WebSocket.Server({port: 8888});
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 13");
board.pinMode(13, board.MODES.OUTPUT); // Configures the specified pin to behave either as an input or an output.
console.log("Enabling pin 2 for button");
board.pinMode(2, board.MODES.INPUT);
});
function handler(req, res) {
fs.readFile(__dirname + "/example07.html",
function (err, data) {
if (err) {
res.writeHead(500, {"Content-Type": "text/plain"});
return res.end("Error loading html page.");
}
res.writeHead(200);
res.end(data);
});
}
http.listen(8080); // server will listen on port 8080 to serve html page
var sendValueViaWebSocket = function(){};
board.on("ready", function() {
wss.on('connection', function (ws) {
ws.send("Server connected.");
sendValueViaWebSocket = function(value) {
ws.send(value);
}
}); // end of wss code
board.digitalRead(2, function(value) {
if (value == 0) {
console.log("LED off");
board.digitalWrite(13, board.LOW);
sendValueViaWebSocket("0");
}
if (value == 1) {
console.log("LED on");
board.digitalWrite(13, board.HIGH);
sendValueViaWebSocket("1");
}
}); // end of board digital read
}); // end of board.on "ready"
<!DOCTYPE html>
<meta charset = utf8>
<html>
<head>
<title>Example with HW button</title>
</head>
<body>
<div id="print1"></div>
<script type="text/javascript">
"use strict"; // in order to use clasess
var divElement = document.getElementById("print1"); // var for div el.
function log(msg) {
divElement.innerHTML += "<div>" + msg + "</div>"; // print to div
}
var ws = new WebSocket('ws://192.168.254.149:8888/');
ws.onmessage = function (event) {
log(event.data);
}
ws.onclose = function (value) {
log("Connection closed.");
}
</script>
</body>
</html>