VESIT-CMPN / cc_aws_iot

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cc_aws_iot

Creating a Virtual thing on AWS IoT and Developing End-to-End IoT Application.

Problem Defination :

With an increasing trend in the world of IoT and in the concept of smart city , it has become neceessity to handle the data generated by smart devices and that too remotely.Data analytics of the data generated by IoT device and device manipulation with respect to data becomes much easier with the help of good cloud platform service which supports connecting an ioT device over its cloud network.

Requirement Gathering :

  • Understanding the state of device.
  • Required data to change the device's state.
  • User should be able to control the device's state with the help of cloud network.
  • One good cloud platform with more than 99% up-time.

Methodology :

  • The first thing to ensure about the decide is that , it should be generating some data and saving it locally.
  • Now that the device is saving data locally , to get this data on cloud , we need to register our thing (IoT Device) over the cloud platform.
  • Once the thing is registered over the cloud its uniqueness is maintained with the help of private and public keys obtained by cloud platform.
  • Once the uniqueness is maintained i.e. the keys are authenticated , the data generated by the IoT device is saved over the cloud (in JSON format) and the API End points are obtained from where the data can be retrived.

Architecture :

AWS IoT Architecture

Which cloud platform service and why ?

The only IAAS which comes to our mind to build this usecase is Amazon Web Services ; It is like the only cloud platform , we feel , whose documentation can be easily understood . AWS offers one year of free tire for most of its services , one of which includes AWS IoT. The only product which Aws has for developing and managing connection for any gadget or device (IoT) is AWS IoT.Further AWS IoT's rules can helpful for connecting the thing to the services like AWS Lambda, DynamoDB , EC2 , etc, which can be useful to store data generated by the thing (IoT Device).

Implementation :

  • Creating the Thing in AWS IoT Console

Thing_temp_sensor

  • Creating a rule for the Thing to save the data generated by it.

Rule: Invoking a lambda function on MQTT message

Thing_rule

  • Connecting with the MQTT Gateway to generate messages (data)

Thing_mqtt

  • Creating a function in AWS Lambda and adding triggers to it

Lambda_fn

  • Code that saves MQTT messages(data) in DynamoDB table
'use strict';

console.log('Loading function');

const AWS = require('aws-sdk');

const docClient = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});


exports.handler = (event, context, callback) => {
    console.log('Received event:', JSON.stringify(event, null, 2));
    console.log('value1 =', event.test_data);
    
    var count = 0;
    
    var params = {
        
        Item:{
            id : count.toString() ,
            message : event.test_data.toString(),
        },
        
        TableName : 'consumption'
        
    };
    count++;
    
    docClient.put(params, function(err,data){
        if (err) {
            callback(err,null);
            console.log('Error saving Data!');
        } else {
            callback(null,data);
            console.log('Data Saved!');
        }
        
    });
    
        
    
    // console.log('value2 =', event.key2);
    // console.log('value3 =', event.key3);
    callback(null, event.key1);  // Echo back the first key value
    //callback('Something went wrong');
};
  • DynamoDB Table

dynamo

  • Launching EC2 Instance to Host the Data generated by the Thing.

ec2

  • Front-End Code that feteches data from DynamoDB.
var AWS = require("aws-sdk");
var awsConfig = {
    "region": "us-east-1",
    "endpoint": "http://dynamodb.us-east-1.amazonaws.com",
    "accessKeyId": "AFBSFUWVOKVIRHVSRS", "secretAccessKey": "bdsbldbliwvblw/viuviweugvi/fbviaufg"
};
AWS.config.update(awsConfig);

var docClient = new AWS.DynamoDB.DocumentClient();
var fetchOneByKey = function () {
    var params = {
        TableName: "consumption",
        Key: {
            "id": "0"
        }
    };
    docClient.get(params, function (err, data) {
        if (err) {
            console.log("users::fetchOneByKey::error - " + JSON.stringify(err, null, 2));
        }
        else {
            console.log("users::fetchOneByKey::success - " + JSON.stringify(data, null, 2));
        }
    })
}


fetchOneByKey();

CONCLUSION :

we learnt about different AWS Services like AWS IoT, Lambda, DynamoDB, EC2, WHILE DEVELOPING THE End-to-End IoT Application by creating virtual thing.

About