Montvydas Klumbys
/
AutoPlants
Auto-Greenhouse project
main.cpp@0:85f540673fe1, 2015-05-29 (annotated)
- Committer:
- moklumbys
- Date:
- Fri May 29 15:28:26 2015 +0000
- Revision:
- 0:85f540673fe1
final part? almost...;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
moklumbys | 0:85f540673fe1 | 1 | #include "mbed.h" |
moklumbys | 0:85f540673fe1 | 2 | #include "string.h" |
moklumbys | 0:85f540673fe1 | 3 | #include "DHT11.h" |
moklumbys | 0:85f540673fe1 | 4 | |
moklumbys | 0:85f540673fe1 | 5 | #define TEMPERATURE_OFFSET 0.0f //in degrees +- temperature from the mean point |
moklumbys | 0:85f540673fe1 | 6 | #define GROUND_HUMIDITY_OFFSET 0.0f//in percentage |
moklumbys | 0:85f540673fe1 | 7 | |
moklumbys | 0:85f540673fe1 | 8 | //for reference and real values |
moklumbys | 0:85f540673fe1 | 9 | // 0 - inside temperature, 1 - inside humidity, 2 - ground humidity, 3 - light intensity |
moklumbys | 0:85f540673fe1 | 10 | |
moklumbys | 0:85f540673fe1 | 11 | //for actuator |
moklumbys | 0:85f540673fe1 | 12 | // 0 - heater (digital), 1 - ventilator(digital), 2 - water valve(digital), 3 - LEDs(analogue) |
moklumbys | 0:85f540673fe1 | 13 | |
moklumbys | 0:85f540673fe1 | 14 | //-----------------------------Ini Classes----------------------------- |
moklumbys | 0:85f540673fe1 | 15 | Serial bt(D10, D2); //initiate bluetooth |
moklumbys | 0:85f540673fe1 | 16 | Serial pc(USBTX,USBRX); //and pc serial connections |
moklumbys | 0:85f540673fe1 | 17 | DigitalOut myled(LED1); //and an LED |
moklumbys | 0:85f540673fe1 | 18 | PwmOut light(D11); |
moklumbys | 0:85f540673fe1 | 19 | DigitalOut valve(D4); |
moklumbys | 0:85f540673fe1 | 20 | DigitalOut heater(D5); |
moklumbys | 0:85f540673fe1 | 21 | DigitalOut ventilator(D6); |
moklumbys | 0:85f540673fe1 | 22 | |
moklumbys | 0:85f540673fe1 | 23 | AnalogIn lightSensor(A0); |
moklumbys | 0:85f540673fe1 | 24 | AnalogIn humiditySensor(A1); |
moklumbys | 0:85f540673fe1 | 25 | DHT11 dht(A2); |
moklumbys | 0:85f540673fe1 | 26 | //------------------------Function prototypes-------------------------- |
moklumbys | 0:85f540673fe1 | 27 | void onRequest(); |
moklumbys | 0:85f540673fe1 | 28 | void onUpdate(); |
moklumbys | 0:85f540673fe1 | 29 | void compareLight(); |
moklumbys | 0:85f540673fe1 | 30 | void compareMoisture(); |
moklumbys | 0:85f540673fe1 | 31 | void compareTemperature(); |
moklumbys | 0:85f540673fe1 | 32 | void senseLight(); |
moklumbys | 0:85f540673fe1 | 33 | void senseMoisture(); |
moklumbys | 0:85f540673fe1 | 34 | void senseDHT11(); |
moklumbys | 0:85f540673fe1 | 35 | //-------------------------------------ISR----------------------------- |
moklumbys | 0:85f540673fe1 | 36 | char word[11]; |
moklumbys | 0:85f540673fe1 | 37 | float reference[4]; |
moklumbys | 0:85f540673fe1 | 38 | float real[4]; |
moklumbys | 0:85f540673fe1 | 39 | float actuator[4]; |
moklumbys | 0:85f540673fe1 | 40 | |
moklumbys | 0:85f540673fe1 | 41 | void ISR() |
moklumbys | 0:85f540673fe1 | 42 | { |
moklumbys | 0:85f540673fe1 | 43 | if(bt.readable()) { |
moklumbys | 0:85f540673fe1 | 44 | bt.scanf ("%10s", word); |
moklumbys | 0:85f540673fe1 | 45 | if (strcmp(word, "request")==0) |
moklumbys | 0:85f540673fe1 | 46 | { |
moklumbys | 0:85f540673fe1 | 47 | onRequest(); |
moklumbys | 0:85f540673fe1 | 48 | } |
moklumbys | 0:85f540673fe1 | 49 | if (strcmp(word, "update")==0) |
moklumbys | 0:85f540673fe1 | 50 | { |
moklumbys | 0:85f540673fe1 | 51 | onUpdate(); |
moklumbys | 0:85f540673fe1 | 52 | } |
moklumbys | 0:85f540673fe1 | 53 | } |
moklumbys | 0:85f540673fe1 | 54 | } |
moklumbys | 0:85f540673fe1 | 55 | |
moklumbys | 0:85f540673fe1 | 56 | //-----------------------------------MAIN------------------------------ |
moklumbys | 0:85f540673fe1 | 57 | int main() |
moklumbys | 0:85f540673fe1 | 58 | { |
moklumbys | 0:85f540673fe1 | 59 | light.period_ms(20); |
moklumbys | 0:85f540673fe1 | 60 | reference[0] = 25; //temperature |
moklumbys | 0:85f540673fe1 | 61 | reference[1] = 25; //air humidity |
moklumbys | 0:85f540673fe1 | 62 | reference[3] = 55; //light intensity |
moklumbys | 0:85f540673fe1 | 63 | reference[2] = 30; //ground humidity |
moklumbys | 0:85f540673fe1 | 64 | |
moklumbys | 0:85f540673fe1 | 65 | bt.baud(9600); |
moklumbys | 0:85f540673fe1 | 66 | pc.baud(115200); |
moklumbys | 0:85f540673fe1 | 67 | |
moklumbys | 0:85f540673fe1 | 68 | bt.attach(&ISR); //attach an interrupt on a bt serial connection |
moklumbys | 0:85f540673fe1 | 69 | light = 0.0; |
moklumbys | 0:85f540673fe1 | 70 | |
moklumbys | 0:85f540673fe1 | 71 | pc.printf ("Oh well...\n"); |
moklumbys | 0:85f540673fe1 | 72 | real[3] = 0.0; |
moklumbys | 0:85f540673fe1 | 73 | while(1) { |
moklumbys | 0:85f540673fe1 | 74 | //here disable interrupts |
moklumbys | 0:85f540673fe1 | 75 | // __disable_irq(); |
moklumbys | 0:85f540673fe1 | 76 | |
moklumbys | 0:85f540673fe1 | 77 | senseLight(); |
moklumbys | 0:85f540673fe1 | 78 | senseMoisture(); |
moklumbys | 0:85f540673fe1 | 79 | senseDHT11(); |
moklumbys | 0:85f540673fe1 | 80 | |
moklumbys | 0:85f540673fe1 | 81 | pc.printf ("temp=%.2f, hum=%.2f, moist=%.2f, inten=%.2f\n", real[0], real[1], real[2], real[3]); |
moklumbys | 0:85f540673fe1 | 82 | |
moklumbys | 0:85f540673fe1 | 83 | compareLight (); |
moklumbys | 0:85f540673fe1 | 84 | compareMoisture (); |
moklumbys | 0:85f540673fe1 | 85 | compareTemperature(); |
moklumbys | 0:85f540673fe1 | 86 | |
moklumbys | 0:85f540673fe1 | 87 | //here enable interrupts |
moklumbys | 0:85f540673fe1 | 88 | // __enable_irq(); |
moklumbys | 0:85f540673fe1 | 89 | |
moklumbys | 0:85f540673fe1 | 90 | wait(3.0f); |
moklumbys | 0:85f540673fe1 | 91 | } |
moklumbys | 0:85f540673fe1 | 92 | } |
moklumbys | 0:85f540673fe1 | 93 | |
moklumbys | 0:85f540673fe1 | 94 | //--------------------------------Functions------------------------------------------ |
moklumbys | 0:85f540673fe1 | 95 | //*********************************REQUEST info from sensors************************* |
moklumbys | 0:85f540673fe1 | 96 | void onRequest() |
moklumbys | 0:85f540673fe1 | 97 | { |
moklumbys | 0:85f540673fe1 | 98 | // bt.printf ("%d\n", 1); |
moklumbys | 0:85f540673fe1 | 99 | //bt.printf ("temp=%.1f, hum=%.1f, groundHum=%.1f, inten=%.1f\n", 10.0, 20.0, 30.0, 40.0); |
moklumbys | 0:85f540673fe1 | 100 | // for (int i = 0; i < 4; i++) |
moklumbys | 0:85f540673fe1 | 101 | // { |
moklumbys | 0:85f540673fe1 | 102 | // bt.printf ("%.2f\n", real[i]); |
moklumbys | 0:85f540673fe1 | 103 | // bt.printf ("%d\n", (int)real[i]); |
moklumbys | 0:85f540673fe1 | 104 | // } |
moklumbys | 0:85f540673fe1 | 105 | bt.printf ("%d\n", (int)real[0]); |
moklumbys | 0:85f540673fe1 | 106 | bt.printf ("%d\n", (int)real[1]); |
moklumbys | 0:85f540673fe1 | 107 | bt.printf ("%d\n", (int)real[2]); |
moklumbys | 0:85f540673fe1 | 108 | bt.printf ("%d\n", (int)real[3]); |
moklumbys | 0:85f540673fe1 | 109 | } |
moklumbys | 0:85f540673fe1 | 110 | //***********************************UPDATE info from phone************************** |
moklumbys | 0:85f540673fe1 | 111 | void onUpdate() |
moklumbys | 0:85f540673fe1 | 112 | { |
moklumbys | 0:85f540673fe1 | 113 | int i = 0; |
moklumbys | 0:85f540673fe1 | 114 | char buff; |
moklumbys | 0:85f540673fe1 | 115 | |
moklumbys | 0:85f540673fe1 | 116 | while(true) |
moklumbys | 0:85f540673fe1 | 117 | { |
moklumbys | 0:85f540673fe1 | 118 | if (bt.readable()) //read off 4 values - references |
moklumbys | 0:85f540673fe1 | 119 | { |
moklumbys | 0:85f540673fe1 | 120 | if(!bt.scanf ("%f", &reference[i])) |
moklumbys | 0:85f540673fe1 | 121 | bt.scanf ("%c", &buff); //if not float, put info buffer and through out.. |
moklumbys | 0:85f540673fe1 | 122 | else |
moklumbys | 0:85f540673fe1 | 123 | i++; |
moklumbys | 0:85f540673fe1 | 124 | } |
moklumbys | 0:85f540673fe1 | 125 | if (i == 4) break; //after getting enough values go back to normal loop |
moklumbys | 0:85f540673fe1 | 126 | } |
moklumbys | 0:85f540673fe1 | 127 | pc.printf ("%.1f, %.1f, %.1f, %.1f\n", reference[0], reference[1], reference[2], reference[3]); |
moklumbys | 0:85f540673fe1 | 128 | } |
moklumbys | 0:85f540673fe1 | 129 | //******************************LIGHT intensity function***************************** |
moklumbys | 0:85f540673fe1 | 130 | void compareLight() |
moklumbys | 0:85f540673fe1 | 131 | { |
moklumbys | 0:85f540673fe1 | 132 | if ((reference[3] > real[3]) && (light < 1.0f)) //if not enough of light - turn on additional light little by little |
moklumbys | 0:85f540673fe1 | 133 | light = light + 0.05f; |
moklumbys | 0:85f540673fe1 | 134 | else if ((reference[3] < real[3]) && (light > 0.0f)) //if enough of light - turn off the light little by little |
moklumbys | 0:85f540673fe1 | 135 | light = light - 0.05f; |
moklumbys | 0:85f540673fe1 | 136 | } |
moklumbys | 0:85f540673fe1 | 137 | //****************************GROUND moisture detection***************************** |
moklumbys | 0:85f540673fe1 | 138 | void compareMoisture() |
moklumbys | 0:85f540673fe1 | 139 | { |
moklumbys | 0:85f540673fe1 | 140 | if (reference[2] - GROUND_HUMIDITY_OFFSET > real[2]) //if moisture drops below the reference - offset, turn on valve with water |
moklumbys | 0:85f540673fe1 | 141 | valve = 1; |
moklumbys | 0:85f540673fe1 | 142 | else |
moklumbys | 0:85f540673fe1 | 143 | valve = 0; |
moklumbys | 0:85f540673fe1 | 144 | } |
moklumbys | 0:85f540673fe1 | 145 | //*****************************TEMPERATURE sensor************************************ |
moklumbys | 0:85f540673fe1 | 146 | void compareTemperature() |
moklumbys | 0:85f540673fe1 | 147 | { |
moklumbys | 0:85f540673fe1 | 148 | if (reference[0] + TEMPERATURE_OFFSET < real[0]) //if above the reference + offset, turn on ventilator |
moklumbys | 0:85f540673fe1 | 149 | { |
moklumbys | 0:85f540673fe1 | 150 | ventilator = 1; |
moklumbys | 0:85f540673fe1 | 151 | heater = 0; |
moklumbys | 0:85f540673fe1 | 152 | } |
moklumbys | 0:85f540673fe1 | 153 | else if (reference[0] - TEMPERATURE_OFFSET > real[0]) //if below reference - offset, then turn on heater |
moklumbys | 0:85f540673fe1 | 154 | { |
moklumbys | 0:85f540673fe1 | 155 | ventilator = 0; |
moklumbys | 0:85f540673fe1 | 156 | heater = 1; |
moklumbys | 0:85f540673fe1 | 157 | } |
moklumbys | 0:85f540673fe1 | 158 | else //else let the plants grow! |
moklumbys | 0:85f540673fe1 | 159 | { |
moklumbys | 0:85f540673fe1 | 160 | heater = 0; |
moklumbys | 0:85f540673fe1 | 161 | ventilator = 0; |
moklumbys | 0:85f540673fe1 | 162 | } |
moklumbys | 0:85f540673fe1 | 163 | } |
moklumbys | 0:85f540673fe1 | 164 | //******************************************READ LIGHT******************************* |
moklumbys | 0:85f540673fe1 | 165 | void senseLight() |
moklumbys | 0:85f540673fe1 | 166 | { |
moklumbys | 0:85f540673fe1 | 167 | real[3] = lightSensor.read()*100.0; |
moklumbys | 0:85f540673fe1 | 168 | } |
moklumbys | 0:85f540673fe1 | 169 | //*****************************************READ Moisture***************************** |
moklumbys | 0:85f540673fe1 | 170 | void senseMoisture() |
moklumbys | 0:85f540673fe1 | 171 | { |
moklumbys | 0:85f540673fe1 | 172 | real[2] = (1.0-humiditySensor.read())*100.0; |
moklumbys | 0:85f540673fe1 | 173 | } |
moklumbys | 0:85f540673fe1 | 174 | //****************************************READ temperature*************************** |
moklumbys | 0:85f540673fe1 | 175 | void senseDHT11() |
moklumbys | 0:85f540673fe1 | 176 | { |
moklumbys | 0:85f540673fe1 | 177 | int s = dht.readData(); |
moklumbys | 0:85f540673fe1 | 178 | if (s != DHT11::OK) |
moklumbys | 0:85f540673fe1 | 179 | { |
moklumbys | 0:85f540673fe1 | 180 | printf("Error!\r\n"); |
moklumbys | 0:85f540673fe1 | 181 | } |
moklumbys | 0:85f540673fe1 | 182 | else |
moklumbys | 0:85f540673fe1 | 183 | { |
moklumbys | 0:85f540673fe1 | 184 | real[0] = dht.readTemperature(); |
moklumbys | 0:85f540673fe1 | 185 | real[1] = dht.readHumidity(); |
moklumbys | 0:85f540673fe1 | 186 | } |
moklumbys | 0:85f540673fe1 | 187 | } |