Hardware Reference
In-Depth Information
Solution
This solution builds on the Node.js-based web server solution in
Recipe 6.3
.
To make this recipe, you will need:
▪ Breadboard and jumper wires (see
“Prototyping Equipment”
)
▪ Pushbutton switch (see
“Miscellaneous”
)
Wire your pushbutton as shown in
Figure 2-5
. Add the code in
Example 6-3
to a file called
GPIOserver.js
in the
serverExample
directory.
Example 6-3. A simple Node.js-based web server to read a GPIO (GPIOserver.js)
#!/usr/bin/env node
// Initial idea from Getting Started With node.js and socket.io
// http://codehenge.net/blog/2011/12/
getting-started-with-node-js-and-socket-io-
// v0-7-part-2/
// This is a simple server for the various web frontends
// Display status of P9_42
"use strict"
;
var
port
=
9090
,
// Port on which to listen
http
=
require
(
'http'
),
url
=
require
(
'url'
),
fs
=
require
(
'fs'
),
b
=
require
(
'bonescript'
),
gpio
=
'P9_42'
;
// gpio port to read
var
htmlStart
=
"\
<!DOCTYPE html>\
<html>\
<body>\
\
<h1>"
+
gpio
+
"</h1>\
data = "
;
var
htmlEnd
=
"\
</body>\
</html>"
;