Node / Spark Easy Start

As you could tell from the pre­vi­ous few posts, I have been work­ing with Open­Shift, Node.js (among oth­er .js) and I have cre­at­ed what I hope could be an easy start­ing point for those hop­ing to do the same.

  1. Get an Open­Shift Account —
  2. Cre­ate a new appli­ca­tion using “Code Any­thing” (Bot­tom of the new appli­ca­tion page) with —
  3. This car­tridge will pro­vide the assets and some code need­ed to inter­act with you devies.

Please refer to my pre­vi­ous post for detail relat­ed to the above steps. Now for some code details.

The code for every­thing you see on your page is in views/index.jade in your car­tridge, which you can use git to down load and edit. When you look at it, you will see how it inter­acts with the code below.

To get the most out of this first page, it is good to have some code on your to work with. Here is the basics.

If you want to get some tem­per­a­ture data, start with set­ting up your sim­il­iar to this: (I used a more sim­ple resis­tance type sen­sor, from the mak­er kit, so you will see some code dif­fer­ent to con­vert them tem­per­a­ture).

On your spark,io you can add the fol­low­ing code to have the but­tons and such to work from your web page. Remem­ber to include math.h, string and application.h if you do not have them includ­ed already.

// -----------------
// Read temperature
// -----------------
// Create a variable that will store the temperature value
double reading = 0;
//double adc = 0;
double resistance = 0;
double temperature = 0;
double steinhart = 0;
int led = D7;
int blueLight(String command);
void setup()
Spark.function("light", blueLight);
// Register a Spark variable here
//Spark.variable("reading", &reading, DOUBLE);
//Spark.variable("adc", &adc, DOUBLE);
//Spark.variable("resistance", &resistance, DOUBLE);
Spark.variable("temperature", &temperature, DOUBLE);
//Spark.variable("steinhart", &steinhart, DOUBLE);
// Connect the temperature sensor to A1 and configure it
// to be an input
pinMode(A7, INPUT);
pinMode(D7, OUTPUT);
void loop()
// Keep reading the temperature so when we make an API
// call to read its value, we have the latest one
reading = analogRead(A7);
//adc = reading / (reading + 35000) * 4095;
resistance = 35000 / ( 4095 / reading - 1);
steinhart = resistance / 100000;
steinhart = log10(steinhart);
steinhart = steinhart / 4300;
steinhart = steinhart + (1.0 / (25 + 273.15));
steinhart = 1.0 / steinhart;
steinhart = steinhart - 273.15;
temperature = steinhart * 9 / 5 + 32;
int blueLight(String command)
if(command == "on")
digitalWrite(led, HIGH);
return 1;
if(command == "off")
digitalWrite(led, LOW);
return 1;
else return -1;

Hope­ful­ly this helps some­one out!

OpenShift Cartridge to get you started with Spark.js

I have spent a bit of time fig­ur­ing out how to get spark.js, node.js and Open­Shift all work­ing togeth­er and if you have read the last two post you may have seen some of the progress. Recent­ly I have cre­at­ed a car­tridge for Open­Shift, com­bin­ing every­thing into a rel­a­tive­ly easy to use pack­age. I hope it can save some­one some time and get their devices up and report­ing on the web faster!

Syn­op­sis (If you are in a hur­ry):

  1. Cre­ate Open­Shift Account:
  2. Add a new appli­ca­tion and paste into Code Any­thing input field.
  3. After com­plet­ing the forms and build­ing the appli­ca­tion, click through the appli­ca­tion, log in to Spark and see your devices and details list­ed in the web­site.

First, there are a ton of resources I need to thank for putting this togeth­er. Open­Shift and the car­tridge devel­op­ment kit (CDK) were a great start­ing point. The spark.js repo was an awe­some source of infor­ma­tion, along with the jade, node.js, express and the sites. Now, the good stuff!

First, if you do not have an Open­Shift account, you can cre­ate one for free here: . Sign up, wait for an email and log in.

Once you are in, you can cre­ate a new appli­ca­tion by click­ing on the but­ton and you will see all of the great car­tridges already avail­able. How­ev­er, instead of select­ing one of them, scroll down to the “Code Any­thing” area and paste in this link to the cus­tom car­tridge:

Once you hit next, it will ask you for a domain name and the name of the appli­ca­tion you are cre­at­ing. Once you have filled those in, you can scroll down to “Cre­ate Appli­ca­tion” and when you hit the but­ton it will build the site for you.

