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!

Leave a Reply