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 other .js) and I have cre­ated 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 needed to inter­act with you devies.

Please refer to my pre­vi­ous post for detail related 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 maker kit, so you will see some code dif­fer­ent to con­vert them temperature).

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 included 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­fully 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 together and if you have read the last two post you may have seen some of the progress. Recently I have cre­ated a car­tridge for Open­Shift, com­bin­ing every­thing into a rel­a­tively 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 hurry):

  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 listed in the website.

First, there are a ton of resources I need to thank for putting this together. 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­ever, 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­ated it will pro­vide you a link to the name of the appli­ca­tion and domain you selected ear­lier. 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 button.

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­vided in your appli­ca­tion to clone your git locally 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 related 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 other stuff from my last post to finally have a sim­ple “Hello World”, I took the next step of actu­ally putting more than just a page with hello world in my new application.

  1. First I decided 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 folder struc­ture and other sup­port­ing items. You can find the details and install stuff here:
    1. If you installed express glob­ally, 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 needed to put it in my git repos­i­tory, not in its appli­ca­tion folder name like it was cre­ated. 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 repository!
  3. Since I now have a new start­ing loca­tion, it was server.js as the Open­Shift stan­dard; I needed 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 updated so it pulls in the envi­ron­men­tal vari­ables from OpenShift.


#!/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­mented here:

Once you com­mit every­thing you should now see your com­pleted 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­fully 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 later…

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 wanted 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 responses in the con­sole, you can actu­ally stop here, but I thought I would take it a few steps fur­ther to cre­ate a “true” devel­op­ment environment.

  1. Cre­ate a free account on Open­Shift
    1. If you have not seen this, it is pretty cool. You can get 3 small gears for free and they can host your node.js appli­ca­tion (among other 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 running.
  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 actual 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 Interface.

Now, if every­thing is setup and con­nected prop­erly, you can actu­ally 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­cally restarts and you should see your Hello World.

All this and I never 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 related to hid­ing parts of URLs with Apache Web Server and Adobe Expe­ri­ence Manger, I believe I may have pulled it all together. This is my attempt at explain­ing how it works.

First, what is going on between Browser, Web Server, Cache and AEM?

Out of the box, every­thing works due to the full path being used from the browser -> web server -> cache and if a page is updated the Flush­ing agent is able to inval­i­date the proper page due to the path being the same in AEM as in the cache.

Now, since I am a devel­oper 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­erly to the user, what is going on?

Let’s take a look.

Ok, so my Browser -> Web Server -> 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 never 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 server. 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 folder.
Nice! This works, the cache looks good and the Flush­ing agent can inval­i­date the page! So I click on another link on the page and then the whole path pops up again! I guess this is good enough, who really looks at the URL once they are in the site any­ways. Said no cus­tomer ever….

Well, let’s try putting them together!
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 slightly different.

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 server and is writ­ten to /en.html, so it inval­i­dates the proper page. So every­thing seems to work, the only issue is the caching does not reflect the folder 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 folder. 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 server 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 pretty dra­mat­i­cally 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­ily I had a spark device and sen­sors on hand, so I threw together 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 Maker 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 sensor.

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 Temperature

I then added the light sen­sor from the kit on A5, using the fil­tered power. After test­ing and such I extended the wires as you will see in the fin­ished product.

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");

Finally I put the whole con­trap­tion into the refrig­er­a­tor and started measuring.

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


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

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 together the Net­duino, Spark­fun MP3 Player and all of the but­tons, switches 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 speaker, 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­mented 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 another one soon.


Raspberry Pi, are you there?

On my last order from Spark­Fun I threw a Rasp­berry Pi into the cart to get free ship­ping just finally had a chance to get it up and run­ning. The whole process was super easy, just used the info found on their site. ( )

I have not actu­ally tried any­thing beyond boot­ing and look­ing around, but the first thing I noticed was silence! I am used to work­ing on desk­tops or lap­tops with scream­ing fans, spin­ning hard dri­ves  and assorted click­ing and clank­ing. When I turned of the machine I used to pre­pare the SD card for the Pi, there was com­plete silence and I noticed it almost imme­di­ately. I think I like it, but now all I can hear is my keyboard…

Antique Radio Update — 2

I now have the MP3 player hooked up to the Net­duino and all of the switches 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 replacements.

Luck­ily 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­pleted. They are a great resource, check them out if you have any questions.

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 player that had been gut­ted of pretty much all of the work­ing parts. All that was left was the switches, old tubes and an ancient speaker. I thought it would be a good project to try and turn it into a stand­alone mp3 player 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­ceeded 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 decided on the Net­duino ( ) as the prod­uct to try and make this work, which uses C# and Visual Stu­dio to develop 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 develop 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 selected have a ton of online sup­port and infor­ma­tion. Luck­ily 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­ally 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 other was to turn it off (I think). I decided to use the selec­tor to turn the mp3 player 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 volume.

My first step was to take the parts out, hook them up and try to get elec­tric­ity 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 together with the mp3 player shield. I will post some code when I am fin­ished. It is all rather ugly right now.