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 Spark.io. 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 — https://openshift.redhat.com
  2. Cre­ate a new appli­ca­tion using “Code Any­thing” (Bot­tom of the new appli­ca­tion page) with — http://cdk-hippreality.rhcloud.com/manifest/master
  3. This car­tridge will pro­vide the assets and some code need­ed to inter­act with you spark.io 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 spark.io to work with. Here is the basics.

If you want to get some tem­per­a­ture data, start with set­ting up your spark.io sim­il­iar to this: http://docs.spark.io/examples/#measuring-the-temperature (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, 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: http://expressjs.com/starter/generator.html
    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: http://blog.nodejs.org/2011/03/23/npm-1–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.

package.json

"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.

./bin/www

#!/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 || "127.0.0.1");
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: https://blog.openshift.com/run-your-nodejs-projects-on-openshift-in-two-simple-steps

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!