Hackster is hosting Hackster Holidays, Ep. 5: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 5 on Friday!
truthkos
Published © CC BY

Ambient Light Sensor for Philips Hue

Let the sun turn your Hue lights on automatically.

IntermediateFull instructions provided7,163

Things used in this project

Hardware components

Philips hue
Philips hue
Requires the basic kit (Hue Bridge and 1 Bulb)
×1
Adafruit SI1145 UV/IR/Visible Light Sensor
Collects visible light readings measured in Lux.
×1

Software apps and online services

AWS IoT
Amazon Web Services AWS IoT
Reads in the light sensor data from Edison, and only passes data along to SNS if the light reading is above/below a specified level.
AWS Lambda
Amazon Web Services AWS Lambda
Reads in light sensor data from SNS and sends a command message over a topic to the IFTT Maker Channel.
AWS SNS
Amazon Web Services AWS SNS
Passes along the light sensor data from IoT to Lambda.
AWS IAM
Amazon Web Services AWS IAM
Manages the permissions needed to send light sensor data between AWS services.
Maker service
IFTTT Maker service
Receives commands from AWS and tells Hue to turn on/off light.

Story

Read more

Schematics

Edison + SI1145

Code

AWS Lambda Code

JavaScript
This is the code that tells AWS Lambda to send a message to your IFTT event name over the Maker channel.
TO DO:
1. Cut and paste this code under the "Code" tab of an AWS Lambda function.
2. Replace the "iftttMakerSecretKey" value you generated when you set up the IFTT Maker channel in your account.
3. Replace the "iftttMakerEventName" value with the event name you created when setting up the Hue recipe on the IFTT website.
console.log('Loading function');

var https = require('https');
var querystring = require("querystring");

// IFTTT Maker Configuration, see https://ifttt.com/maker for more info
var iftttMakerEventName = 'YOUR_IFTTT_RECIPE_EVENT_NAME' // You can use a different IFTTT Maker EventName here
var iftttMakerSecretKey = 'YOUR_IFTTT_USER_KEY';

var iftttMakerUrl =
    'https://maker.ifttt.com/trigger/'
    + iftttMakerEventName
    + '/with/key/'
    + iftttMakerSecretKey;

exports.handler = function(event, context) {
    var message = event.Records[0].Sns.Message;
    console.log('From SNS:', message);

    // The output to send, you can optionally filter/edit the original SNS message here
    var output = message;
    console.log('Output: ', output);

    // The output is send as 'value1' to IFTTT Maker 
    var params = querystring.stringify({value1: output});

    https.get(encodeURI(iftttMakerUrl) + '?' + params, function(res) {
        console.log("Got response: " + res.statusCode);
        res.setEncoding('utf8');
        res.on('data', function(d) {
            console.log('Body: ' + d);
        });
        context.succeed(res.statusCode);
    }).on('error', function(e) {
        console.log("Got error: " + e.message);
        context.fail(e.message);
    });

};

Intel Edison with Light Sensor Code

JavaScript
Upload this Node.js code to an Intel Edison that is connected to a SI1145 uv/ir/visible light sensor.

TO DO:
Configure the device parameters with your Edison's values:
1. AWS IoT private.pem.key
2. AWS IoT certficate.pem.crt
3. Your Edison's hostname
4. The AWS region you are using AWS IoT
5. SNS topic name to: device.subscribe and device.publish
/*jslint node:true, vars:true, bitwise:true, unparam:true */
/*jshint unused:true */

//* CONFIGURE and DEFINE

var fs = require('fs');
var os = require('os');
var mraa = require('mraa'); //require mraa
var si_lib = require('jsupm_si114x');
var AWS = require('aws-sdk');
var awsIot = require('aws-iot-device-sdk');


// AWS IoT DEVICE
 var device = awsIot.device({
   keyPath: '/home/root/awsCerts/YOUR-AWS-IOT-private.pem.key',
  certPath: '/home/root/awsCerts/YOUR-AWS-IOT-certificate.pem.crt',
    caPath: '/home/root/awsCerts/root-CA.crt',
  clientId: 'YOUR-EDISON-NAME',
    region: 'us-east-1' // fill this in with the AWS region you are using
});

// Connect to AWS IT and publish a test output "testData: 1" to the SNS topic
device
  .on('connect', function() {
    console.log('connect');
    device.subscribe('YOUR-SNS-TOPIC');
    device.publish('YOUR-SNS-TOPIC', JSON.stringify({ testData: 1 }));
    });


// define light sensor and get the Edison's host name
var uv = new si_lib.SI114X(1);
var deviceId = os.hostname();


// initialize sensors and services
uv.setUVCalibration(0,9,5,0);
//uv.setUVCalibration(4,21,2,0);
uv.initialize();

// CALL THE PERIODIC ACTIVITY FUNCTION
periodicActivity(); 



// * Periodic Activity Function - Runs in a loop with wait in between

function periodicActivity() 
{  
    
// get the light sensor reading  
var uv_visible = uv.readWord(0x22); // lux

  
//*** This section is for debugging with XDK console output ***//
/*    
    // get the current date in ISO format
var date = new Date;
var time_now = date.toISOString();
    
    var json = {
        device_id: deviceId,
        time: time_now,
        uv_visible: uv_visible // lux
    };
    
var payload = JSON.stringify(json);    
console.log(payload);
*/


// Publish the light sensor reading to the AWS SNS topic
device.publish('YOUR-SNS-TOPIC', JSON.stringify({uv_visible: uv_visible}));
 

setTimeout(periodicActivity,10000); // run periodicActivity again in 10 seconds
    
}

AWS Lambda Role Policy

Plain text
generated automatically when creating lambda, here for reference.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": "arn:aws:logs:*:*:*"
        }
    ]
}

AWS SNS Role Policy

Plain text
{
    "Version": "2012-10-17",
    "Statement": {
        "Effect": "Allow",
        "Action": "sns:Publish",
        "Resource": "arn:aws:sns:us-east-1:973529660983:hue-office-topic"
    }
}

AWS IoT Rule SQL Query

SQL
This is the SQL query that IoT runs against incoming data. The query only selects to ingest data from the SNS hue-office-topic when the uv_visble < 400.
SELECT uv_visible FROM 'hue-office-topic' WHERE uv_visible < 400

Credits

truthkos

truthkos

4 projects • 11 followers
Climate science hacker.

Comments