Skip to content

Instantly share code, notes, and snippets.

@rupl
Created June 20, 2017 12:24
Show Gist options
  • Save rupl/b86269b6109c010fbb3d344c4faf3c3d to your computer and use it in GitHub Desktop.
Save rupl/b86269b6109c010fbb3d344c4faf3c3d to your computer and use it in GitHub Desktop.
Så Ett Frö — Greenhouse v2, June 2017
/**
* Code to run a basic automatic greenhouse.
*
* - Temp/humidity sensor
* - Soil moisture sensor
* - Water pump
* - Ventilation fan
*
* @see http://saettfro.com/vaxthus/
*/
#include <SimpleDHT.h>
#include <LiquidCrystal.h>
// Initialize humidity sensor.
const int pinDHT11 = 6;
SimpleDHT11 dht11;
// Initialize water pump
const int pump = 7;
const int MOISTURE_PERCENT_THRESHOLD = 50; // percent
const int PUMP_TIME = 500; // milliseconds
// Initialize soil moisture sensor
const int soil = A0;
// Initialize LCD. We're using the one in the Arduino starterkit.
const int DISPLAY_DELAY = 5000; // milliseconds
const int lcdCols = 16;
const int lcdRows = 2;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
pinMode(soil, INPUT);
pinMode(pump, OUTPUT);
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
// Display is two 16-char rows
lcd.begin(lcdCols, lcdRows);
// Print a startup message.
lcd.print("HSB Living Lab");
delay(200);
// Draw startup animation
int progress = lcdCols - 1;
lcd.setCursor(0, 1);
for (int x = 0; x <= progress; x++) {
lcd.print("*");
delay(random(20,600));
}
delay(1000);
lcd.clear();
// SETUP DEBUG
Serial.begin(9600);
}
void loop() {
// Variables for DHT sensor output.
byte temperature = 0;
byte humidity = 0;
// Variables for soil moisture.
int moisture = 0;
// Clear LCD from previous loop
lcd.clear();
// Read DHT sensor data
dht11.read(pinDHT11, &temperature, &humidity, NULL);
// DEBUG
Serial.print(temperature);
Serial.print("\t");
Serial.print(humidity);
Serial.print("\t");
// Print first line to LCD
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(round(temperature));
lcd.print("C / ");
lcd.print(round(temperature * 1.8 + 32));
lcd.print("F "); // padded with space in case C temp drops to single digit
// Print second line to LCD
lcd.setCursor(0,1);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print("% "); // padded with space in case humidity drops to single digit
// Delay at least 1 second because DHT11 sampling rate is 1HZ. Delay longer
// so a person can read the display before we show other data.
delay(DISPLAY_DELAY);
// Read soil moisture
moisture = analogRead(soil) / 10.23; // convert 10-bit number to 0-100 scale so other data isn't too small on plotter
// DEBUG
Serial.print(moisture);
Serial.print("\t");
// Display soil moisture on LCD.
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Soil: ");
lcd.print(moisture);
lcd.print("% "); // padded with space in case moisture drops to single digit
// FINISH DEBUG. Plotter needs line-feed (the "ln" part of the command) to know that the data is finished.
Serial.println();
// If moisture is below threshold, pump the water
if (moisture < MOISTURE_PERCENT_THRESHOLD) {
digitalWrite(pump, HIGH);
delay(PUMP_TIME);
digitalWrite(pump, LOW);
}
// Delay so people can read display
delay(DISPLAY_DELAY);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment