Project Autus - Automated Plant Chamber

Dependencies:   TextLCD mbed

Fork of keypad_test by Plamen Totev

Autus

This is the codebase accompanying the project Autus.

Autus is an automated growth chamber for plants.

Features

Control Humidity inside chamber wrt to external humidity. Control Temperature inside chamber. ( Peltier Heaters/Coolers ) Water and shower plants. Control soil humidity. Monitor water tanks level (Load Cell) /media/uploads/umairaftab/frdm_-_new_page1.png

Code Base Features

Fixed timing and CRC for DHT-11 Sensor. Fixed OneWire bug for ds18b20

Cyclic Executive Scheduler with Priority. Async IPC framework for PC App over bluetooth

Fake RTC systick, I was having some trouble with the on board rtc.

/media/uploads/umairaftab/download.png

main.cpp

Committer:
umairaftab
Date:
2014-04-12
Revision:
13:6bf09cd68013
Parent:
12:b3137bb72ef7
Child:
14:72176f1e4907

File content as of revision 13:6bf09cd68013:


//QUICK REFS:
// TICKER is using timer 1 for buzzer. 

#include "mbed.h"

#include "keypad.h"
#include "buzz.h"

#include "pindefs.h" //led pin definitions
#include "led_lights.h"

#include "peltier.h"
#include "peltier_pindefs.h"

#include "pumps.h"
#include "pumps_pindefs.h"

#include "airhumidity.h"
#include "soilmoisture.h"
#include "soilmoisture_pindefs.h"

#include "ui.h"
#include "ui_pindefs.h"
#include "TextLCD.h"

#include "rtcimp.h"

#include <string>
using namespace std;

//SERIAL
Serial bluetooth(PTA2, PTA1);  // tx, rx
Serial printer(PTC4,PTC3);  // tx, rx
//TICKERS
Ticker timer1ms;
Ticker clock_mine;
Timer lcd_timeout;
//FLAGS
bool startup_flag=true; 
bool watered_plant1 = false;
int watered_plant1_time = 0; 
bool watered_plant2 = false;
int watered_plant2_time = 0; 

//SETPOINTS
float setpoint_air_humidity = 30.00 ;
float setpoint_soil_humid1 = 0.3 ;
float setpoint_soil_humid2 = 0.3 ;

float setpoint_MAX_air_temp = 45;

int setpoint_red_lights;
int setpoint_blue_lights;
int setpoint_green_lights;

//WINDOWING
float window_f = 5 ;
int window_i =2 ;

//GLOBAL VARS
float current_water_level = 0; 
bool lcd_timeout_flag=false;

//SAFETY LIMITS
const float max_peltier_temp = 68 ; //CELCIUS
const int MAX_WATERING_TIME_GAP = 20; //Number of Hours
const int CYCLE_AIR_TIME = 2; //Wait between on and off seconds
const int LCD_TIMEOUT_VALUE = 30; //Number of seconds


//**************************FUNCTION TO READ SENSORS ********************************************** 
void read_sensors(){
    
    //Read values for AIR
    
    //OUTSIDE
    outside_humidity = get_air_humid_outside();
    outside_temp = get_air_temp_outside(temp_unit); 
    outside_dewpoint = get_air_dewpoint_outside();
    
    //INSIDE
    inside_humidity = get_air_humid_inside();
    inside_temp = get_air_temp_inside(temp_unit); 
    inside_dewpoint = get_air_dewpoint_inside();
    
    //Read value for soil
    read_soil_humidity();
    calc_soil_humid_values(); //values in soil1_humid and soil2_humid perecentages
    soil1_humidity = soil1_humid ;
    soil2_humidity = soil2_humid ;
    printf("%4.2f \n\r",soil1_humidity);
}

//****************************Functions that perform tasks**************************************
//Water Plants
void waterplants(){
    
    
    if(startup_flag == true){
        //call for pump function 
        watered_plant1 = true;
        watered_plant1_time = hour ; 
        watered_plant2 = true;
        watered_plant2_time = hour ; 
    }
    else{
        
        if( (soil1_humid < setpoint_soil_humid1) || (watered_plant1 == false) ){ 
            //enable pump1
            watered_plant1 = true;
            watered_plant1_time = hour ;
        }
        if( (soil2_humid < setpoint_soil_humid2) || (watered_plant2 == false) ){ 
            //enable pump2
            watered_plant2 = true;
            watered_plant2_time = hour ;
        }
    }
    //Make sure it is watered at least every x hours in case soil humid doesnt work
    if (watered_plant1 == true){
        if(abs(hour-watered_plant1_time)>MAX_WATERING_TIME_GAP){
            watered_plant1 = false;
        }
    }   
    if (watered_plant2 == true){
        if(abs(hour-watered_plant2_time)>MAX_WATERING_TIME_GAP){
            watered_plant2 = false;
        }
    } 
    
}

