Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Benjamin WiniarskiAlex Winiarski
Published © GPL3+

Smart Water Management System for Developing Farmers

Would like to solve the problems of water scarcity and lack of farming techniques in developing nations through the use of IoT and AI.

AdvancedFull instructions provided10 hours6,507
Smart Water Management System for Developing Farmers

Things used in this project

Story

Read more

Schematics

Raspberry Pi Zero Schematics

Found the pins I needed

Code

AWS Lambda Code

Java
AI Brain to trigger watering system
console.log('Loading event'); 
var AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB();
var iotData = new AWS.IotData({endpoint: '{your_endpoint}'});
var callback;

//make sure your replace your_AWS_endpoint_you_copied_earlier with the value you copied just before

exports.handler = function(event, context,callback) {
    console.log("Request received:\n", JSON.stringify(event));
    console.log("Context received:\n", JSON.stringify(context));
    
    var tableName = event.operatorId;
    var time = event.timestamp;
    var Moisture = event.payloads.measure;
    
    var sleep = require('sleep');
    
    if (Moisture <= 300) {
        console.log("Turning Water On");
        var command = 'wateron';
        httpsGet(command);
        sleep.sleep(40);
        console.log("Turning Water Off");
        var command = 'wateroff';
        httpsGet(command);
    };
    
    function returnTime(value){
        return new Date((value)*1000 + 39600000); 
    }
    //replace 39600000 with the Unix time offset according to your timezone. Here it is set to Sydney time offset. 




 //Next function will store the message in a dynamoDB table
    dynamodb.putItem({
            "TableName": event.operatorId,
            "Item": {
                "deviceId": {
                    "S": event.operatorId
                }, 
                "time": {
                    "S": event.timestamp.toString() 
                },
                 "Moisture" :{
                    "S": Moisture.toString() 
                }
                
            }
        },
    
        function(err, data){
            if (err) {
                context.fail('ERROR: Dynamo failed: ' + err);
            } else {
        console.log('Dynamo Success: ' + JSON.stringify(data, null, ' '));
                context.succeed('SUCCESS');
            }
        }); 
        
        
        
    var https = require('https');
    function httpsGet(command) {
        var options = {
            host: 'maker.ifttt.com',
            port: 443,
            path: '/trigger/'+command+'/with/key/{your_webhook_key}',
            method: 'GET',
        };
    
        var req = https.request(options, res => {
            res.setEncoding('utf8');
            callback('Success');
            var returnData = "";
    
            res.on('data', chunk => {
                returnData = returnData + chunk;
            });
        });
        req.end();

    }    
     
      
        
        
        
};

Rasberry Pi Zero Code

Code used to get soil moisture readings and send them to Soracom

Credits

Benjamin Winiarski

Benjamin Winiarski

3 projects • 15 followers
Entrepreneur and IOT Developer
Alex Winiarski

Alex Winiarski

2 projects • 8 followers

Comments