Pooja Baraskar
Published © LGPL

Let's make a smartwatch with AWS IoT

Intel Edison is small enough to use as a wearable, so why not we make a smart watch with Edison and powerful AWS IoT.

IntermediateFull instructions provided5,079
Let's make a smartwatch with AWS IoT

Things used in this project

Story

Read more

Code

ThingShadows AWS

JavaScript
/*

MRAA - Low Level Skeleton Library for Communication on GNU/Linux platforms
Library in C/C++ to interface with Galileo & other Intel platforms, in a structured and sane API with port nanmes/numbering that match boards & with bindings to javascript & python.

Steps for installing MRAA & UPM Library on Intel IoT Platform with IoTDevKit Linux* image
Using a ssh client: 
1. echo "src maa-upm http://iotdk.intel.com/repos/1.1/intelgalactic" > /etc/opkg/intel-iotdk.conf
2. opkg update
3. opkg upgrade

*/
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;



//aws.config.region = REGION;
//var privateKey = fs.readFileSync(awsClientPrivateKey);
//var clientCert = fs.readFileSync(awsClientCert);
//var rootCA = [fs.readFileSync(awsRootCACert)];
//var kinesis = new aws.Kinesis();



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


});

Code for Displaying Time and Temperature

JavaScript
var lcd = new jsUpmI2cLcd.Jhd1313m1(6, 0x3E, 0x62);

var groveSensor = require('jsupm_grove');

var today = setInterval(function ()
    {
    var d = new Date();
    var b= d.toTimeString();
    lcd.setColor(0, 255, 0);

// Go to the 2nd row, 6th character (0-indexed)

    lcd.setCursor(0,0);
    lcd.write(b);
     var celsius = temp.value();
        var fahrenheit = celsius * 9.0/5.0 + 32.0;
        var t = Math.round(fahrenheit);
        lcd.setCursor(1, 1);
        lcd.write(t+" *F");
       v.saveValue(t);
}, 1000);

Code for Sending SMS

JavaScript
var twilio = require('twilio');


// Create a new REST API client to make authenticated requests against the

// twilio back end

var TWILIO_ACCOUNT_SID = '' ;

var TWILIO_AUTH_TOKEN = '';

var OUTGOING_NUMBER = '';

var TWILIO_NUMBER = '';


var client = new twilio.RestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);


// Pass in parameters to the REST API using an object literal notation. The

// REST client will handle authentication and response serialzation for you.

client.sms.messages.create({

    to:OUTGOING_NUMBER,

    from:TWILIO_NUMBER,

    body:'Hi, sending from my Edison SmartWatch'

}, function(error, message) {

    // The HTTP request to Twilio will run asynchronously. This callback

    // function will be called when a response is received from Twilio

    // The "error" variable will contain error information, if any.

    // If the request was successful, this value will be "falsy"

    if (!error) {

        // The second argument to the callback will contain the information

        // sent back by Twilio for the request. In this case, it is the

        // information about the text messsage you just sent:

        console.log('Success! The SID for this SMS message is:');

        console.log(message.sid);


        console.log('Message sent on:');

        console.log(message.dateCreated);

    } else {

        console.log('error: ' + error.message);

    }

});

 

Code for Accelerometer and Gyroscope

JavaScript
var accelrCompassSensor = require('jsupm_lsm303');

// Instantiate LSM303 compass on I2C

var myAccelrCompass = new accelrCompassSensor.LSM303(0);


var successFail, coords, outputStr, accel;

var myInterval = setInterval(function()

{

        // Load coordinates into LSM303 object

        successFail = myAccelrCompass.getCoordinates();

        // in XYZ order. The sensor returns XZY,

        // but the driver compensates and makes it XYZ

        coords = myAccelrCompass.getRawCoorData();


    // Print out the X, Y, and Z coordinate data using two different methods

        outputStr = "coor: rX " + coords.getitem(0)

                                      + " - rY " + coords.getitem(1)

                                      + " - rZ " + coords.getitem(2);

        console.log(outputStr);

        outputStr = "coor: gX " + myAccelrCompass.getCoorX()

                               + " - gY " + myAccelrCompass.getCoorY()

                               + " - gZ " + myAccelrCompass.getCoorZ();

        console.log(outputStr);


    // Get and print out the heading

        console.log("heading: " + myAccelrCompass.getHeading());


    // Get the acceleration

        myAccelrCompass.getAcceleration();

        accel = myAccelrCompass.getRawAccelData();

    // Print out the X, Y, and Z acceleration data using two different methods

        outputStr = "acc: rX " + accel.getitem(0)

                               + " - rY " + accel.getitem(1)

                               + " - Z " + accel.getitem(2);

        console.log(outputStr);

        outputStr = "acc: gX " + myAccelrCompass.getAccelX()

                               + " - gY " + myAccelrCompass.getAccelY()

                               + " - gZ " + myAccelrCompass.getAccelZ();

        console.log(outputStr);

        console.log(" ");

}, 1000);


// Print message when exiting

process.on('SIGINT', function()

{

        clearInterval(myInterval);

        myAccelrCompass = null;

        accelrCompassSensor.cleanUp();

        accelrCompassSensor = null;

        console.log("Exiting");

        process.exit(0);

});

Code for turning on the flashlight with button press

JavaScript
var groveSensor = require('jsupm_grove');

// Create the button object using GPIO pin 0

var button = new groveSensor.GroveButton(2);

// Read the input and print, waiting one second between readings


function readButtonValue() {
    console.log(button.name() + " value is " + button.value());
    var v=button.value();
    if(v==1){ led.on();}
    if(v==0){ led.off();}
}
setInterval(readButtonValue, 1000);

EdisonSmartWatchAWS

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