// Air Humidity Level
void airhumidity(){
    
    if( !( (setpoint_air_humidity+window_f > inside_humidity) && (setpoint_air_humidity-window_f < inside_humidity) ) ){
    
        //Turn on Air humidity fan 
        wait(5);
        //Turn off Air humisity fan
    }   
}

//CYCLE AIR 
void cycleair(){
    
    //Turn on fan 
    wait(CYCLE_AIR_TIME);
    //turn of fan 
    
}
//LCD TIMEOUT FUNCTION
void lcd_timeout_check(){
    
    if(lcd_timeout.read()>LCD_TIMEOUT_VALUE){
            lcd_timeout_flag = true;
        }
    
}
//SETTINGS UI 
void ui_settings(){
    
    lcd.cls();
    lcd.locate(0,0);
    lcd.printf("Autus Settings");
    lcd.locate(0,1);
    lcd.printf("Performing Checks");
    char key_value;
    bool exit_settings=false;
    
    //disable peltier
    
    wait(2);
    string menu[4];
    menu[0]= "1.LED Brightness";
    menu[1]= "2.Air Humidty Lvl";
    menu[2]= "3.Soil Moisture Lvl";
    menu[3]= "4.Air Temperature";
    
    lcd.cls();
    int counter_menu =0 ; 
    while(!exit_settings){
        key_value = Keypad();
        if(key_value == 100){
            lcd.locate(0,0);
            lcd.printf(">%s",menu[counter_menu].c_str());
            lcd.locate(0,1);
            if(counter_menu ==3){
            counter_menu = 0;
            lcd.printf(" %s",menu[counter_menu].c_str());
            }
            else{
            lcd.printf(" %s",menu[counter_menu+1].c_str());
            }
            wait(0.2); 
        }//No Press If Block
        else if(key_value == 44){//Down Key Pressed if block
            lcd.cls();
            counter_menu++;
        }//Down Key Pressed if block end
        else if(key_value == 41){ //OK Key Pressed if block
            char key_value2;
            bool break_flag = false;
            switch(counter_menu){
                case 0:
                    lcd.cls();
                    lcd.locate(0,0);
                    lcd.printf("Not Authorised");
                    wait(5);
                    break;
                case 1:
                    break_flag = false;
                    lcd.cls();
                    lcd.locate(0,0);
                    lcd.printf("Cur Air Humidity Lvl");
                    //lcd.locate(0,1);
                    //lcd.printf("%3.1f",setpoint_air_humidity);
                    while(!break_flag){
                        lcd.locate(0,1);
                        lcd.printf("%3.1f",setpoint_air_humidity);
                        key_value2 = Keypad();
                        wait(0.2);
                        if(key_value2 == 50){setpoint_air_humidity++;}
                        else if(key_value2 == 47){setpoint_air_humidity--;}
                        else if(key_value2 == 51){break_flag=true;}
                        if (setpoint_air_humidity <0 || setpoint_air_humidity >99) {setpoint_air_humidity=30.00;}
                    }
                    
                    lcd.cls();
                    lcd.locate(0,0);
                    lcd.printf("NEW Air Humid Setpoint");
                    lcd.locate(0,1);
                    lcd.printf("%3.1f",setpoint_air_humidity);
                    wait(5);
                    
                    break;
                case 2:
                    break_flag = false;
                    lcd.cls();
                    lcd.locate(0,0);
                    lcd.printf("Soil Humidity Setpoint");
                    
                    while(!break_flag){
                        lcd.locate(0,1);
                        lcd.printf("S1: %4.2f",setpoint_soil_humid1);
                        lcd.printf(" S2: %4.2f",setpoint_soil_humid2);
                        key_value2 = Keypad();
                        wait(0.2);
                        if(key_value2 == 50){
                            setpoint_soil_humid1++;
                            setpoint_soil_humid2++;
                        }
                        else if(key_value2 == 47){
                            setpoint_soil_humid1--;
                            setpoint_soil_humid2--;
                        }
                        else if(key_value2 == 51){break_flag=true;}
                        //if (setpoint_air_humidity <0 || setpoint_air_humidity >99) {setpoint_air_humidity=30.00;}
                    }
                    
                    lcd.cls();
                    lcd.locate(0,0);
                    lcd.printf("New Soil Setpoints:");
                    lcd.locate(0,1);
                    lcd.printf("S1: %4.2f",setpoint_soil_humid1);
                    lcd.printf(" S2: %4.2f",setpoint_soil_humid2);
                    wait(5);
                    break;
                case 3:
                
                    break_flag = false;
                    lcd.cls();
                    lcd.locate(0,0);
                    lcd.printf("Cur Air Tmp Setpoint");
                    while(!break_flag){
                        lcd.locate(0,1);
                        lcd.printf("%3.1f",setpoint_MAX_air_temp);
                        key_value2 = Keypad();
                        wait(0.2);
                        if(key_value2 == 50){setpoint_MAX_air_temp++;}
                        else if(key_value2 == 47){setpoint_MAX_air_temp--;}
                        else if(key_value2 == 51){break_flag=true;}
                        if (setpoint_MAX_air_temp <0 || setpoint_MAX_air_temp >99) {setpoint_MAX_air_temp=45.0;}
                    }
                    
                    lcd.cls();
                    lcd.locate(0,0);
                    lcd.printf("NEW Air Tmp Setpoint");
                    lcd.locate(0,1);
                    lcd.printf("%3.1f",setpoint_MAX_air_temp);
                    wait(5);
                    break;
            }//switch end
        } //OK button if block
        else if(key_value == 57){
            exit_settings = true;
            lcd.cls();
            lcd.printf("SAVE Config");
            wait(2);
        } 

    }//While settings loop exit 
    lcd.cls();
    lcd.printf("Exit Settings");
    wait(2);
}
//Function to process keypad inputs
void process_user_input(char keypad_value_in, int ui_current_screen_in){
    
    if(keypad_value_in == 36){
            if(lcd_timeout_flag == false){
                switch (ui_current_screen_in){
                    case 1:
                        ui_screen2();
                        ui_current_screen_in =2;
                        break;
                    case 2:
                        ui_screen3();
                        ui_current_screen_in =3;
                        break;
                    case 3:
                        ui_screen4();
                        ui_current_screen_in =4;
                        break;    
                }//switch block current ui screen
                lcd_timeout.reset();
                lcd_timeout.start();
                wait(2);
            }// if block lcdflag
            else{
                ui_screen1();
                wait(5);    
            }
        }// if block 36 end 
        
        // check for setting screen press enter
        if(keypad_value_in == 51){
                //Disable peltier 
                ui_settings();
                wait(5);
        }
}




