Hardware components | ||||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 |
Here is a project I worked last weekend, integrating Pinoccio and Xively. The application uses Pinnoccio, TSL2561 Light Sensor and TMP102 Temperature Sensor. There are two modules, first one is the Pinoccio application that retrieve the Light and Temperature sensor values and post to Pinoccio HQ. The second module is the Node.js module to subscribe to the live Pinoccio streams and post the sensor data to Xively. To connect and send data to Xively, I am using this library. You can log on to Xively to visualize the sensor values.
Warning: embedding parts within the project story has been deprecated. To edit, remove or add more parts, go to the "Hardware" tab. To remove this list from the story, click on it to trigger the context menu, then click the trash can button (this won't delete it from the "Hardware" tab). | ||||||
|
||||||
|
||||||
|
Untitled file
Warning: Embedding code files within the project story has been deprecated. To edit this file or add more files, go to the "Software" tab. To remove this file from the story, click on it to trigger the context menu, then click the trash can button (this won't delete it from the "Software" tab).
var pinoccio = require('pinoccio'); // works in browserify and server side
var xively = require("xively");
// create xively client
var xivelyclient = new xively();
// create an api instance with default options
var api = pinoccio("<<pinoccio_key>>");// in the browser the token may be read from a pinoccio_id cookie,
var stream = api.sync();
xivelyclient.setKey("<<xively_api_key>>");
stream.on("data", function(data){
var streamData = data.data;
// check for our troop and scout
if(streamData.troop == 18 && streamData.scout == 1){
// check our stream
if(streamData.value.type == 'env'){
console.log("Light level is " + streamData.value.l + " lx" + ", Temperature is " + streamData.value.t + " °C");
// Datastreams
var dp = {
"version":"1.0.0",
"datastreams" : [
{
"id" : "temperature",
"current_value" : streamData.value.t
},
{
"id" : "light",
"current_value" : streamData.value.l
},
]
}
// Send data to Xively
xivelyclient.feed.new('<<feed_id>>', {
data_point: dp,
callback: function(e) {
console.log("Data posted to Xively");
}
});
}
}
});
Untitled file
Warning: Embedding code files within the project story has been deprecated. To edit this file or add more files, go to the "Software" tab. To remove this file from the story, click on it to trigger the context menu, then click the trash can button (this won't delete it from the "Software" tab).
#include <SPI.h>
#include <Wire.h>
#include <Scout.h>
#include <GS.h>
#include <bitlash.h>
#include <lwm.h>
#include <js0n.h>
#include <key/key.h>
#include <SFE_TSL2561.h>
#define SKETCH_NAME "Custom"
#define SKETCH_BUILD -1
#define SKETCH_REVISION "unknown"
// Create an SFE_TSL2561 object, here called "light":
SFE_TSL2561 light;
// Global variables:
int tmp102Address = 0x48;
boolean gain = 0;
unsigned int ms;
unsigned char time = 2;
double lux = 0.0;
float temperature = 0.0;
int timedelay = 10000;
long prevtime = 0;
void setup(){
// Setup
Serial.begin(115200);
Scout.setup(SKETCH_NAME, SKETCH_REVISION, SKETCH_BUILD);
// Setup TSL2561
light.begin();
light.setTiming(gain,time,ms);
light.setPowerUp();
// Add Bitlash functions
addBitlashFunction("env.light", (bitlash_function)getlight);
addBitlashFunction("env.temperature", (bitlash_function)gettemperature);
addBitlashFunction("envmon.report", (bitlash_function)reportenvdata);
}
void loop(){
Scout.loop();
// Check time interval
if((millis() - prevtime) >= timedelay){
// Get values
lux = getLux();
temperature = getTemperature();
// Debug messages
Serial.print("Light Value: ");
Serial.print(lux);
Serial.print(" lx");
Serial.print(" Temperature: ");
Serial.print(temperature);
// Report values to the HQ
reportHQ();
// Update previous time
prevtime = millis();
}
}
// Returns LUX
double getLux(){
unsigned int data0, data1;
light.getData(data0,data1);
light.getLux(gain,ms,data0,data1,lux);
return lux;
}
// Return temperature
float getTemperature(){
Wire.requestFrom(tmp102Address,2);
byte MSB = Wire.read();
byte LSB = Wire.read();
//it's a 12bit int, using two's compliment for negative
int TemperatureSum = ((MSB << 8) | LSB) >> 4;
float celsius = TemperatureSum*0.0625;
return celsius;
}
// Bitlash function to retrieve light value
numvar getlight(void){
return (numvar)lux;
}
// Bitlash function to retrieve temperature
numvar gettemperature(void){
return (numvar)temperature;
}
// Bitlash function to retrieve env data
numvar reportenvdata(void){
reportHQ();
return (numvar)1;
}
// Send data to HQ
void reportHQ(void) {
StringBuffer report(100);
report.appendSprintf("[%d,[%d, %d],[%d, %d]]",
keyMap("env", 0),
keyMap("l", 0),
keyMap("t", 0),
(int)lux,
(int)temperature);
Scout.handler.report(report);
}
var pinoccio = require('pinoccio'); // works in browserify and server side
var xively = require("xively");
// create xively client
var xivelyclient = new xively();
// create an api instance with default options
var api = pinoccio("<<pinoccio_key>>");// in the browser the token may be read from a pinoccio_id cookie,
var stream = api.sync();
xivelyclient.setKey("<<xively_api_key>>");
stream.on("data", function(data){
var streamData = data.data;
// check for our troop and scout
if(streamData.troop == 18 && streamData.scout == 1){
// check our stream
if(streamData.value.type == 'env'){
console.log("Light level is " + streamData.value.l + " lx" + ", Temperature is " + streamData.value.t + " C");
// Datastreams
var dp = {
"version":"1.0.0",
"datastreams" : [
{
"id" : "temperature",
"current_value" : streamData.value.t
},
{
"id" : "light",
"current_value" : streamData.value.l
},
]
}
// Send data to Xively
xivelyclient.feed.new('<<feed_id>>', {
data_point: dp,
callback: function(e) {
console.log("Data posted to Xively");
}
});
}
}
});
#include <SPI.h>
#include <Wire.h>
#include <Scout.h>
#include <GS.h>
#include <bitlash.h>
#include <lwm.h>
#include <js0n.h>
#include <key/key.h>
#include <SFE_TSL2561.h>
#define SKETCH_NAME "Custom"
#define SKETCH_BUILD -1
#define SKETCH_REVISION "unknown"
// Create an SFE_TSL2561 object, here called "light":
SFE_TSL2561 light;
// Global variables:
int tmp102Address = 0x48;
boolean gain = 0;
unsigned int ms;
unsigned char time = 2;
double lux = 0.0;
float temperature = 0.0;
int timedelay = 10000;
long prevtime = 0;
void setup(){
// Setup
Serial.begin(115200);
Scout.setup(SKETCH_NAME, SKETCH_REVISION, SKETCH_BUILD);
// Setup TSL2561
light.begin();
light.setTiming(gain,time,ms);
light.setPowerUp();
// Add Bitlash functions
addBitlashFunction("env.light", (bitlash_function)getlight);
addBitlashFunction("env.temperature", (bitlash_function)gettemperature);
addBitlashFunction("envmon.report", (bitlash_function)reportenvdata);
}
void loop(){
Scout.loop();
// Check time interval
if((millis() - prevtime) >= timedelay){
// Get values
lux = getLux();
temperature = getTemperature();
// Debug messages
Serial.print("Light Value: ");
Serial.print(lux);
Serial.print(" lx");
Serial.print(" Temperature: ");
Serial.print(temperature);
// Report values to the HQ
reportHQ();
// Update previous time
prevtime = millis();
}
}
// Returns LUX
double getLux(){
unsigned int data0, data1;
light.getData(data0,data1);
light.getLux(gain,ms,data0,data1,lux);
return lux;
}
// Return temperature
float getTemperature(){
Wire.requestFrom(tmp102Address,2);
byte MSB = Wire.read();
byte LSB = Wire.read();
//it's a 12bit int, using two's compliment for negative
int TemperatureSum = ((MSB << 8) | LSB) >> 4;
float celsius = TemperatureSum*0.0625;
return celsius;
}
// Bitlash function to retrieve light value
numvar getlight(void){
return (numvar)lux;
}
// Bitlash function to retrieve temperature
numvar gettemperature(void){
return (numvar)temperature;
}
// Bitlash function to retrieve env data
numvar reportenvdata(void){
reportHQ();
return (numvar)1;
}
// Send data to HQ
void reportHQ(void) {
StringBuffer report(100);
report.appendSprintf("[%d,[%d, %d],[%d, %d]]",
keyMap("env", 0),
keyMap("l", 0),
keyMap("t", 0),
(int)lux,
(int)temperature);
Scout.handler.report(report);
}
Comments