Shirley AravaPriya Daniel MSampreeti Alam
Published © Apache-2.0

Plantopia

Build a utopia around the year for your plants even when you are not near.

IntermediateWork in progress16 hours396
Plantopia

Things used in this project

Story

Read more

Schematics

Schematics 'Plantopia'

4vxjgmoyrmdorbvnyud2

Code

Plantopia

JavaScript
/*
 * testPcode Node.js starter app.
 *
 * Use this template to start an IoT Node.js app on any supported IoT board.
 * The target board must support Node.js. It is helpful if the board includes
 * support for I/O access via the MRAA and UPM libraries.
 *
 * https://software.intel.com/en-us/xdk/docs/lp-xdk-iot
 */



"use strict" ;

var mraa = require("mraa") ;
var mqtt = require('mqtt');
var fSystem = require('fs');

var LightSensorPin_A0 = new mraa.Aio(0);
var MoistureSensorPin_A2 = new mraa.Aio(2);

var ReadSensorValue = false;


var options = {
  host: 'a28xfzz2n1aori.iot.us-west-2.amazonaws.com',
  key: fSystem.readFileSync('/home/root/connect_device_package/myEdison.private.key'),
  cert: fSystem.readFileSync('/home/root/connect_device_package/myEdison.cert.pem'),
  ca:  [fSystem.readFileSync('/home/root/connect_device_package/root-CA.crt')],
  requestCert: true,
  rejectUnauthorized: true,
  port: 8883,
  cleanSession: true,
  //protocolId: 'MQIsdp',
  //protocolVersion: 
};


var mqttClient  = mqtt.connect('mqtts://data.iot.us-west-2.amazonaws.com',options);
console.log("Connecting to AWS MQTT ...");
mqttClient.on('connect', function onConnect(data) {
  console.log("Connected to AWS MQTT: " + data);
  if (ReadSensorValue == false) {
    fnLoop(); 
  }
});


var LightSensorStatus = 0;
var TempSensorStatus = 0;
var MoistureSensorStatus = 0;
var HumiditySensorStatus = 0;

var min_Moist_val = 0;
var max_Moist_val = 1023;
var min_Moist_perc = 0;
var max_Moist_perc = 100;

var five = require("johnny-five");
var Edison = require("edison-io");
var board = new five.Board({
  io: new Edison()
});

var fnLoop = function(){
    
    ReadSensorValue = true;
    var ReadLightSensor = false;
    var ReadMoistureSensor = false;
    
    var LightSensorValue    = LightSensorPin_A0.read();
    var MoistureSensorValue = MoistureSensorPin_A2.read();
    console.log("\nLight Intensity:", LightSensorValue);
    console.log("--------------------------------------");
    var moisture_percent = mapMoistureValue(MoistureSensorValue, 0, 1023, 0, 100);
    console.log("\nPercentage of Soil-Moisture:", moisture_percent + "%");
    console.log("--------------------------------------");
    
    var pubMessage = {reading_timestamp:new Date().getTime(), Illuminance:LightSensorValue};
        mqttClient.publish('Plantopia/Sensors/Illuminance/LightSensor', JSON.stringify(pubMessage),{
        }, function() {
          ReadLightSensor = true;
        }); 

    var pubMessage = {reading_timestamp:new Date().getTime(), Moisture:moisture_percent};
        mqttClient.publish('Plantopia/Sensors/Moisture/MoistureSensor', JSON.stringify(pubMessage), { 
        }, function() {
          ReadMoistureSensor = true;
        });

};

board.on("ready", function() {
    var mul = new five.Multi({
        controller: "TH02"
});

    mul.on("change", function() {
        
        console.log("Thermometer");
        //console.log("  celsius           : ", this.thermometer.celsius);
        //console.log("  fahrenheit        : ", this.thermometer.fahrenheit);
        //console.log("  kelvin            : ", this.thermometer.kelvin);
        console.log("--------------------------------------");

        console.log("Hygrometer");
        //console.log("  relative humidity : ", this.hygrometer.relativeHumidity);
        console.log("--------------------------------------");


        });
});

function mapMoistureValue(val, min_Moist_Val, max_Moist_val, min_Moist_perc, max_Moist_perc){
    return Math.round((val - min_Moist_val) * (max_Moist_perc - min_Moist_perc) / (max_Moist_val - min_Moist_val) + min_Moist_perc);
}


var intervalID = setInterval(fnLoop, 2000) ;  // read interval = 2s

process.on("exit", function(code) {
    clearInterval(intervalID) ;
    console.log("\nExiting " + ", with code:", code) ;
}) ;

Credits

Shirley Arava

Shirley Arava

1 project • 0 followers
Contact
Priya Daniel M

Priya Daniel M

1 project • 0 followers
Contact
Sampreeti Alam

Sampreeti Alam

1 project • 0 followers
Graduate Student in Dept. of ECE, New York University
Contact

Comments