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!