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!
Jas ChimniLou RPerry ChowNaveena Iam
Published © MIT

Crop Defender

Crop Defender saves agriculture crops from high temperature conditions and pests.

IntermediateWork in progress20 hours1,684
Crop Defender

Things used in this project

Hardware components

Seeed Studio Grove Indoor Environment Kit for Intel® Edison
×1
Seeed Studio Grove temperature sensor
×1
12V DC water pump
×1
Seeed Studio Grove relay
×1
12V power supply with power connector
×1

Software apps and online services

AWS Lambda
Amazon Web Services AWS Lambda
Arduino IDE
Arduino IDE

Story

Read more

Code

Edison/Arduino code

Arduino
Reads the sensors and triggers some action.
/*    
 * Crop Defender
 * AWS-Intel Hardware Hackathon
 * November 5-6, 2016
 */
#include <Wire.h>
#include <TimerOne.h>
#include "Arduino.h"
#include <math.h>;
#include "rgb_lcd.h"
#include "pitches.h"
#include <Servo.h> 

#define MoisturePin     A1
#define WaterflowPin    5
#define RelayPin        6
#define BuzzerPin       7

//#define OneSecond       1000
//#define DataUpdateInterval 20000  // 20S
//#define RelayOn         HIGH
//#define RelayOff        LOW

/*macro definitions of PIR motion senso///r pin and LED pin*/
#define PIR_MOTION_SENSOR 2 // Use pin 2 to receive the signal from the module
#define LED 4               // the Grove - LED is connected to D4 of Arduino
 
//*************************

// Temperature
int a;
float temperature;
int B=3975;                  //B value of the thermistor
float resistance;

// RGB LCD
rgb_lcd lcd;

// Moisture
// Test code for Grove - Moisture Sensor 
int moisturePin = A1; // select the input pin for the potentiometer
int moistureValue = 0; // variable to store the value coming from the sensor7

// Buzzer
int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_C4
};

int noteDurations[] = {
  4, 4, 4
};

// Servos for shade and mylar
Servo shadeServo;
Servo mylarServo;


bool bBuzzer = true;     // TODO: Enable this!!!
bool bLED = true;


//*************************
 
void setup()
{
  Serial.begin(9600);  

    // set up the LCD's number of columns and rows:
    lcd.begin(16, 2);
    Serial.println("lcd begin");
    lcd.setRGB(171, 208, 236);

    // Motion sensor and the light it controls
    pinMode(PIR_MOTION_SENSOR, INPUT);
//    pinMode(LED,OUTPUT);  // LED sensor is hosed. :-p
    pinMode(LED_BUILTIN, OUTPUT);

    // Relay for water pump
    pinMode(RelayPin, OUTPUT);

    // Servos for shade and mylar;
    shadeServo.attach(3);
    mylarServo.attach(5);

    delay(4000); 
}
 
void loop()
{
    readTemperature();
    readMoisture();
    readMotion();
  
    delay(1000);
 }

void readTemperature() 
{
    // Temperature sensor
    a=analogRead(0);
    resistance=(float)(1023-a)*10000/a; //get the resistance of the sensor;
    temperature=1/(log(resistance/10000)/B+1/298.15)-273.15;//convert to temperature via datasheet&nbsp;;

    lcd.clear();
    lcd.print("Temperature ");
    lcd.print(temperature);
    lcd.println("˚C");
    Serial.print("Current temperature is ");
    Serial.println(temperature);

    // TODO: If temp above n  (was 28), pull shade cloth
    if (temperature > 50.0) 
    {
      pullShade();
    }
    // TODO: If temp above n + m (second threshold; was 30), turn on sprinkler
    if (temperature > 55.0)
    {
      waterThePlants();
    }
 }

void readMoisture()
{
    moistureValue = analogRead(moisturePin);
    Serial.print("moisture = " );
    Serial.println(moistureValue);

    // TODO: If moisture below x, turn on relay for water
    if (moistureValue < 100)
    {
      waterThePlants();
    }
}

// Flash LED and set off buzzer if motion detected
void readMotion()
{
    int sensorValue = digitalRead(PIR_MOTION_SENSOR);
    if (sensorValue == HIGH) // if motion detected
    {
        playBuzzer();
        flashLED();
        shooBirds();
    }
}

// Flash LED
void flashLED() {
  if (!bLED)
    return;

  int delayMs = 350;
        
  for (int i=0; i<5; i++) 
  {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(delayMs);
    digitalWrite(LED_BUILTIN, LOW);
    delay(delayMs);
  }

  bLED = false;
}

// Make a buzzing noise
void playBuzzer()
{
  if (!bBuzzer)
    return;
    
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 3; thisNote++) {

    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(BuzzerPin, melody[thisNote], noteDuration);  

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(BuzzerPin);
  }

  bBuzzer = false;  // Do this just once; it's too annoying
}

// Turn on water for n seconds
void waterThePlants() {
  digitalWrite(RelayPin, HIGH);
  delay(10000);
  digitalWrite(RelayPin, LOW);
}

void pullShade() {
  if (!shadeServo.attached())
    return;
    
  for (int pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                       // in steps of 1 degree 
    shadeServo.write(pos);                // tell servo to go to position in variable 'pos' 
    delay(15);                            // waits 15ms for the servo to reach the position 
  } 
  delay(15);
  // Reposition servo
  for (int pos = 180; pos>0; pos-=1)      // goes from 180 degrees to 0 degrees 
  {                                
    shadeServo.write(pos);                // tell servo to go to position in variable 'pos' 
    delay(5);                             // waits 15ms for the servo to reach the position 
  } 
  
  shadeServo.detach();
}

void shooBirds() {
  for (int i = 0; i < 3; i++)
  {
    for (int pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
    {                                       // in steps of 1 degree 
      mylarServo.write(pos);                // tell servo to go to position in variable 'pos' 
      delay(5);                             // waits 15ms for the servo to reach the position 
    } 
    for (int pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
    {                                
      mylarServo.write(pos);                // tell servo to go to position in variable 'pos' 
      delay(5);                             // waits 15ms for the servo to reach the position 
    } 
  }
  mylarServo.detach();
}

Credits

Jas Chimni

Jas Chimni

1 project • 2 followers
Lou R

Lou R

2 projects • 1 follower
Perry Chow

Perry Chow

2 projects • 4 followers
hacker
Naveena Iam

Naveena Iam

1 project • 1 follower
Thanks to Lou de los Reyes and Perry Chow.

Comments