//***************************PLACEHOLDER FUNCTION TO DISABLE EVERYTHING **********************
void disable_everything(){
    
    //Disable peltier,vac,fans,pumps,lights.
    
}


//*********************************************************************************************
//*                                                                                           *
//*                                                                                           *
//*                                                                                           *
//*                                                                                           *
//*                                    MAIN ROUTINE                                           *
//*                                                                                           *
//*                           DO NOT MODIFY WITHOUT TELLING UMAIR                             *
//*                                                                                           *
//*                                                                                           *
//*********************************************************************************************

int main(void)
{
    
    //call function that disables everything.
    disable_everything();
    
    
    
    //FOR INTERNAL CLOCK
    clock_mine.attach(&sec_inc, 1.0);
    char keypad_value;
    int ui_current_screen = 1;
    //************************************BOOT SEQUENCE*********************
    //call LCD boot
    ui_startup();
    wait(2);
    //SET TIME
    rtcimp_settime(6,0);
    //call function that reads values
    
/*  FOR CRTITICALITY
    NVIC_DisableIRQ(TIMER3_IRQn);
 
    // critical section
 
    NVIC_EnableIRQ(TIMER3_IRQn);
*/
    lcd.cls();
    lcd.locate(0,0);
    lcd.printf("Reading Sensors");
    
    read_sensors();
    
    wait(2);
    lcd.cls();
    lcd.locate(0,0);
    lcd.printf("Done Reading Sensors");
    //call function that performs functions.
    
    wait(2);
    lcd.cls();
    lcd.locate(0,0);
    lcd.printf("Watering Plants");
    
    waterplants();
    
    wait(2);
    lcd.cls();
    lcd.locate(0,0);
    lcd.printf("Air Humidity");
    
    airhumidity();
    
    wait(2);
    lcd.cls();
    lcd.locate(0,0);
    lcd.printf("Cycling Air");
    
    cycleair();
    
    wait(2);
    lcd.cls();
    lcd.locate(0,0);
    lcd.printf("Testing Lights");
    //led_test();
    
    
    wait(5);
    ui_current_screen = 1;
    ui_screen1();
    lcd_timeout.reset();
    wait(5);
    
    startup_flag = false ;  
    while(1) {
       
        wait(1.5);
        //FOR BUZZER
        timer1ms.attach(&timer1, 0.001); //interrupt attached function(timer) with interval (1 ms)
        keypad_value = Keypad();
        //timer1ms.detach();
        //something
        lcd_timeout_check();
        process_user_input(keypad_value,ui_current_screen);
        
        
        
        //  -> Key pressed
        
    }//WHILE END

        
}