Hydroponics Control with Blynk

My fellow friends, I am using Arduino Uno R3 with ESP-13 Esp8266 shield.
I am trying to implement simple hydroponics setup with
-DHT11 sensor
-Arduino Relay shield
-Water level sensor
-Water temp sensor
-Air pump
-Water Pump

The sketch uses RTC data from Blynk server; compares current time with prespecified value and implements instructions.

The below code gets verified with no errors, and after uploading; ESP-13 connects to Wi-fi with no problems.
However after that the sketch doesn’t respond, doesnt read sensor values and etc…

I realized that :
When I commented out random one function inside “Dailyroutine”; everything works perfect. It doesnt matter which function is commented out.

How can i solve this problem?


#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include "DHT.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SoftwareSerial.h>

// Your WiFi credentials.
// Set password to "" for open networks.





// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1

// or Software Serial on Uno, Nano...

SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600
// DHT type
#define DHTTYPE DHT11
//



//define PINS
#define airValve 9 //output
#define waterPump 10 //output
#define airVantilator 11 //output
#define airHumidifier 12 //output

#define ONE_WIRE_BUS_1 8 // water temp input
#define DHTPIN 7 // air temp and humidity input
#define WETSENSOR 6 //rain sensor input



//outsourced class function calls

OneWire oneWire_in(ONE_WIRE_BUS_1);
DallasTemperature sensor_inhouse(&oneWire_in);
DHT dht(DHTPIN, DHTTYPE);

float t;
float h;

//MorningSchedhule
int MorningAirOnHour = 14;
int MorningAirOnMinute = 30;
int MorningWaterOnHour = 14;
int MorningWaterOnMinute = 31;
int MorningAirWaterOffHour=14;
int MorningAirWaterOffMinute= 32;
int MorningAirOpened = 0;
int MorningWaterOpened = 0;

//NoonSchedhule
int NoonAirOnHour = 14;
int NoonAirOnMinute = 25;
int NoonWaterOnHour = 14;
int NoonWaterOnMinute = 26;
int NoonAirWaterOffHour=14;
int NoonAirWaterOffMinute=27;
int NoonAirOpened = 0;
int NoonWaterOpened = 0;


//EveningSchedhule
int EveningAirOnHour = 15;
int EveningAirOnMinute = 30;
int EveningWaterOnHour = 15;
int EveningWaterOnMinute = 31;
int EveningAirWaterOffHour=15;
int EveningAirWaterOffMinute=32;
int EveningAirOpened = 0;
int EveningWaterOpened = 0;


float waterTemp; // water temperature

ESP8266 wifi(&EspSerial);

BlynkTimer timer;

WidgetRTC rtc;





BLYNK_CONNECTED() {
  // Synchronize time on connection
  rtc.begin();
}

void setup()
{

  t=0;
  h=0;
  // Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  
  Blynk.begin(auth, wifi, ssid, pass);
  
  setSyncInterval(10*60); // Sync interval in seconds (1 week)


  digitalWrite(airValve,HIGH);// initially off
  digitalWrite(waterPump,HIGH); //initially off
  digitalWrite(airVantilator,HIGH);//initially off
  digitalWrite(airHumidifier,HIGH);//initially off

  
  pinMode(airValve, OUTPUT);
  pinMode(waterPump, OUTPUT);
  pinMode(airVantilator, OUTPUT);
  pinMode(airHumidifier, OUTPUT);
  sensor_inhouse.begin();
  dht.begin();
  pinMode(WETSENSOR, INPUT);
  




    // Display digital clock every 1 seconds
    timer.setInterval(1000L, DailyRoutine);
  //timer.setInterval(12000L, waterStatusRead);
  

  
}
void OpenAirValve() {
  Serial.println("OpeningAirValve");
  digitalWrite(airValve,LOW);
}
void CloseAirValve() {
  Serial.println("ClosingAirValve");
  digitalWrite(airValve,HIGH);
}
void OpenWaterPump() {
  Serial.println("OpeningWaterPump");
  digitalWrite(waterPump,LOW);
}
void CloseWaterPump() {
  Serial.println("ClosingWaterPump");
  digitalWrite(waterPump,HIGH);
}
void DailyRoutine(){
  
  ambientTempHumidityRead();
  waterStatusRead();
  //clockDisplay();
  MorningRoutine();
  NoonRoutine();
  EveningRoutine();
   

}





