aws iot

Posted on Actualizado enn

1.[on the left] registry / things / [TempSensorThing]

A thing Amazon Resource Name uniquely identifies this thing.

arn:aws:iot:us-west-2:9xxxx810:thing/TempSensor

2.Interact

This thing already appears to be connected.[click on] connect a device, in this case we are going to select linux and nodejs

Next, get started, and download the connection kit, that will give you the private key and the certificate (TempSensor.public.key / TempSensor.private.key / TempSensor.cert.pem)

3.Rule

Add a rule [AddLectureThing]

SELECT sensorID, temperatura FROM ‘MyTopic’, these fields match the dynamodb fields on the last example [sensorID,temparatura], MyTopic will be used in the JS

device.publish(‘MyTopic’, JSON.stringify({sensorID: ”+Math.random(), temperatura: ”+Math.random()}));

Finally add the lambda function with previous lambda function created, in this case RegisterLecture

Captura

Now creating a Rule that send an email when temperature is bigger than X, in this case 45

First create another Rule as follow

Captura

in configuration action /  create new resource

Create new topic

CapturaCaptura1

validate by email

Captura

Captura

var iot = require('aws-iot-device-sdk');
var __dirname="certs";

var device = iot.device({
    keyPath: __dirname + '/TempSensorThing.private.key',
    certPath: __dirname + '/TempSensorThing.cert.pem',
    caPath: __dirname + '/root-CA.crt',
    clientID: 'Prueba',
    region: 'us-west-2'
});

/*
device
  .on('connect', function() {
    console.log('connect');
    device.subscribe('TempSensorThing');
    device.publish('TempSensorThing', JSON.stringify({sensorID: '004', temperatura: '70'}));
    console.log('connectsuccess');
  });

device
  .on('message', function(topic, payload) {
    console.log('message', topic, payload.toString());
  });
*/

device.on('connect', function() {
    console.log('connect');
    device.subscribe('testIN',function(error,result){
        console.log(result);
    })
});

device.on('close', function() {
    console.log('close');
});

device.on('reconnect', function() {
    console.log('reconnect');
});

device.on('offline', function() {
    console.log('offline');
});

device.on('message',function(topic,payload) {
    console.log('message',topic,payload.toString());
});

/*
var myVar = setInterval(myTimer, 5*1000);//cada 5 seg para prueba
//var myVar = setInterval(myTimer, 5*1000*60);//cada 5 min para prod

function myTimer() {
    var d = new Date();
    console.log('Enviando info '+d.toLocaleTimeString());
    var max=10000;var min=1;
    device.publish('MyTopic', JSON.stringify({sensorID: ''+Math.floor(Math.random() * (max - min + 1)) + min, temperatura: ''+Math.random() * (50 - 0) + 0}));
}
setInterval(callbackFunction(), interval);
*/

setInterval(function(){
    var d = new Date();
    var max=10000;var min=1;
    var temp=Math.random() * (50 - 0) + 0;
    //device.publish('MyTopic', JSON.stringify({sensorID: ''+Math.floor(Math.random() * (max - min + 1)) + min, temperatura: ''+temp}));
    device.publish('MyTopicEmail', JSON.stringify({sensorID: ''+Math.floor(Math.random() * (max - min + 1)) + min, temperatura: ''+temp}));
    console.log('Enviando info '+temp+' / '+d.toLocaleTimeString());
}, 5*1000);

 

{
    "name": "IoTNodeJS",
    "version": "1.0.0",
    "keywords": ["util", "functional", "server", "client", "browser"],
    "author": "Hugo de la Mora",
    "contributors": [],
    "dependencies": {
        "aws-iot-device-sdk": "*"
    }
}

Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s