Introduction: Simple Spark Thermometer
Hi there,
today I had the opportunity to get to play with Spark Core. Since this Dorkbot event is just a few hours and I had a big project in mind, I decided to test just a few parts of it (more will come, rest assured).
So, mainly I wanted to test reading sensor values via JS from the spark core, test the inclusion of libraries for my sensors and the ability to drive a NeoPixel strip.
The idea behind today project is simple: Measure the temperature, write a simple web page which can pull this value, and, for effect, somehow show the temperature somehow locally.
Step 1: The Spark Core: Wired Up
I assume you already brought your core to your wifi, claimed it in your account and are familiar to the IDE.
First, hook up some kind of temperature sensor to your core. I used a BMP180 combined pressure/temperature sensor since I wanted to test it for later. You can use whatever you want, a simple LM35 would do.
Connect the sensor as specified, in my case:
V_in -> V_in
SCL -> D1
SDA -> D0
GND -> GND
Step 2: Show a Temperature
Since I had a strip of 17 NeoPixels left from a previous project, I decided to use those to visualize temperatures from 20 to 37 degrees centrigrade.
Connect the strip to V_in, GND, and the data line to D2. I did it quick 'n dirty, but if you want to do it properly remember AdaFruit recommends a 470 Ohm resistor between core and data line. If you use a longer strip you should consider external power supply.
Fine, all is wired up, now to the code!
Step 3: The Code: Measuring Temperature
The code for measuring is quite simple:
#include "application.h"
#include "Adafruit_BMP085/Adafruit_BMP085.h"
Adafruit_BMP085 bmp;
double tmp;
void setup() {
if (!bmp.begin()) { while (1) {} }
Spark.variable("temperature", &tmp, DOUBLE);
}
void loop() {
tmp = (double) bmp.readTemperature();
delay(100);
}
You should now be able to read the temperature over the web:
For simple testing try accessing:
https://api.spark.io/v1/devices/your core id/temperature?access_token=Your token
You should get a JSON-object like
{ "cmd": "VarReturn", "name": "temperature", "result": 28.918750762939453, "coreInfo": { "last_app": "", "last_heard": "2014-11-19T20:18:09.171Z", "connected": true, "deviceID": "1234567890" } }
Step 4: HTML, JQUERY and JSON
To get the temperature over web, create a new HTML file, insert in the body a span or div element with unique identifier, include jquery and place the following script somewhere in script-tags:
setInterval(function(){
$.getJSON( "https://api.spark.io/v1/devices/ your core id /temperature?access_token=your access token",
function( data ) { $('#tmp').html(Math.round(data.result*100)/100); });
},1000);
In my case, the temperature is now placed every one second in the span with id tmp.
Step 5: Code the Strip
I decided for my strip to switch on one LED for each degree in the specified range. For a even better effect, the LED color should range from blue (the 20°C LED) to red (the 37°C LED) with a soft transition. For this I wrote a function which somehow I can't post here.
See the attached file for the full code:
2 Comments
8 years ago on Introduction
Great... project...
There will be more applications with your project.
8 years ago on Introduction
Nicely done. Thanks for sharing this!