Skip to content

Instantly share code, notes, and snippets.

@alexanderscott
Last active December 29, 2015 14:47
Show Gist options
  • Save alexanderscott/f5df9a37fcbabdb4e78c to your computer and use it in GitHub Desktop.
Save alexanderscott/f5df9a37fcbabdb4e78c to your computer and use it in GitHub Desktop.
Arduino gardening sensors & watering code
#include <LiquidCrystal.h>
#include "DHT.h"
#define DHTTYPE DHT11
template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
/* Enable/Disable Features */
bool enableDhtSensor0 = true;
bool enableDhtSensor1 = false;
bool enableLightSensor0 = true;
bool enableLightSensor1 = false;
bool enableMoistureSensor0 = false;
bool enableMoistureSensor1 = false;
bool enablePump0 = true;
bool enablePump1 = true;
/* Pin Setup */
const int DHT_PIN_0 = 6;
const int DHT_PIN_1 = 7;
const int PUMP_PIN_CATHODE_0 = 8;
const int PUMP_PIN_ANODE_0 = 9;
const int PUMP_PIN_ANODE_1 = 10;
const int PUMP_PIN_CATHODE_1 = 13;
const int MOISTURE_PIN_0 = A0;
const int MOISTURE_PIN_1 = A1;
const int LIGHT_PIN_0 = A2;
const int LIGHT_PIN_1 = A3;
const int PUMP_BTN_PIN_0 = A4;
const int PUMP_BTN_PIN_1 = A5;
/* Library Initialization */
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
if (enableDhtSensor0) {
DHT dht0(DHT_PIN_0, DHTTYPE);
}
if (enableDhtSensor1) {
DHT dht1(DHT_PIN_1, DHTTYPE);
}
/* Variable Measurements */
float lightValue0 = 0;
long int lightSum0 = 0;
float lightValue1 = 0;
long int lightSum1 = 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 */
long READ_DELAY = 60000;
const int SAMPLES_PER_READ = 30;
const int PUMP_WATER_TIME = 10000;
const int SERIAL_PORT = 9600;
void setup() {
delay(2000); //allow lcd to wake up.
lcd.begin(20, 4);
lcd << ".:: RoboGrow ::.";
lcd.setCursor(0, 2);
lcd << "Initializing sensors";
if (enableDhtSensor0) {
dht0.begin();
}
if (enableDhtSensor1) {
dht1.begin();
}
if (enableLightSensor0) {
pinMode(LIGHT_PIN_0, INPUT);
}
if (enableLightSensor1) {
pinMode(LIGHT_PIN_1, INPUT);
}
if (enablePump0) {
pinMode(PUMP_BTN_PIN_0, INPUT);
pinMode(PUMP_PIN_ANODE_0, OUTPUT);
pinMode(PUMP_PIN_CATHODE_0, OUTPUT);
}
if (enablePump1) {
pinMode(PUMP_BTN_PIN_1, INPUT);
pinMode(PUMP_PIN_ANODE_1, OUTPUT);
pinMode(PUMP_PIN_CATHODE_1, OUTPUT);
}
Serial.begin(SERIAL_PORT);
delay(500);
}
void loop() {
resetReadings();
computeLightMoisture();
computeTemp();
printSerial();
printLCD();
checkPumpButtons();
readSerialEvents();
readSerialEvents();
}
void readSerialEvents() {
long beginTime = millis();
unsigned long currentTime = millis();
while (currentTime - beginTime < READ_DELAY) {
if(Serial.available() > 0)
{
String command = Serial.readStringUntil(' ');
String payload = Serial.readStringUntil('\n');
executeSerialCommand(command, payload);
}
delay(50);
currentTime = millis();
}
}
void executeSerialCommand(String cmd, String payload) {
Serial << "Command: " << cmd << ", payload: " << payload << "\r\n";
if (cmd == "WATER_0" && enablePump0) {
int waterTime = payload.toInt();
pumpWater0(waterTime);
} else if (cmd == "WATER_1" && enablePump1) {
int waterTime = payload.toInt();
pumpWater1(waterTime);
} else if(cmd == "WATER") {
int waterTime = payload.toInt();
if (enablePump0) {
pumpWater0(waterTime);
delay(waterTime);
}
if (enablePump1) {
pumpWater1(waterTime);
}
}
}
void checkPumpButtons() {
if (enablePump0) {
pumpBtnState0 = digitalRead(PUMP_BTN_PIN_0);
if (pumpBtnState0 == LOW) {
Serial.println("Pump0 button press");
pumpWater0(PUMP_WATER_TIME);
}
}
if (enablePump1) {
pumpBtnState1 = digitalRead(PUMP_BTN_PIN_1);
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;
lightValue1 = 0;
lightSum1 = 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
if (enableMoistureSensor0) {
moistureSum0 = moistureSum0 + analogRead(MOISTURE_PIN_0);
}
if (enableMoistureSensor1) {
moistureSum1 = moistureSum1 + analogRead(MOISTURE_PIN_1);
}
// Read light
if (enableLightSensor0) {
lightSum0 = lightSum0 + analogRead(LIGHT_PIN_0);
}
if (enableLightSensor1) {
lightSum1 = lightSum1 + analogRead(LIGHT_PIN_1);
}
delay(100);
}
moistureValue0 = moistureSum0 / SAMPLES_PER_READ;
moistureValue1 = moistureSum1 / SAMPLES_PER_READ;
if (enableLightSensor0) {
float lightValue0Tmp = lightSum0 / SAMPLES_PER_READ;
lightValue0 = voltageToLux(lightValue0Tmp);
}
if (enableLightSensor1) {
float lightValue1Tmp = lightSum1 / SAMPLES_PER_READ;
lightValue1 = voltageToLux(lightValue1Tmp);
}
}
// 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() {
if (enableDhtSensor0) {
humidityValue0 = dht0.readHumidity();
tempValue0 = dht0.readTemperature();
tempFahrValue0 = dht0.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 (enableDhtSensor1) {
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(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");
digitalWrite(PUMP_PIN_ANODE_0, HIGH);
digitalWrite(PUMP_PIN_CATHODE_0, LOW);
delay(waterTime);
Serial.println("Deactivating pump0");
digitalWrite(PUMP_PIN_ANODE_0, LOW);
digitalWrite(PUMP_PIN_CATHODE_0, LOW);
//lcd.display();
delay(1000);
}
void pumpWater1(int waterTime) {
//lcd.noDisplay();
//delay(200);
Serial.println("Activating pump1");
digitalWrite(PUMP_PIN_ANODE_1, HIGH);
digitalWrite(PUMP_PIN_CATHODE_1, LOW);
delay(waterTime);
Serial.println("Deactivating pump1");
digitalWrite(PUMP_PIN_ANODE_1, LOW);
digitalWrite(PUMP_PIN_CATHODE_1, LOW);
//lcd.display();
delay(1000);
}
/*
Display Functions
*/
void printSerial() {
if (enableLightSensor0) {
Serial << "light_0," << lightValue0 << "\r\n";
}
if (enableLightSensor1) {
Serial << "light_1," << lightValue1 << "\r\n";
}
if (enableMoistureSensor0) {
Serial << "moisture_0," << moistureValue0 << "\r\n";
}
if (enableMoistureSensor1) {
Serial << "moisture_1," << moistureValue1 << "\r\n";
}
if (enableDhtSensor0 && dht0operational == true) {
Serial << "humidity_0," << humidityValue0 << "\r\n";
Serial << "temperature_0," << tempFahrValue0 << "\r\n";
Serial << "heat_0," << heatValue0 << "\r\n";
}
if (enableDhtSensor1 && 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 l1 = (int) lightValue1;
int he0 = (int) heatValue0;
int he1 = (int) heatValue1;
lcd.clear();
delay(200);
lcd.setCursor(0, 0);
lcd << "MSTR 0:" << moistureValue0 << " 1:" << moistureValue1;
lcd.setCursor(0, 1);
lcd << "TEMP 0:" << t0 << "*F 1:" << t1 << "*F";
lcd.setCursor(0, 2);
lcd << "HUM 0:" << hu0 << "% 1:" << hu1 << "%";
lcd.setCursor(0, 3);
lcd << "LIT 0:" << l0 << " 1:" << l1;
}
/*
Conversion Functions
*/
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