Pooja Baraskar
Published © LGPL

Environment monitor with AWS IoT

This projects shows a simple environment monitor which sends you an EMail when an abnormal weather condition is detected.

BeginnerFull instructions provided2,658
Environment monitor with AWS IoT

Things used in this project

Hardware components

Seeed Studio Grove Temperature Sensor
×1

Software apps and online services

AWS IoT
Amazon Web Services AWS IoT
AWS IAM
Amazon Web Services AWS IAM
AWS SNS
Amazon Web Services AWS SNS
Intel XDK

Story

Read more

Code

Temperature Monitor wit AWS

JavaScript
var awsIot = require('aws-iot-device-sdk'); //require for aws iot 
var mraa = require('mraa'); //require mraa for analog/digital read/write
console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the console



/*
 * CONFIGURATION VARIABLES 
 * To set your AWS credentials, export them to your environment variables.
 * Run the following from the Edison command line:
 * export AWS_ACCESS_KEY_ID='AKID'
 * export AWS_SECRET_ACCESS_KEY='SECRET'
 */

// AWS IoT Variables
var mqttPort = 8883;
var rootPath = '/home/root/awscerts/';
var awsRootCACert = "root-CA.pem.crt";
var awsClientCert = "certificate.pem.crt";
var awsClientPrivateKey = "private.pem.key";
var topicName = "Edison";
var awsClientId = "Edison";
var awsIoTHostAddr = "https://AWVI662RQY269.iot.us-west-2.amazonaws.com";



/*
 * Instance AWS variables for use in the application for
 * AWS IoT Certificates for secure connection.
 */
var privateKeyPath = rootPath + awsClientPrivateKey;
var clientCertPath = rootPath + awsClientCert;
var rootCAPath = rootPath + awsRootCACert;


/*
*Initializing Device Communication for AWS IoT
*/



var myThingName = 'Edison';

var thingShadows = awsIot.thingShadow({
     keyPath: privateKeyPath,
    certPath: clientCertPath,
    caPath: rootCAPath,
  clientId: awsClientId,
    region: 'us-west-2'
});
console.log("AWS IoT Device object initialized");


mythingstate = {
  "state": {
    "reported": {
      "ip": "unknown"
    }
  }
}

var networkInterfaces = require( 'os' ).networkInterfaces( );
mythingstate["state"]["reported"]["ip"] = networkInterfaces['wlan0'][0]['address'];

var temperaturePin = new mraa.Aio(2); //setup access analog input Analog pin #2 (A2)

var temperatureValue = temperaturePin.read(); //read the value of the analog pin
console.log(temperatureValue); //write the value of the analog pin to the console


// calculate temperature
var tmpVoltage = ((temperatureValue*5.0)/1023.0); // convert analog value to voltage
var temperature = (5.26*Math.pow(tmpVoltage,3))-(27.34*Math.pow(tmpVoltage,2))+(68.87*tmpVoltage)-17.81;
console.log(temperature);


  thingShadows.on('connect', function() {
  console.log("Connected...");
  console.log("Registering...");
  thingShadows.register( myThingName );

  // An update right away causes a timeout error, so we wait about 2 seconds
  setTimeout( function() {
    console.log("Updating my IP address...");
    clientTokenIP = thingShadows.update(myThingName, mythingstate);
    console.log("Update:" + clientTokenIP);
  }, 2500 );


  // Code below just logs messages for info/debugging
  thingShadows.on('status',
    function(thingName, stat, clientToken, stateObject) {
       console.log('received '+stat+' on '+thingName+': '+
                   JSON.stringify(stateObject));
    });

  thingShadows.on('update',
      function(thingName, stateObject) {
         console.log('received update '+' on '+thingName+': '+
                     JSON.stringify(stateObject));
      });

  thingShadows.on('delta',
      function(thingName, stateObject) {
         console.log('received delta '+' on '+thingName+': '+
                     JSON.stringify(stateObject));
      });

  thingShadows.on('timeout',
      function(thingName, clientToken) {
         console.log('received timeout for '+ clientToken)
      });

  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);
    });
	
  
  //Watch for temperature
if(temperature > 35 ){
   thingShadows.publish('arn:aws:sns:us-west-2:316723939866:TemperaturAlarm', 
                        'Your room temperature is greater than 35deg C');
   }


});

Credits

Pooja Baraskar

Pooja Baraskar

25 projects • 178 followers
Pooja Baraskar is an accomplished software engineer, inventor, and technical author with over a decade of experience in the tech industry.

Comments