Things used in this project

Schematics

Schematics 'Plantopia'
4vxjgmoyrmdorbvnyud2

Code

PlantopiaJavaScript
/*
 * 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

N6doiusgtjbuxyyqiucv
Sampreeti Alam

Graduate Student in Dept. of ECE, New York University

Replications

Did you replicate this project? Share it!

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Developing a Low-Cost Microcentrifuge Using E-Waste
Intermediate
  • 109
  • 3

Work in progress

Quality microcentrifuges are generally too expensive for most people to afford. This project aims to provide a low-cost alternative.

Flightduino 101
Intermediate
  • 353
  • 7

Full instructions

A drone project that uses the Neural Pattern Recognition Module and other aspects of the Curie Module to advantage for creating a cool drone

DIY Smart Lamp - controlled by toggle switch and Alexa
Intermediate
  • 291
  • 5

Full instructions

Build a smart lamp that can be controlled by flipping a switch and Alexa, for only $35.

Arduino 101 BLE App
Intermediate
  • 1,826
  • 23

Work in progress

I want to show you how to make an application and connect it with the Arduino 101 platform for Internet of things applications.

Raspberry Pi Facial Recognition
Intermediate
  • 5,116
  • 42

Full instructions

So you want to control entry to your secret lair, huh? This project will allow you to use facial recognition using a Pi and AWS.

SMART CUBE: A New Way to Control Your Home
Intermediate
  • 619
  • 5

Full instructions

Discover a new way to interact with your smart home thanks to Arduino 101: tilt and shake the cube to control your devices via Bluetooth!

ProjectsCommunitiesContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login
Respect project
Feedback