-
Notifications
You must be signed in to change notification settings - Fork 6
Node JS
Hey gang. So last week we checked out NodeJS, an application framework that you drive with (server-side) Javascript. This lets you do things like create custom web servers, connect to serial devices, and provide other interesting hooks between browser-based interfaces (e.g. p5.js) and the computer.
Here's how to get setup with NodeJS so that it will (a) serve webpages and (b) connect to a serial port for Arduino.
To install NodeJS and Arduino to get all of this running:
ON MAC:
- Download and install NodeJS (latest version).
- Download and install Arduino (latest version).
- Open up a Terminal, and 'cd' into the folder where your node server file will live. To run the examples from Class12, 'cd' into that folder. Remember you can type 'cd' followed by a space in the Terminal and then drag the folder icon over from the Finder and it will fill in.
- Using the Terminal, install some node packages using npm, the NodeJS package manager:
- npm install mime
- npm install socket.io
- npm install serialport
- run your Node script to test things out.
ON WINDOWS:
On Windows, we need to use a slightly older version of NodeJS to get the serial driver working. Hopefully this will be fixed soon. You will also need Python 2 to compile NodeJS packages... Python comes pre-installed on the Mac; you may have to install it on Windows.
- Download and install NodeJS version 0.12.7.
- Download and install Python version 2.17.10.
- NodeJS will install a custom command prompt called the 'node.js commmand prompt'. Run it and you will see a terminal interface.
- In the NodeJS command prompt, 'cd' into the folder where your node server file will live. To run the examples from Class12, 'cd' into that folder. Remember you can type 'cd' followed by a space in the command prompt and then drag the folder icon over from the Windows Explorer and it will fill in.
- Using the command prompt, install some node packages using npm, the NodeJS package manager:
- npm install mime
- npm install socket.io
- npm install serialport@1.7.4
The stuff you need is in the Class 12 repository. The main file you're gonna work with is called lukeserver.js. It will serve up websites from the folder where it's run, as well as folders below it in the file system hierarchy. It will also connect to a serial interface (e.g. an Arduino) and dump data from that interface over a web socket to any clients (i.e. web browsers) that are listening.
You don't run it directly. Instead, you link it into a NodeJS server file that you write by using 'require' and binding it to a variable. For example, to create a simple web server, you'd create a Javascript file in the same folder as the 'lukeserver.js' file and write:
var foo = require('./lukeserver.js');
foo.startServer(8080);
foo.debugServer(true);
If you save that file as, e.g. 'testing.js', you would then open up a Terminal (Mac) or node.js command prompt (Windows), 'cd' into that folder, and type:
node testing.js
You will see a friendly hello message:
lukeserver v.0.1!!!!
it slices. it dices.
If you then open up Chrome, and point it to http://localhost:8080, you will be able to serve up web pages from that folder and subfolders within.
If you want to work with serial data (from an Arduino), you would change your NodeJS server to start a serial port. The name of the serial port is what you find under the Tools menu in the Arduino software. On a Mac, it's typically something like '/dev/cu.usbmodem1411'. On Windows, it's typically 'COM3' or 'COM4'. So to run a server that also serves up Arduino data, you'd modify your Javascript to say:
ON MAC:
var foo = require('./lukeserver.js');
foo.startServer(8080);
foo.startSerial('/dev/cu.usbmodem1411');
foo.debugServer(true);
foo.debugSerial(true);
on PC:
var foo = require('./lukeserver.js');
foo.startServer(8080);
foo.startSerial('COM3');
foo.debugServer(true);
foo.debugSerial(true);
This will connect to an Arduino and start streaming data via web socket to any browsers connected on port 8080. If you look at the Class12 examples, you'll see how this can work.