Close

Firmware Completion

A project log for Hydroponics control

Automated control of hydroponics Garden with Control Via Blynk

saabmanSaabman 12/26/2017 at 10:590 Comments

A good run today saw me complete the Firmware, no doubt modifications will be coming but things have progressed enough to move on to the Hardware.

The code is listed below.
from the App it is possible to adjust the setpoints for the Temperature, Humidity and Moisture level pressing the save button stores the values in the EEPROM of the ESP8266 which is read on start up should the 8266 ever be rebooted.

/*************************************************************
  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Blynk community:            http://community.blynk.cc
    Social networks:            http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license

 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#include <EEPROM.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxx";
char ssid[] = "xxxx";
char pass[] = "xxxx";
bool level = 0;
int fan;
int mist;
int pump;
int lowWater;
int moisture = 0;
int dryLevel = 100;
int maxTemp = 30;
int minHumidity = 50;
float humidity = 0;
float temperature = 0;

#define DHTPIN D2          // What digital pin we're connected to
#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;


void readInputs()
{
  digitalWrite(D8,HIGH);                // turns on Moisture sensor
  humidity = dht.readHumidity();        // Reads Humidity
  temperature = dht.readTemperature();  // Reads Temperature
  lowWater = digitalRead(D1);           // Read Low  water level sensor
  moisture = analogRead(A0)/4;          //read moisture sensor scales value to have a max of 255 for easy storage in EEPROM
  digitalWrite(D8,LOW);                 // turns off moisture sensor

  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
}


void reportToBlynk()
//Sends data values to Blynk
{
  // Sets sliders to values from Controller
  Blynk.virtualWrite(V1, dryLevel);
  Blynk.virtualWrite(V2, minHumidity);
  Blynk.virtualWrite(V3, maxTemp);
  // Display Values 
  Blynk.virtualWrite(V5, humidity);
  Blynk.virtualWrite(V6, temperature);
  Blynk.virtualWrite(V7, moisture);
  Blynk.virtualWrite(V4, moisture);

  // Satus LED's
  Blynk.virtualWrite(V12, fan);         // Controls LEDs in App to indicate
  Blynk.virtualWrite(V11, mist);        // status of Pumps and fans etc
  Blynk.virtualWrite(V10, pump);
  Blynk.virtualWrite(V9, lowWater*255);     // Low Water warning indicator. Reading the input pin returns 1 but need to scale up to 255 to drive the BLYNK LED widget to full brightnes so * 255

  
}

BLYNK_WRITE(V1)
  /*  gets called when Slider attched to virtual pin 1 in the Blynk app changes
      sets the value of the sider to drylevel. This is the setpoint level for the
      activation of the flood pump.
  */   
{
  dryLevel = param.asInt();           // assigning incoming value from pin V1 as an int to the dryLevel variable
  Serial.print ("Moisture set ");
  Serial.println (dryLevel);
  EEPROM.write(1,dryLevel);           //Stores the value of drylevel to EEPROM
}

BLYNK_WRITE(V2)
  /* 
   * sets the min Humidity level for the mister to turn on 
   */
{
  minHumidity =param.asInt();
  Serial.print ("Humidty ");
  Serial.println (minHumidity);
  EEPROM.write(2,minHumidity);           //Stores the value of minHumidity to EEPROM
}

BLYNK_WRITE(V3)
/*
 * sets the tempertaure level for the fans to come on
 */
{
  maxTemp = param.asInt();
  Serial.print ("Temp Set ");
  Serial.println ( maxTemp);
  EEPROM.write(3,maxTemp);           //Stores the value of maxTemp to EEPROM
}

BLYNK_WRITE(V8)
/*
 * Loads settings into EEPROM on Button press
 */
{
  EEPROM.commit();
}
 
void controlOutputs()

/*  Turns on watre pump to irrigate the grow bed
    Turns on fans to reduce temperature
    Turns on mister to increase humidity
*/

{
  // GROW BED Moisture
   if (moisture < dryLevel) {         //  compares the mositure level of the bed with the set point value
    digitalWrite(D5, HIGH);           //  If the bed is too dry then turn on the flodd pump and irrigate the bed
    pump = 255;                      //  Sets Pump status
   }
    else {
      digitalWrite(D5, LOW);          //  If the bed is moist - no need to turn on the pump.
      pump = 0;
   }
    
  // Ambient Temperature
   if (temperature > maxTemp){
    digitalWrite(D6, HIGH);          //  Checks to see if Tempertaure is too high 
    fan = 255;
   }                                  //  If the Temp is too High turn on the exhaust fan
    else {
      digitalWrite(D6, LOW);          //  If its no to high turn the fan is off
      fan = 0;
    }

  //  Humidity Control
   if (humidity < minHumidity){       //  Checks to see if Humidty is too low
    digitalWrite(D7, HIGH);           //  If its too low turns on mister
    mist = 255;
   }
    else {
      digitalWrite(D7, LOW);          //  If its high enough turn the mister off
      mist = 0;
    }
}




void setup()
{

  EEPROM.begin(512);    // Initialise EEPROM for storing values
  
  // Set Output pins to outputs
  
  pinMode(D5, OUTPUT);  // Moisture sensor
  pinMode(D6, OUTPUT);  // Flood Pump
  pinMode(D7, OUTPUT);  // Fans
  pinMode(D8, OUTPUT);  // Mister
  
  // Debug console
  Serial.begin(9600);
  
  // Set up Blynk
  Blynk.begin(auth, ssid, pass);

  // Set up Humidty and temp sensor 
  dht.begin();

  //Read default parameters from EEProm
  dryLevel = EEPROM.read(1);
  minHumidity = EEPROM.read(2);
  maxTemp = EEPROM.read(3);

  Serial.println(dryLevel);
  Serial.println(minHumidity);
  Serial.println(maxTemp);

  // Setup a function to be called every second
  // this is to limit reporting to Blynk to only once a second
  timer.setInterval(1000L, reportToBlynk);
}


void loop()
{
  readInputs();
  controlOutputs();
  Blynk.run();
  timer.run(); // Initiates SimpleTimer

Discussions