Node.js + Express + Spacebrew

For our Spatial Media class, we are learning Node.js (http://nodejs.org/) and using it to connect web-based inputs and outputs via Spacebrew (http://docs.spacebrew.cc/). I had a tricky time with it, mainly because I know nothing about servers, javascript, Node.js or the inner-workings of my cat’s mind… but that’s another story.

We were using code examples from from this github:
https://github.com/jefftimesten/SpatialMedia/tree/master/Week_08

Here are a few lessons I learned.

- In layman’s terms, Node.js is a way to use javascript to make efficient servers so you don’t have to run things like Apache. I’m still hazy on the server concept, but this article is a good start: http://en.wikipedia.org/wiki/Nodejs

- To install Node.js, go to http://nodejs.org/

- Node.js applications can be written/edited in any plain text editor (I used TextWrangler).

- The .js applications are compiled/run via Terminal. You must navigate into the appropriate folder and run the “app.js” or “server.js” (those are the common server app names) by entering the following command:
$ node app.js  Note: “$”  should not be typed into the terminal—it’s just a way to indicate something is a Terminal command.

- For the github examples used in our class, you need to install Express — a web application framework for Node.

- Node.js uses npm (Node Packaged Modules: https://npmjs.org/) to install “plugins” or “libraries” — the things that help it run.

- You can install Express locally (into the folder where your code lives so it only works for code in that folder) or globally (so after one installation Express will work for any file on the computer).

- To install locally (via Terminal), navigate into your folder and enter:
$ npm install express

- To install globally, enter: $ npm install -g npm  Note: If you are using a Mac and get errors with the above global install command, try adding “sudo” in front of it: $ sudo npm install -g express

- After installing Express, my node-spacebrew example didn’t run because I was missing a “ws” websocket Spacebrew uses (check out this article about websockets: http://en.wikipedia.org/wiki/WebSocket). To install the “ws” websocket via Terminal, enter $ npm install ws

- Once everything is installed, open Spacebrew by going here:
http://labatrockwell.github.io/spacebrew/admin/admin.html?server=sandbox.spacebrew.cc
I had trouble getting the file to run until Spacebrew was open

- Run the example in Terminal by entering: $ node app.js

- You should see your “publisher” appear in the Spacebrew window. I changed the name of my publisher to “It Works!” by opening the app.js file in TextWrangler and changing:
var name = “It works!”
var description = “Yuliya’s Node test!”;

Screen Shot 2013-04-09 at 7.41.44 AM

P.S. Thank you Andy Sigler for helping figure this out!

Camera: one on one (step 1)

step1_setup

To get a better handle on all the tools we covered in class, I combined TSPS facial tracking, oF, Spacebrew, Processing and Arduino (sounds messier than it actually was).

I combined the TSPS > oF and the oF > Spacebrew code we covered in class into an oF file that receives face recognition information from TSPS and sends a Boolean via Spacebrew. A processing sketch receives the Boolean from Spacebrew and passes the info to Arduino. For a simple proof of concept, the Arduino turned on a Red LED when a face was spotted and a Green LED if “the coast was clear”.

I could have communicated with TSPS via Processing directly, but the oF > Spacebrew > Processing > Arduino connection can be useful in the future, when I wish to connect oF Open CV or other powerful oF tools, with an Arduino.

Notice the LED (on the breadboard in the bottom of the video). The red one turns on when a face is visible to the camera. The green one turns on when the face is blocked.

oF > Spacebrew 1 (local)

I had a few issues connecting to Spacebrew locally because I wasn’t getting the js server to run. When I ran the node node_server.js in Terminal, the connection failed because Terminal couldn’t find the right path to my file. This is what finally worked:

  1. open Terminal
  2. type: node
  3. drag the node_server.js final into the Terminal window after “node”
  4. press enter

This got the server to run. Then I changed the sample button’s testApp.cpp file from string host = Spacebrew::SPACEBREW_CLOUD; to  string host = “localhost”;

string host = Spacebrew::SPACEBREW_CLOUD; could also be written as  string host = “sandbox.spacebrew.cc”;. ”sandbox.spacebrew.cc” is specified in the ofXSpacebrew.h file, in the Spacebrew namespace: static const std::string SPACEBREW_CLOUD = “sandbox.spacebrew.cc”; 

I attempted to create my own I created my own line of code specifying local host connection: static const std::string SPACEBREW_LOCAL = “localhost”; and then changed the string host line in the testApp.cpp file to: string host = Spacebrew::SPACEBREW_LOCAL; As predicted, this allowed me to connect to Spacebrew locally.

Screen Shot 2013-02-15 at 1.29.30 PM

oF > Spacebrew 1 (cloud)

After a few failed attempts, I finally connected the oF “test button” file with Spacebrew on the cloud. During the first few tries, the program would run but nothing would happen in Spacebrew. I shut down and restarted both my browsers and XCode, which eventually got the connection going. I think there was an issue of some duplicate windows and/or the wrong sequence of events.

Screen Shot 2013-02-14 at 4.28.27 PM