After the install com­pletes you will be able to see a list of your appli­ca­tions and when you click on the one you just cre­at­ed it will pro­vide you a link to the name of the appli­ca­tion and domain you select­ed ear­li­er. If you click on the link, you will see all of the parts from the pre­vi­ous posts run­ning in a sim­ple page. Just two lines of text and a but­ton.

Click on “Login to Spark” and put in your cre­den­tials and you will be sent to a page giv­ing you infor­ma­tion about your devices! This is just a start­ing point, using the frame­work you can cre­ate your own pages, but­tons and inter­ac­tions with your devices, which will be avail­able through this site!

If you want to start cus­tomiz­ing the page, use the link pro­vid­ed in your appli­ca­tion to clone your git local­ly and start build­ing your web appli­ca­tion! For sim­ple changes, to make sure every­thing is work­ing, you can edit routes/index.js title to reflect your own page infor­ma­tion. From here, your imag­i­na­tion is the only limitation…that and time!


For more infor­ma­tion relat­ed to Open­Shift and Node.js, refer to my last two posts. Good Luck!

OpenShift, express.js and spark.js — A bit farther

After work­ing with git, Open­Shift, node.js and all of the oth­er stuff from my last post to final­ly have a sim­ple “Hel­lo World”, I took the next step of actu­al­ly putting more than just a page with hel­lo world in my new appli­ca­tion.

  1. First I decid­ed to go with express.js as my start­ing point. I had already installed express.js to use, but they also have an express gen­er­a­tor that builds out a stan­dard appli­ca­tion with fold­er struc­ture and oth­er sup­port­ing items. You can find the details and install stuff here:
    1. If you installed express glob­al­ly, you might also need to install it in your app node_modules also after your run the gen­er­a­tor. If you try npm install after the gen­er­a­tor and you get a ton of errors cd into myapp/node_modules and npm install express into this loca­tion. Good info here:–0-global-vs-local-installation .
  2. Now that I have a skele­ton, I need­ed to put it in my git repos­i­to­ry, not in its appli­ca­tion fold­er name like it was cre­at­ed. This ensures it goes into the cor­rect loca­tion when I com­mit to Open­Shift. Make sure you leave the .git and .open­shift in your repos­i­to­ry!
  3. Since I now have a new start­ing loca­tion, it was server.js as the Open­Shift stan­dard; I need­ed to update my package.json so the app starts where I need it to.


"name": "spark",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
"main": "./bin/www",

