Things used in this project

Hardware components:
Pinoccio
×1
TSL2561 Light sensor
×1
TMP102 Temperature sensor
×1

Code

file_7170.jsJavaScript
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.txtC/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);
}

Credits

B042670db133334c3fbeec62cf29cd59
Krishnaraj Varma

Software Engineer from Cochin, Kerala, India

Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

DHT22 Sensor and Pinoccio
  • 1,611
  • 5

Full instructions

Using DHT22 Temperature and Humidity sensor with Pinoccio

Mesh Your Brain
  • 3,459
  • 22

Full instructions

Build your own Internet of Brains with this wireless EEG hack!

Human Presence Detection & Visualization
  • 1,064
  • 9

Full instructions

Team Cogitate's social radar displays people nearby as a set of glowing orbs

Hideous Arc Reactor Sweater (WIP)
  • 811
  • 6

Work in progress

Power through the holiday slump with this festive, EEG-reactive sweater!

Pinoccio Craft
  • 374
  • 5

Full instructions

Send Bitlash Pinoccio Commands through Minecraft.

Solar Powered Pinoccio Scout
  • 1,078
  • 9

Run your wireless mesh forever with Adafruit's solar LiPo charger

Add projectSign up / Login