void MorningRoutine() {

  
  
  if( (hour() == MorningAirOnHour) && (minute() == MorningAirOnMinute) && (MorningAirOpened ==0))
  {
    OpenAirValve();
    MorningAirOpened = 1;
  }

  
   if( (hour() == MorningAirWaterOffHour) && (minute() == MorningAirWaterOffMinute) && ((MorningAirOpened && MorningWaterOpened)  ==1))
  {
    CloseAirValve();
    CloseWaterPump();    
    MorningAirOpened = 0;
    MorningWaterOpened = 0;
  }
  
  
  if( (hour() == MorningWaterOnHour) && (minute() == MorningWaterOnMinute) && (MorningWaterOpened ==0))
  {
    OpenWaterPump();
    MorningWaterOpened = 1;
  }  
  
}

void NoonRoutine() {
  if( (hour() == NoonAirOnHour) && (minute() == NoonAirOnMinute) && (NoonAirOpened ==0))
  {
    OpenAirValve();
    NoonAirOpened = 1;
  }

  
   if( (hour() == NoonAirWaterOffHour) && (minute() == NoonAirWaterOffMinute) && ((NoonAirOpened && NoonWaterOpened)  ==1))
  {
    CloseAirValve();
    CloseWaterPump();    
    NoonAirOpened = 0;
    NoonWaterOpened = 0;
  }
  
  
  if( (hour() == NoonWaterOnHour) && (minute() == NoonWaterOnMinute) && (NoonWaterOpened ==0))
  {
    OpenWaterPump();
    NoonWaterOpened = 1;
  }  
  
}

void EveningRoutine() {
  if( (hour() == EveningAirOnHour) && (minute() == EveningAirOnMinute) && (EveningAirOpened ==0))
  {
    OpenAirValve();
    EveningAirOpened = 1;
  }

  
   if( (hour() == EveningAirWaterOffHour) && (minute() == EveningAirWaterOffMinute) && ((EveningAirOpened && EveningWaterOpened)  ==1))
  {
    CloseAirValve();
    CloseWaterPump();    
    EveningAirOpened = 0;
    EveningWaterOpened = 0;
  }
  
  
  if( (hour() == EveningWaterOnHour) && (minute() == EveningWaterOnMinute) && (EveningWaterOpened ==0))
  {
    OpenWaterPump();
    EveningWaterOpened = 1;
  }  
  
}

void waterTempRead()
{
  waterTemp = sensor_inhouse.getTempCByIndex(0);
  //Blynk.virtualWrite(V4,waterTemp);
  Serial.print("Water temperature = ");
  Serial.println(waterTemp); 



    
}


void waterLevelRead()
{
     if (digitalRead(WETSENSOR) == LOW)
    {
     //Blynk.virtualWrite(V5,255);
     //Blynk.setProperty(V5,"color","#23C48E");
     Serial.println("Water level OK");
      
     
    }

  else{
   //  Blynk.setProperty(V5,"color","#D3435C"); 
     Serial.println("Warning! Water is LOW"); 
    }
 
}

void waterStatusRead()
{
     waterTempRead();
     waterLevelRead();
    
}

void clockDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details

  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();

}

void ambientTempHumidityRead()
{
   // Read temperature as Celsius (the default)
  t = dht.readTemperature();
  h = dht.readHumidity();


  
   if (isnan(h) || isnan(t) ) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));   
  Serial.print(F("         Humidity: "));
  Serial.println(h);

}


void loop()
{
  Blynk.run();
  timer.run();
 
  
  

  

}

So, once every second you are doing the following things:

  • Checking the temperature and humidity
  • Checking the water temperature
  • Checking the water level
  • Checking it it’s morning
  • Checking if it’s noon
  • Checking if it’s evening

In addition, your check morning, noon and evening routines are structured in a way that the code has to evaluate three if statements in each routine, rather than simply skipping the rest of the if tests if the first if statement evaluates as false.

Do you think that’s good programming practice?

Pete.

Hi Pete,
Thanks, I get what you mean. I increased time checking interval to 5 mins with no success.
I still get the same result.
What approach can you recommend to check whether it is time to open and close?

I’d use multiple timers, and in one of them do a check to see if the minute has just rolled-over from 59 to 0 then if it has check if any of the morning, noon or evening timer star times is true and run the appropriate function of it is.

Pete.