Environment Monitor With AWS IoT

The compute module of Intel called Intel Edison is slightly larger than a SD card. It has an onboard Wi-Fi and Bluetooth, perfect for IoT projects. We can connect to Edison remotely and run commands or access the file system. This gives lots of flexibility to developers via SSH.

Edison can be used with Arduino IDE but to get the most out of it you can use other programming languages like Python, Node.js, C/C++. Intel has its own IDE called Intel XDK IoT edition which makes programming with Edison easy. While setting up the programming environment for Edison you can choose between Arduino IDE, Intel XDK or Eclipse.

Let us create a simple temperature monitor with Edison which will monitor the room temperature and notify us through an email. We will be using AWS Cloud in this project. AWS environment consists of a number of different AWS services providing security, transport, and storage of the sender data produced by your device. All services in AWS are delivered via a rich set of REST APIs. You can use a service programmatically through the APIs or can invoke manually using the console which make AWS Cloud powerful. It also offers numbers of API to speed our development with the language of our choice. Hence I have chosen its Node.Js library to interact with my Edison.

Follow the steps to setup the cloud environment:

Create an account at AWS Cloud.

Create an account at AWS Cloud
                                             
                                                      Figure 1:
Create an account at AWS Cloud

Sign in to AWS IoT Console https://aws.amazon.com/iot.

Sign in to AWS IoT
                                                               Figure 2: Sign in to AWS IoT

Create and attach a thing.

Create and attach thing
                                                      Figure 3: Create and attach thing

Generate and download the keys and certificates to make connections.

Certificates to make connections
                                                      Figure 4: Certificates to make connections

Set up AWS SNS and create a topic.

Simple notification service
                                                      Figure 5: Simple notification service

Create an Email Subscription in SNS and publish the topic.

Email Subscription in SNS
                                                   Figure 6: Email Subscription in SNS

In AWS IoT page, Create a Rule from the Resource panel and select “Send message as a push notification (SNS)” from Action dropdown. Create a new Role and add the recently created SNS action.

Create SNS action

                                                               Figure 7: Create SNS action

Send message as a push notification (SNS)
                                                         Figure 8: Send message as a push notification (SNS)

Create an IAM Role. Enter a User Name as snsReceiver and click Create. Now attach the Policy. From the list select AmazonSNSFullAccess&AmazonIoTFullAccess and click on Attach Policy.

Attach Policy
                                                                        Figure 9: Attach Policy

Setting up Edison

Flash your Edison and enable WIFI on it.

Enable WIFI
                                                                     Figure 10: Enable WIFI

Transfer Certificates and key files to Edison.

Key files to Edison
                                                               Figure 11: Key files to Edison

Establish a serial connection with Edison and run this command to install the AWS IoT SDK.

npm install aws-iot-device-sdk

Install aws-iot-device-sdk
                                                            Figure 12: Install aws-iot-device-sdk

Create a new project in Intel XDK, and paste the code.

