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!

AEM Resource Mapping and Apache mod_rewrite, Teamwork!

After spend­ing a few days (or weeks!) look­ing for infor­ma­tion relat­ed to hid­ing parts of URLs with Apache Web Serv­er and Adobe Expe­ri­ence Manger, I believe I may have pulled it all togeth­er. This is my attempt at explain­ing how it works.

First, what is going on between Brows­er, Web Serv­er, Cache and AEM?

Out of the box, every­thing works due to the full path being used from the brows­er -> web serv­er -> cache and if a page is updat­ed the Flush­ing agent is able to inval­i­date the prop­er page due to the path being the same in AEM as in the cache.

Now, since I am a devel­op­er and I have access to cre­ate my own resource map­ping, I am going to add a con­fig­u­ra­tion and point a short­ened URL to my land­ing page.

Sling:internalRedirect     string     /content/geometrixx/$1

Sling:match                         string*)$

I save it, repli­cate it to pub­lish and try it out. Click,, sweet it works. Wow that was easy, looks good, all done! Wait a minute, when I update the page it doesn’t seem to show up prop­er­ly to the user, what is going on?

Let’s take a look.

Ok, so my Brows­er -> Web Serv­er -> Cache is all look­ing for, and the map­ping in AEM points it to /content/geometrixx/en.html, so it works from that end, but my Flush­ing agent is inval­i­dat­ing /content/geometrixx/en.html in the cache, which nev­er exists! The cache is putting my page at /en.html! Well that works the first time, but if I need to update the page I will need to clear the cache every time and my Flush­ing Agent will be doing noth­ing for me.

Well scratch that, let me try to do this on the web serv­er. I think I remem­ber some­thing about RewriteRule…

RewriteRule ^/$ /content/geometrixx/en.html [PT]

Ok, now when some­thing comes in, it goes to my land­ing page and in the cor­rect cache fold­er.
Nice! This works, the cache looks good and the Flush­ing agent can inval­i­date the page! So I click on anoth­er link on the page and then the whole path pops up again! I guess this is good enough, who real­ly looks at the URL once they are in the site any­ways. Said no cus­tomer ever….

Well, let’s try putting them togeth­er!
This works on the ini­tial call and all fol­low­ing calls to en.html, but why? Here is the break­down.
First, you will notice the RewriteRule is slight­ly dif­fer­ent.

RewriteRule ^/content/geometrixx/ ^/$1 [PT]

This lets through with no changes because it does not match the fil­ter and the map­ping in AEM points it to /content/geometrixx/en.html. When the Flush­ing Agent fires off, it hits the fil­ter in the web serv­er and is writ­ten to /en.html, so it inval­i­dates the prop­er page. So every­thing seems to work, the only issue is the caching does not reflect the fold­er struc­ture in AEM. If cache struc­ture is not a con­sid­er­a­tion for your project, then this solu­tion could work.

You will notice we only took one page, en.html, into con­sid­er­a­tion, along with only the con­tent fold­er. A final solu­tion for all assets is more com­plex and I am work­ing on a write up for it. Also, I am look­ing into only hav­ing set­ting on the web serv­er so I can keep every­thing in one spot.

Resources 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!

Antique Radio Update — 3

Here is the cur­rent work­ing ver­sion of the antique radio. I hooked togeth­er the Net­duino, Spark­fun MP3 Play­er and all of the but­tons, switch­es and lights and placed them in the case.

It is now up and run­ning in the liv­ing room and seems to be oper­at­ing with no issues. At some point in the future I need to put in a real speak­er, with an amp, and replace the poten­tiome­ter to make a work­ing vol­ume knob. But, as with most projects, once it works it sits until I get around to it!


I believe that is every­thing. My code con­tains some stuff for a vol­ume con­trol, com­ment­ed out, and I am sure there bet­ter ways to do some of it. Any sug­ges­tions are of course wel­come! There are ton of resources online and over­all it was a fun project and I am look­ing for­ward to find­ing anoth­er one soon.


