Page 1 of 1

Plant monitor

Posted: Tue Jan 05, 2016 5:41 pm
by DylanM
Hey, New project which I've been working on over the holidays. It's a box that tells me how my pet plant chilli is going by lighting up different led's it also works as a bluetooth weather device which I can connect my phone to to check humidity and temperature. I have done some code that I think should work and is running on it right now. I was wondering if there is a way to make it simpler? :?:

#include "DHT.h"


#define DHTPIN 6

#define DHTTYPE DHT22
int lightLevel;
DHT dht(DHTPIN, DHTTYPE);
int WaterMoi;

void setup() {
Serial.begin(9600);
dht.begin();
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}

void loop() {
String command = "";
float h = dht.readHumidity();
float t = dht.readTemperature();
lightLevel = analogRead(A0);
WaterMoi = analogRead(2);
command = Serial.readString();
if (command == "Light?"){
Serial.print("Light Level ");
Serial.println(lightLevel, DEC);
}

if (isnan(t) || isnan(h)) {
Serial.println("FAIL");
} else if (command == "Humidity?"){
Serial.print("Humidity: ");
Serial.print(h);
Serial.println(" %\t");
}
if (command == "Temperature?"){
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
}
if (command == "Moisture?"){
Serial.print("Moisture: ");
Serial.println(WaterMoi);
}
if (WaterMoi <= 500){
digitalWrite(10, HIGH);
delay(1000);
digitalWrite(10, LOW);
delay(1000);
}
if (WaterMoi >= 500, WaterMoi <= 1000){
digitalWrite(9, HIGH);
}
if (lightLevel <= 20){
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
delay(1000);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
delay(1000);
}
if (t >= 40){
digitalWrite(8, HIGH);
delay(1000);
digitalWrite(8, LOW);
delay(1000);
}
}
Thanks Dylan

Re: Plant monitor

Posted: Thu Jan 07, 2016 2:36 pm
by seaton
Looks like a great start start.

A couple of suggestions :

1) Look at averaging your readings rather than reporting the raw value, as reporting the raw readings directly can give you a bit of a noise on the output, this can be done two ways, do a running average over the consecutive readings or each time you need to report read the sensor say 10 times and average the results and report the average. I do this for sensors like temperature, humidity etc this way http://blog.strobotics.com.au/2009/06/1 ... se-filter/

2) Is the device battery powered? if so then look at the Arduino sleep modes and timer interrupt, i.e. after reading sensors and reporting put device in deep sleep, also look how the bluetooth module can be put into a low power mode. look at the timer IRQ or an input IRQ to wake up periodically and do things, i.e. if it needs to do next reading, or listen on the BT. This will get rid of a lot of your delay functions. (this probably won't help much if you have LED lights on tho)

3) I've used in the past a library called metro to do scheduled tasks (http://playground.arduino.cc/Code/Metro) i.e. read sensor every x seconds

e.g.

// Global timers
Metro secTimer = Metro(1000); // 1 Second timer
Metro sec5Timer = Metro(5000) ; // 5 Second timer

//then in your main loop
if(sec5Timer.check()==1){ // check 5 Second timer
battery.update(); // read battery voltage
}//min5Timer

4) Also it's good programming practice that if you do it more than once in code then create a function, then call this function in your code when needed. This way you only need to update it once, i.e. in your function and it will be reflected throughout your code, this avoided introducing errors if at some point down the track you need to change something and forget to change one section because you missed it.

Stephen...

Re: Plant monitor

Posted: Sat Jan 09, 2016 1:08 pm
by DylanM
Thanks for the suggestions I will probably look at the metro library and arduino sleep because I am running off a battery. Also at the moment I am using an rgb led to show me visually what my plant needs and I have decided to change from bluetooth to wifi eventually. :)