parthiban hairul
Published © GPL3+

AWS Agro Automation System

Intelligent measure to monitor and protect the agricultural lands

IntermediateFull instructions provided1,720
AWS Agro Automation System

Things used in this project

Hardware components

Grove starter kit plus for Intel Edison
Seeed Studio Grove starter kit plus for Intel Edison
Grove piezo sensor, Grove sound senor
×1

Software apps and online services

INTEL XDK

Story

Read more

Schematics

DSC015s19.jpg

Code

main.js

JavaScript
const thingShadow = require('./thing');
//UPM dependencies 
//Load Grove module
var groveSensor = require('jsupm_grove');

// Create the Piezo sensor object using AIO pin 0
var Piezo = new groveSensor.GrovePiezo(0);

// Create the Sounderature sensor object using AIO pin 1
var Sound = new groveSensor.GroveSound(1);

//Define your device name
var Device_Name = '***';

//define AWS IoT specifications as required
var args = 
{


	privateKey:'***',
	clientCert:'***',
	caCert:'***',
	clientId:'icebreaker_edison',
	region:'***', 
	reconnectPeriod:'***' 
} 


//create global state variable
var reported_state={ piezo: 0, Sound: 0};

//create global sensor value variables:
var read_piezo = 0;
var read_Sound = 0;

//launch sample app function 
update_state(args);

function update_state(args) 
{

//create a things Shadows object

const thingShadows = thingShadow(
{
  keyPath: args.privateKey,
  certPath: args.clientCert,
  caPath: args.caCert,
  clientId: args.clientId,
  region: args.region,
  reconnectPeriod: args.reconnectPeriod,
}
);

//When Thing Shadows connects to AWS server:
thingShadows
  .on('connect', function() {
  	console.log('registering device: '+ Device_Name)

  	//register device
  	thingShadows.register(Device_Name);

  	
  	//read sensor values and send to AWS IoT every 5 seconds 
  	setInterval(function(){

  	read_sensor(send_state); 

	}, 5000);

  });


// motitor for events in the stream and print to console:

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);
  });
thingShadows
  .on('message', function(topic, payload) {
    console.log('message', topic, payload.toString());
  });

};

//define function for reading sensor
  function read_sensor(cb){

  	read_piezo = Piezo.value();
  	read_Sound = Sound.value();

  	cb();
  };

//define function for updating thing state:

  function send_state(){

  	//define the payload with sensor values
  	reported_state ={ piezo: read_piezo, Sound: read_Sound};

  	//create state update payload JSON:
  	device_state={state: { reported: reported_state }};

  	//send update payload to aws:
  	thingShadows.update(Device_Name, device_state );

  };

Credits

parthiban hairul

parthiban hairul

1 project • 3 followers

Comments