Antique Radio Update — 2

I now have the MP3 play­er hooked up to the Net­duino and all of the switch­es appear to be work­ing. The poten­tiome­ter was shot and the rest of them did not func­tion much bet­ter. I will need to look into get­ting replace­ments.

Luck­i­ly I was able to find some code on the Net­duino forums and the heavy lift­ing of get­ting the Net­duino to com­mu­ni­cate with the VS1053b over SPI was already com­plet­ed. They are a great resource, check them out if you have any ques­tions.

Code from the Net­duino Forum for the VS1053b (Thanks hanz­ibal!) I made some tweaks to a few areas for it to work in my project. I will put every­thing in the next and final (for now) post.

Now to try to put every­thing into the case and make it look bet­ter for the liv­ing room.


Antique Radio Update — 1

One of the pieces of fur­ni­ture my wife brought with her when she moved in was an old radio / phono­graph play­er that had been gut­ted of pret­ty much all of the work­ing parts. All that was left was the switch­es, old tubes and an ancient speak­er. I thought it would be a good project to try and turn it into a stand­alone mp3 play­er using the old but­tons as the inter­face. So after ask­ing for per­mis­sion, prob­a­bly the most impor­tant step, I pro­ceed­ed to fig­ure out how to make this work.

The first point I should make is I know noth­ing about elec­tron­ics, beyond plug­ging them in and look­ing for burnt parts when they do not work any­more. So I knew this was going to be a learn­ing expe­ri­ence. I decid­ed on the Net­duino ( ) as the prod­uct to try and make this work, which uses C# and Visu­al Stu­dio to devel­op in. I also real­ized the Net­duino would need some­thing to decode mp3’s, so I pur­chased a VS1053b shield from Spark­Fun. ( ) In some of the pic­tures or videos you may see a Net­duino Plus ( it has an Eth­er­net port ), I was using both boards to devel­op on and may use the Net­duino Plus in the future if I can fig­ure out how to stream mp3’s to the shield. Both prod­ucts I select­ed have a ton of online sup­port and infor­ma­tion. Luck­i­ly I was able to find infor­ma­tion I was able to under­stand and a lot of peo­ple ask­ing the same ques­tions I had. Also Net­duino and Net­duino Plus have a cou­ple of books you can get with them cov­er­ing all of the basics. They were also very well writ­ten and avail­able on their site.

After tak­ing the con­sole apart I real­ized I had a radio switch with 5 selec­tors, two poten­tiome­ters and a four sec­tion switch. There actu­al­ly may have been more, the radio has two more but­tons, but I think one was to set the sta­tions back in the day and the oth­er was to turn it off (I think). I decid­ed to use the selec­tor to turn the mp3 play­er on when it was switched to phone and the five radio but­tons to switch between gen­res of music. As well as try to use one of the poten­tiome­ters for vol­ume.

My first step was to take the parts out, hook them up and try to get elec­tric­i­ty flow­ing through them to see if they worked. I was sur­prised the radio but­tons worked well, phono­graph switch worked and the poten­tiome­ters seemed to work.

Next to work this all togeth­er with the mp3 play­er shield. I will post some code when I am fin­ished. It is all rather ugly right now.

Nexus 7 and WordPress

Just installed Word­Press for Android on my new Nexus 7 tablet, and I have to say, it works great! Doing this post from the couch using WiFi and this inter­face is very well built.

One note, you need to enable Remote Pub­lish­ing on your site for it to work. Just go to Set­tings > Writ­ing, look for Remote Pub­lish­ing on the page and check XML-RPC. Remem­ber to  save set­ting and you should be all set.

Now if only Adobe would get their Touch Apps work­ing on a 7in tablet!

Check out their apps and maybe let them know you think the Nexus 7 would be a great addi­tion to their sup­port­ed plat­forms!