"dependencies": {
"express": "~4.9.0",
"body-parser": "~1.8.1",
"cookie-parser": "~1.3.3",
"morgan": "~1.3.0",
"serve-favicon": "~2.1.3",
"debug": "~2.0.0",
"jade": "~1.6.0"

4.  Also, the stan­dard www file in the express build needs to be updat­ed so it pulls in the envi­ron­men­tal vari­ables from Open­Shift.


#!/usr/bin/env node
var debug = require('debug')('spark');
var app = require('../app');
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 8080);
app.set('host', process.env.OPENSHIFT_NODEJS_IP || "");
var server = app.listen(app.get('port'), app.get('host'), function() {
debug('Express server listening on port ' + server.address().port);

The last two steps are well doc­u­ment­ed here:

Once you com­mit every­thing you should now see your com­plet­ed Wel­come to Express page and now you can start build­ing out your app!

This will be my start­ing point for inter­act­ing with my Spark.IO devices and work­ing with spark.js. Hope­ful­ly I will have some work­ing exam­ples soon!

Email about spark.js and now my Saturday is gone!

A lit­tle while back I saw an email about spark.js and thought it would be cool to check it out. This Sat­ur­day morn­ing I fig­ured I would down­load it and play around a lit­tle while it stopped rain­ing out­side. 6 hours lat­er…

First, I must say I am not a node.js expert, but I have some expe­ri­ence with Ver­sion Con­trol Sys­tems (VCS, Sub­ver­sion as an exam­ple), I do know JavaScript and I have tin­kered around with var­i­ous lan­guages, hard­ware etc. All that being said, it took a bit for me to get a work­ing envi­ron­ment to play around with spark.js the way I want­ed to!

Here were the steps I took and snags I found. This is all on Win­dows 7

  1. Install node.js
  2. Install spark.js

From here you can try out some stuff and get respons­es in the con­sole, you can actu­al­ly stop here, but I thought I would take it a few steps fur­ther to cre­ate a “true” devel­op­ment envi­ron­ment.

  1. Cre­ate a free account on Open­Shift
    1. If you have not seen this, it is pret­ty cool. You can get 3 small gears for free and they can host your node.js appli­ca­tion (among oth­er things!)
  2. When you go through the process on the above page, you will need to install Git ( which is super easy to use and have up and run­ning.
  3. You will also need Ruby to use the rhc gem to inter­act with your Open­Shift appli­ca­tion through com­mand line. I used Ruby 1.9.3-p545 and it worked with only one issue. If you check the box to add Ruby to your PATH, it does not work.
    1. Try >echo %PATH% to check, from com­mand line
    2. If it is not there you can try >set PATH=C:\RUBY193\bin:%PATH% to add it. Make sure you use your actu­al path.
  4. Install the rhc gem and set it up:
  5. Install express and save your­self some time:
  6. You can con­nect your git to your Open­Shift App to make it sim­ple to man­age your code, just make a key and drop it into the Open­Shift Inter­face.

Now, if every­thing is set­up and con­nect­ed prop­er­ly, you can actu­al­ly tail your appli­ca­tion from your con­sole with rhc, add code to your local git and push it to your app and view your appli­ca­tion through your own URL. Mine as an exam­ple: .


var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080
var host = process.env.OPENSHIFT_NODEJS_IP || "";
var server = app.listen(port, host, function () {
console.log('Example app listening at http://%s:%s', host, port);

After you com­mit your code, the appli­ca­tion auto­mat­i­cal­ly restarts and you should see your Hel­lo World.

All this and I nev­er even got to the spark.js (beyond return vars in the con­sole). That will be com­ing next! Refridgerator Monitor

A few weeks ago my wife noticed our refrig­er­a­tor tem­per­a­ture was start­ing to go up and down pret­ty dra­mat­i­cal­ly and she was wor­ried about the impact it would have on our food, and her shop­ping bud­get if we had to throw a bunch of stuff out. Luck­i­ly I had a spark device and sen­sors on hand, so I threw togeth­er some hard­ware and code to mea­sure the tem­per­a­ture in the refrig­er­a­tor and the amount of time the door was open.

First, the hard­ware: Spark Mak­er Kit This con­tains every­thing I used in this project. A spark device, the wires, bread­boards, capac­i­tor, temp sen­sor and light sen­sor.

A pic­ture is worth thou­sands of my hor­ri­ble inef­fi­cient words.
Great instruc­tions on the set up can be found here: Mea­sur­ing the Tem­per­a­ture

I then added the light sen­sor from the kit on A5, using the fil­tered pow­er. After test­ing and such I extend­ed the wires as you will see in the fin­ished prod­uct.

Here is the code for the spark core:

char resultstr[64];
int lightsec;
void setup()
pinMode(A7, INPUT); // A7 Analog Input is temperature
pinMode(A5, INPUT); // A5 Analog Input is light
lightsec = 0; // Variable to keep track of how long light is on
Spark.variable("result", &resultstr, STRING);
void loop()
int temp = analogRead(A7); // Read temp data
int light = analogRead(A5); // Read light data
if (light > 2200) { lightsec++; } // If light is on add a second to count
// Put the results out for Google Script to grab
sprintf(resultstr, "{\"temp\":%d,\"lightsec\":%d}", temp, lightsec);
delay(1000); // wait for a second

And here is the code for the Google Script run­ning in the Google Sheet. An exam­ple and instruc­tions can be found here: Log­ging and Graph­ing Data

function collectData() {
var sheet = SpreadsheetApp.getActiveSheet();
var response = UrlFetchApp.fetch("[DEVICE_NAME]/result? access_token=[TOKEN]");
try {
var response = JSON.parse(response.getContentText()); // parse the JSON the Core API created
var result = unescape(response.result); // you'll need to unescape before your parse as JSON
try {
var p = JSON.parse(result); // parse the JSON you created
var d = new Date(); // time stamp
sheet.appendRow([d, p.temp, ((((p.temp*3.3)/4095)-0.5)*100)*1.8+32,p.lightsec]); // Add the line and convert the temp to F
} catch(e)
Logger.log("Unable to do second parse");
} catch(e)
Logger.log("Unable to returned JSON");

Final­ly I put the whole con­trap­tion into the refrig­er­a­tor and start­ed mea­sur­ing.

I let it run for about 36 hours, mea­sur­ing every 10 min­utes.


Here is the data, and it appears the wife might be cor­rect and a 10–15 degree swing is to much for our refrig­er­a­tor.

It also seems we do not go into the fridge much. About 4 mins over 36 hours.

I guess it is time to call the repair man!