Code

  1. varawsIot = require('aws-iot-device-sdk'); //require for awsiot  
  2. varmraa = require('mraa'); //require mraa for analog/digital read/write  
  3. console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the console  
  4.   
  5.   
  6. /* 
  7.  * CONFIGURATION VARIABLES  
  8.  * To set your AWS credentials, export them to your environment variables. 
  9.  * Run the following from the Edison command line: 
  10.  * export AWS_ACCESS_KEY_ID='AKID' 
  11.  * export AWS_SECRET_ACCESS_KEY='SECRET' 
  12.  */  
  13.   
  14. // AWS IoT Variables  
  15. varmqttPort = 8883;  
  16. varrootPath = '/home/root/awscerts/';  
  17. varawsRootCACert = "root-CA.pem.crt";  
  18. varawsClientCert = "certificate.pem.crt";  
  19. varawsClientPrivateKey = "private.pem.key";  
  20. vartopicName = "Edison";  
  21. varawsClientId = "Edison";  
  22. varawsIoTHostAddr = "https://AWVI662RQY269.iot.us-west-2.amazonaws.com";  
  23.   
  24.   
  25.   
  26. /* 
  27.  * Instance AWS variables for use in the application for 
  28.  * AWS IoT Certificates for secure connection. 
  29.  */  
  30. varprivateKeyPath = rootPath + awsClientPrivateKey;  
  31. varclientCertPath = rootPath + awsClientCert;  
  32. varrootCAPath = rootPath + awsRootCACert;  
  33.   
  34.   
  35. /* 
  36. *Initializing Device Communication for AWS IoT 
  37. */  
  38.   
  39.   
  40.   
  41. varmyThingName = 'Edison';  
  42.   
  43. varthingShadows = awsIot.thingShadow({  
  44. keyPath: privateKeyPath,  
  45. certPath: clientCertPath,  
  46. caPath: rootCAPath,  
  47. clientId: awsClientId,  
  48. region: 'us-west-2'  
  49. });  
  50. console.log("AWS IoT Device object initialized");  
  51.   
  52.   
  53. mythingstate = {  
  54.   "state": {  
  55.     "reported": {  
  56.       "ip""unknown"  
  57.     }  
  58.   }  
  59. }  
  60.   
  61. varnetworkInterfaces = require( 'os' ).networkInterfaces( );  
  62. mythingstate["state"]["reported"]["ip"] = networkInterfaces['wlan0'][0]['address'];  
  63.   
  64. vartemperaturePin = new mraa.Aio(2); //setup access analog input Analog pin #2 (A2)  
  65.   
  66. vartemperatureValue = temperaturePin.read(); //read the value of the analog pin  
  67. console.log(temperatureValue); //write the value of the analog pin to the console  
  68.   
  69.   
  70. // calculate temperature  
  71. vartmpVoltage = ((temperatureValue*5.0)/1023.0); // convert analog value to voltage  
  72. var temperature = (5.26*Math.pow(tmpVoltage,3))-(27.34*Math.pow(tmpVoltage,2))+(68.87*tmpVoltage)-17.81;  
  73. console.log(temperature);  
  74.   
  75.   
  76. thingShadows.on('connect', function() {  
  77. console.log("Connected...");  
  78. console.log("Registering...");  
  79. thingShadows.register(myThingName );  
  80.   
  81.   // An update right away causes a timeout error, so we wait about 2 seconds  
  82. setTimeout( function() {  
  83. console.log("Updating my IP address...");  
  84. clientTokenIP = thingShadows.update(myThingName, mythingstate);  
  85. console.log("Update:" + clientTokenIP);  
  86.   }, 2500 );  
  87.   
  88.   
  89.   // Code below just logs messages for info/debugging  
  90. thingShadows.on('status',  
  91. function(thingName, stat, clientToken, stateObject) {  
  92. console.log('received '+stat+' on '+thingName+': '+  
  93. JSON.stringify(stateObject));  
  94.     });  
  95.   
  96. thingShadows.on('update',  
  97. function(thingName, stateObject) {  
  98. console.log('received update '+' on '+thingName+': '+  
  99. JSON.stringify(stateObject));  
  100.       });  
  101.   
  102. thingShadows.on('delta',  
  103. function(thingName, stateObject) {  
  104. console.log('received delta '+' on '+thingName+': '+  
  105. JSON.stringify(stateObject));  
  106.       });  
  107.   
  108. thingShadows.on('timeout',  
  109. function(thingName, clientToken) {  
  110. console.log('received timeout for '+ clientToken)  
  111.       });  
  112.   
  113. thingShadows  
  114.     .on('close', function() {  
  115. console.log('close');  
  116.     });  
  117. thingShadows  
  118.     .on('reconnect', function() {  
  119. console.log('reconnect');  
  120.     });  
  121. thingShadows  
  122.     .on('offline', function() {  
  123. console.log('offline');  
  124.     });  
  125. thingShadows  
  126.     .on('error', function(error) {  
  127. console.log('error', error);  
  128.     });  
  129.       
  130.   
  131.   //Watch for temperature  
  132. if(temperature > 35 ){  
  133. thingShadows.publish('arn:aws:sns:us-west-2:316723939866:TemperaturAlarm',   
  134.                         'Your room temperature is greater than 35deg C');  
  135.    }  
  136.   
  137.   
  138. });  
Enter the credentials.

Connect the temperature sensor to A2 pin of Edison.

Temperature sensor to A2 pin of Edison
                                       Figure 13: Temperature sensor to A2 pin of Edison

Upload and Run the code.

Now you will notice if the temperature exceeds 35 degree Celsius you will receive an Email Notification.

Email Notification
                                                            Figure 14: Email Notification

If you are a beginner with Amazon AWS, stay tuned for a detailed article.
 
Read more articles on Internet of Things (IoT):

 

Up Next
    Ebook Download
    View all
    Learn
    View all