Krishnaraj Varma
Published

Pinoccio and Xively

Pinoccio and Xively integration

Full instructions provided1,215
Pinoccio and Xively

Things used in this project

Hardware components

Pinoccio
×1
TSL2561 Light sensor
×1
TMP102 Temperature sensor
×1

Story

Read more

Code

file_7170.js

JavaScript
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");
				}
			}); 
		}
	}
});

file_7171.txt

C/C++
#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);
}

Github

Credits

Krishnaraj Varma

Krishnaraj Varma

47 projects • 360 followers
Software Engineer from Cochin, Kerala, India

Comments