As you could tell from the previous few posts, I have been working with OpenShift, Node.js (among other .js) and Spark.io. I have created what I hope could be an easy starting point for those hoping to do the same.
- Get an OpenShift Account - https://openshift.redhat.com
- Create a new application using “Code Anything” (Bottom of the new application page) with - http://cdk-hippreality.rhcloud.com/manifest/master
- This cartridge will provide the assets and some code needed to interact with you spark.io devies.
Please refer to my previous post for detail related to the above steps. Now for some code details.
The code for everything you see on your page is in views/index.jade in your cartridge, which you can use git to down load and edit. When you look at it, you will see how it interacts 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 temperature data, start with setting up your spark.io similiar to this: http://docs.spark.io/examples/#measuring-the-temperature (I used a more simple resistance type sensor, from the maker kit, so you will see some code different to convert them temperature).
On your spark,io you can add the following code to have the buttons and such to work from your web page. Remember 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);
// 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
// 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")
if(command == "off")
else return -1;
Hopefully this helps someone out!