Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Published © GPL3+

Are the lights on?

It's a pretty common question, and all the solution takes is an Edison, AWS, wire and photoresistors.

BeginnerFull instructions provided1,064
Are the lights on?

Things used in this project

Hardware components

USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Intel Edison with Mini-Breakout Board
×1
Adafruit Wire - Black
×1
Adafruit Wire - Red
×1
Adafruit USB Battery Pack 5V 1A
×1
Pocket Solder- 60/40 Rosin Core 0.031" diameter
Pocket Solder- 60/40 Rosin Core 0.031" diameter
×1
Adafruit Perforated Board
×1
Female Header 20 Position 2 Row (0.1")
Female Header 20 Position 2 Row (0.1")
×1
Male Header 40 Position 1 Row (0.1")
Male Header 40 Position 1 Row (0.1")
×1
Photo resistor
Photo resistor
×1

Software apps and online services

AWS IAM
Amazon Web Services AWS IAM
AWS IoT
Amazon Web Services AWS IoT
Putty
Intel XDK IoT Edition

Story

Read more

Schematics

Simplified Setup Example

Fritzing source

N.B. The pins on the Edison object are partially labled incorrectly. I have it hoocked up as it should be on an actual Edison. The power will say it is in 3.3 but I have checked the docs and it is actually 1.8 on the board.

Code

main.js

JavaScript
"use strict";
//in mraa the equivalent of Arduino pin one/linux 130 is 26, you'll have to calculate any others try http://iotdk.intel.com/docs/master/mraa/edison.html
//node.js deps
// inherits
// mqtt
// minimist

//npm deps
// aws-iot-device-sdk

//use mraa
var mraa = require('mraa'); //require mraa

var myDigitalPin1 = new mraa.Gpio(26); //setup digital read on Digital pin 
myDigitalPin1.dir(mraa.DIR_IN); //set the gpio direction to input

periodicActivity(); //call the periodicActivity function

function periodicActivity()
{
  var myDigitalValue =  myDigitalPin1.read(); //read the digital value of the pin
  if(myDigitalValue == 1)
  {
      //trigger alarm
      send_state(); //send state to aws only if system is triggered to reduce message usage
  }
  setTimeout(periodicActivity,100); //call the indicated function after .1 second (100 milliseconds)
}
//app deps
var thingShadow = require('aws-iot-device-sdk/thing');  


//Define your device name
var Device_Name = 'mySystem1'; //unique device ID


//define AWS certificate path paramenters
var args = {
	privateKey:'/home/root/aws_certs/privateKey.pem',
	clientCert:'/home/root/aws_certs/cert.pem',
	caCert:'/home/root/aws_certs/aws-iot-rootCA.crt',
	clientId:'lightsOn1',
	region:'us-east-1',
	reconnectPeriod:'10'
};


//create global state variable

var reported_state={ systemState: false};

//launch sample app function 

update_state(args);

function update_state(args) {

//create a things Shadows object

var thingShadows = thingShadow({
  keyPath: args.privateKey,
  certPath: args.clientCert,
  caPath: args.caCert,
  clientId: args.clientId,
  region: args.region,
  reconnectPeriod: args.reconnectPeriod,
});

//When Thing Shadows connects to AWS server:


thingShadows
  .on('connect', function() {
    console.log('registering device: '+ Device_Name);

    //register device
    //register device
    thingShadows.register(Device_Name);
    });


// motitor for events in the stream and print to console:

thingShadows 
  .on('close', function() {
    console.log('close');
  });
thingShadows 
  .on('reconnect', function() {
    console.log('reconnect');
  });
thingShadows 
  .on('offline', function() {
    console.log('offline');
  });
thingShadows
  .on('error', function(error) {
    console.log('error', error);
  });
thingShadows
  .on('message', function(topic, payload) {
    console.log('message', topic, payload.toString());
  });

}



//define function for updating thing state:

  function send_state(){


      //define the payload with sensor values

      reported_state = myDigitalPin1.read();
      
      //create state update payload JSON:
      device_state={state: { reported: reported_state }};

      //send update payload to aws:
      thingShadows.update(Device_Name, device_state );

  }

package.json

JSON
{
  "name": "myLightMonitor",
  "description": "Code to run a light monitor. Build tutorial available on Hackster.io.",
  "version": "1.0.0",
  "main": "main.js",
  "engines": {
    "node": ">=0.10.0"
  },
  "dependencies": {
      "aws-iot-device-sdk" : "^1.0.9"
  }
}

Credits

Comments