Skip to content

Instantly share code, notes, and snippets.

@alexanderscott
Last active August 29, 2015 14:16
Show Gist options
  • Save alexanderscott/e24d3bfacde56fa863a2 to your computer and use it in GitHub Desktop.
Save alexanderscott/e24d3bfacde56fa863a2 to your computer and use it in GitHub Desktop.
Arduino gardening sensors & watering code
/* Imports & Definitions */
#include <LiquidCrystal.h>
#include "DHT.h"
#include <MemoryFree.h>
#include <SerialReceiver.h>
#define DHTTYPE DHT11
template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
/* Pin Setup */
const int DHT_PIN_0 = 6;
const int DHT_PIN_1 = 7;
const int PUMP_PIN_0 = 9;
const int PUMP_PIN_1 = 10;
const int MOISTURE_PIN_0 = A0;
const int MOISTURE_PIN_1 = A1;
const int LIGHT_PIN_0 = A2;
const int PUMP_BTN_PIN_0 = A4;
const int PUMP_BTN_PIN_1 = A5;
/* Library Initialization */
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
DHT dht0(DHT_PIN_0, DHTTYPE);
DHT dht1(DHT_PIN_1, DHTTYPE);
/* Variable Measurements */
float lightValue0 = 0;
long int lightSum0 = 0;
int moistureValue0 = 0;
long int moistureSum0 = 0;
int moistureValue1 = 0;
long int moistureSum1 = 0;
bool dht0operational = true;
float humidityValue0 = 0;
float tempValue0 = 0;
float tempFahrValue0 = 0;
float heatValue0 = 0;
bool dht1operational = true;
float humidityValue1 = 0;
float tempValue1 = 0;
float tempFahrValue1 = 0;
float heatValue1 = 0;
int pumpBtnState0 = 0;
int pumpBtnState1 = 0;
/* Constants / Configuration */
const int READ_DELAY = 10000;
const int SAMPLES_PER_READ = 30;
const int PUMP_WATER_TIME = 10000;
//const long int PUMP_WATER_WAIT_TIME = 86400000;
const int SERIAL_PORT = 9600;
void setup() {
delay(2000); //allow lcd to wake up.
lcd.begin(20, 4);
lcd << "=== iGrow ===" << "\r\n";
lcd << "Initializing sensors..." << "\r\n";
dht0.begin();
dht1.begin();
pinMode(LIGHT_PIN_0, INPUT);
pinMode(PUMP_BTN_PIN_0, INPUT);
pinMode(PUMP_BTN_PIN_1, INPUT);
pinMode(PUMP_PIN_0, OUTPUT);
pinMode(PUMP_PIN_1, OUTPUT);
Serial.begin(SERIAL_PORT);
IncomingCommand::reset();
delay(500);
}
void loop() {
resetReadings();
computeLightMoisture();
computeTemp();
printSerial();
printLCD();
checkPumpButtons();
readSerialEvents();
readSerialEvents();
}
void readSerialEvents() {
IncomingCommand::reset();
unsigned long beginTime = millis();
while ((millis() - beginTime) < READ_DELAY) {
if (Serial.available()) {
char inChar = (char) Serial.read();
IncomingCommand::append(inChar);
if (IncomingCommand::isReady) {
executeSerialCommand(IncomingCommand::command, IncomingCommand::payload);
IncomingCommand::reset();
}
}
delay(50);
}
}
void executeSerialCommand(String cmd, String payload) {
Serial << "Command: " << cmd << ", payload: " << payload << "\r\n";
if (cmd == "WATER_0") {
int waterTime = payload.toInt();
pumpWater0(waterTime);
} else if (cmd == "WATER_1") {
int waterTime = payload.toInt();
pumpWater1(waterTime);
} else if(cmd == "WATER") {
int waterTime = payload.toInt();
pumpWater0(waterTime);
delay(10000);
pumpWater1(waterTime);
}
}
void checkPumpButtons() {
pumpBtnState0 = digitalRead(PUMP_BTN_PIN_0);
pumpBtnState1 = digitalRead(PUMP_BTN_PIN_1);
if (pumpBtnState0 == LOW) {
Serial.println("Pump0 button press");
pumpWater0(PUMP_WATER_TIME);
}
if (pumpBtnState1 == LOW) {
Serial.println("Pump1 button press");
pumpWater1(PUMP_WATER_TIME);
}
}
// Reset readings back to 0 for next loop
void resetReadings() {
moistureValue0 = 0;
moistureSum0 = 0;
moistureValue1 = 0;
moistureSum1 = 0;
humidityValue0 = 0;
tempValue0 = 0;
tempFahrValue0 = 0;
heatValue0 = 0;
humidityValue1 = 0;
tempValue1= 0;
tempFahrValue1 = 0;
heatValue1 = 0;
lightValue0 = 0;
lightSum0 = 0;
pumpBtnState0 = 0;
pumpBtnState1 = 0;
}
// Calculate light & moisture readings from sample avg
void computeLightMoisture() {
for(int i = 0; i < SAMPLES_PER_READ; i++) {
// Read moisture
moistureSum0 = moistureSum0 + analogRead(MOISTURE_PIN_0);
moistureSum1 = moistureSum1 + analogRead(MOISTURE_PIN_1);
// Read light
lightSum0 = lightSum0 + analogRead(LIGHT_PIN_0);
delay(100);
}
moistureValue0 = moistureSum0 / SAMPLES_PER_READ;
moistureValue1 = moistureSum1 / SAMPLES_PER_READ;
float lightValue0Tmp = lightSum0 / SAMPLES_PER_READ;
lightValue0 = voltageToLux(lightValue0Tmp);
}
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
void computeTemp() {
humidityValue0 = dht0.readHumidity();
tempValue0 = dht0.readTemperature();
tempFahrValue0 = dht0.readTemperature(true); // Read temperature as Fahrenheit
humidityValue1 = dht1.readHumidity();
tempValue1 = dht1.readTemperature();
tempFahrValue1 = dht1.readTemperature(true); // Read temperature as Fahrenheit
// Check if any reads failed and exit early (to try again).
if (isnan(humidityValue0) || isnan(tempValue0) || isnan(tempFahrValue0)) {
Serial.println("Failed to read from DHT0 sensor!");
dht0operational = false;
} else {
heatValue0 = dht0.computeHeatIndex(tempFahrValue0, humidityValue0);
dht0operational = true;
}
if (isnan(humidityValue1) || isnan(tempValue1) || isnan(tempFahrValue1)) {
Serial.println("Failed to read from DHT1 sensor!");
dht1operational = false;
} else {
heatValue1 = dht1.computeHeatIndex(tempFahrValue1, humidityValue1);
dht1operational = true;
}
}
void pumpWater0(int waterTime) {
//lcd.noDisplay();
//delay(200);
Serial.println("Activating pump0");
analogWrite(PUMP_PIN_0, 255);
//digitalWrite(PUMP_PIN_0, HIGH);
delay(waterTime);
Serial.println("Deactivating pump0");
analogWrite(PUMP_PIN_0, 0);
//digitalWrite(PUMP_PIN_0, LOW);
//lcd.display();
delay(1000);
}
void pumpWater1(int waterTime) {
//lcd.noDisplay();
//delay(200);
Serial.println("Activating pump1");
analogWrite(PUMP_PIN_1, 255);
//digitalWrite(PUMP_PIN_1, HIGH);
delay(waterTime);
Serial.println("Deactivating pump1");
analogWrite(PUMP_PIN_1, 0);
//digitalWrite(PUMP_PIN_1, LOW);
//lcd.display();
delay(1000);
}
/*
Display Functions
*/
void printSerial() {
// print how much RAM is available.
Serial.print("Free RAM: ");
Serial.println(freeMemory(), DEC);
Serial << "light_0," << lightValue0 << "\r\n";
Serial << "moisture_0," << moistureValue0 << "\r\n";
Serial << "moisture_1," << moistureValue1 << "\r\n";
if (dht0operational == true) {
Serial << "humidity_0," << humidityValue0 << "\r\n";
Serial << "temperature_0," << tempFahrValue0 << "\r\n";
Serial << "heat_0," << heatValue0 << "\r\n";
}
if (dht1operational == true) {
Serial << "humidity_1," << humidityValue1 << "\r\n";
Serial << "temperature_1," << tempFahrValue1 << "\r\n";
Serial << "heat_1," << heatValue1 << "\r\n";
}
}
void printLCD() {
int hu0 = (int) humidityValue0;
int hu1 = (int) humidityValue1;
int t0 = (int) tempFahrValue0;
int t1 = (int) tempFahrValue1;
int l0 = (int) lightValue0;
int he0 = (int) heatValue0;
int he1 = (int) heatValue1;
lcd.clear();
delay(200);
lcd.setCursor(0, 0);
lcd << "M0: " << moistureValue0 << " M1: " << moistureValue1;
lcd.setCursor(0, 1);
lcd << "T0: " << t0 << "*F T1: " << t1 << "*F";
lcd.setCursor(0, 2);
lcd << "HU0: " << hu0 << "% H01: " << hu1 << "%";
lcd.setCursor(0, 3);
lcd << "L1: " << l0;
}
float voltageToLux(float photocellReading) {
// Calculating the voltage in the input of the ADC
float voltage = 5.0 * (photocellReading / 1024.0);
// Calculating the resistance of the photoresistor
// in the voltage divider
float resistance = (10.0 * 5.0) / voltage - 10.0;
// Calculating the intensity of light in lux
float illuminance = 255.84 * pow(resistance, -10/9);
// Converting the intensity of light to text
//sprintf(text, "%0.1f lux ", illuminance);
return illuminance;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment