Ralph Yamamoto
Published © GPL3+

Outdoor Vegetable Garden Monitor and Maintenance Controller

Smart system to monitor and water the garden using the Onion Omega2+ with the IBM Watson IOT Platform.

IntermediateFull instructions provided8 hours2,732

Things used in this project

Hardware components

Onion Corporation Omega2+ & Arduino Dock 2 Bundle
×1
12V Soil Humidity Sensor Controller
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
Adafruit Water Solenoid Valve
×1
Logitech HD Webcam C525
×1
Garden Misting System
×1
1/2 Inch Double Female Hose Connector
×1
1/2 Inch by 25 Foot Hose
×1
1/2 Inch by 10 Foot PVC Pipe
×1

Software apps and online services

Watson
IBM Watson
Arduino IDE
Upverter
Upverter

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Tektronix DMM

Story

Read more

Schematics

Garden Controller Schematic

Code

gardenController.ino

Arduino
This is code to use the Arduino Dock to send measurements to the Omega2+.
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define DHTPIN            2         // digital pin 2
#define photocellPin      0         // analog pin 0
#define moisturePin       1         // analog pin 0

#define DHTTYPE           DHT11     // DHT 11
//#define DHTTYPE           DHT22     // DHT 22 (AM2302)
//#define DHTTYPE           DHT21     // DHT 21 (AM2301)

// datatype for our sensor
typedef struct weatherData {
  float temperature;
  float humidity;
  int ambientLight;
  int soilMoisture;
} weatherData_t;

int photocellReading;
int moistureReading;

// dht object
DHT_Unified dht(DHTPIN, DHTTYPE);

uint32_t delayMS;

weatherData_t sensorResponse;


bool getTemperature(DHT_Unified &dht, sensors_event_t &event) {
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    return false;
  }
  return true;
}

bool getHumidity(DHT_Unified &dht, sensors_event_t &event) {
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    return false;
  }
  return true;
}

bool getLight() {
  photocellReading = analogRead(photocellPin);
  sensorResponse.ambientLight = photocellReading;
}

bool getMoisture() {
  moistureReading = analogRead(moisturePin);
  sensorResponse.soilMoisture = moistureReading;
}

// function to get the weather
// on success, updates the global sensorResponse obj
// on fail, does nothing and returns false
bool getWeather() {

  bool status = false;
  sensors_event_t event;

  // try to read the temperature
  if (getTemperature(dht, event)) {
    status = true;
    sensorResponse.temperature = (event.temperature * 1.8) + 32;  //convert to fahrenheit
  }
  else {
    Serial.println("Could not get temperature.");
    return false;
  }
  
  // try to read humidity
  if (getHumidity(dht, event)) {
    status = true;
    sensorResponse.humidity = event.relative_humidity;
  }
  else {
    Serial.println("Could not get humidity.");
    return false;
  }

  // read light sensor
  getLight();

  // read soil moisture
  getMoisture();
  
  return status;
}

// main responder to serial commands
// response encoded in JSON
void responder() {
  // read the weather sensor
  if (getWeather()) {
    String temperature = String(sensorResponse.temperature);
    String humidity = String(sensorResponse.humidity);
    String light = String(sensorResponse.ambientLight);
    String moisture = String(sensorResponse.soilMoisture);

    // encode output to JSON
    String response = "{\"temperature\":" + temperature + ", \"humidity\":" + humidity + ", \"light\":" + light +  ", \"moisture\":" + moisture + "}";
    Serial.println(response);
  }
  else {
    // send false
    Serial.println("false");
  }
}

void setup() {
  // start serial
  Serial.begin(9600);

  // initialize DHT sensor
  dht.begin();
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  dht.humidity().getSensor(&sensor);
  delayMS = sensor.min_delay / 1000;
}

void loop() {
  // check for input on serial port
  if (Serial.available() > 0) {
    // read the input
    int inByte = Serial.read();
    delay(500); // small delay before responding

    // respond only if correct command is received
    if ((char)inByte == 'r') {
      responder();
      delay(delayMS);
    }
  }
}

Credits

Ralph Yamamoto

Ralph Yamamoto

8 projects • 17 followers

Comments