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

Committer:
umairaftab
Date:
Wed Apr 09 21:36:13 2014 +0000
Revision:
4:8d1df60ef3c3
Rev2;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
umairaftab 4:8d1df60ef3c3 1
umairaftab 4:8d1df60ef3c3 2 #include "DHT.h"
umairaftab 4:8d1df60ef3c3 3
umairaftab 4:8d1df60ef3c3 4 #define DHT_DATA_BIT_COUNT 40
umairaftab 4:8d1df60ef3c3 5
umairaftab 4:8d1df60ef3c3 6 DHT::DHT(PinName pin,int DHTtype) {
umairaftab 4:8d1df60ef3c3 7 _pin = pin;
umairaftab 4:8d1df60ef3c3 8 _DHTtype = DHTtype;
umairaftab 4:8d1df60ef3c3 9 _firsttime=true;
umairaftab 4:8d1df60ef3c3 10 }
umairaftab 4:8d1df60ef3c3 11
umairaftab 4:8d1df60ef3c3 12 DHT::~DHT() {
umairaftab 4:8d1df60ef3c3 13 }
umairaftab 4:8d1df60ef3c3 14
umairaftab 4:8d1df60ef3c3 15 int DHT::readData() {
umairaftab 4:8d1df60ef3c3 16 int i, j, retryCount,b;
umairaftab 4:8d1df60ef3c3 17 unsigned int bitTimes[DHT_DATA_BIT_COUNT];
umairaftab 4:8d1df60ef3c3 18
umairaftab 4:8d1df60ef3c3 19 eError err = ERROR_NONE;
umairaftab 4:8d1df60ef3c3 20 time_t currentTime = time(NULL);
umairaftab 4:8d1df60ef3c3 21
umairaftab 4:8d1df60ef3c3 22 DigitalInOut DHT_io(_pin);
umairaftab 4:8d1df60ef3c3 23
umairaftab 4:8d1df60ef3c3 24 for (i = 0; i < DHT_DATA_BIT_COUNT; i++) {
umairaftab 4:8d1df60ef3c3 25 bitTimes[i] = 0;
umairaftab 4:8d1df60ef3c3 26 }
umairaftab 4:8d1df60ef3c3 27
umairaftab 4:8d1df60ef3c3 28 if (!_firsttime) {
umairaftab 4:8d1df60ef3c3 29 if (int(currentTime - _lastReadTime) < 2) {
umairaftab 4:8d1df60ef3c3 30 err = ERROR_NO_PATIENCE;
umairaftab 4:8d1df60ef3c3 31 return err;
umairaftab 4:8d1df60ef3c3 32 }
umairaftab 4:8d1df60ef3c3 33 } else {
umairaftab 4:8d1df60ef3c3 34 _firsttime=false;
umairaftab 4:8d1df60ef3c3 35 _lastReadTime=currentTime;
umairaftab 4:8d1df60ef3c3 36 }
umairaftab 4:8d1df60ef3c3 37 retryCount = 0;
umairaftab 4:8d1df60ef3c3 38
umairaftab 4:8d1df60ef3c3 39 do {
umairaftab 4:8d1df60ef3c3 40 if (retryCount > 125) {
umairaftab 4:8d1df60ef3c3 41 err = BUS_BUSY;
umairaftab 4:8d1df60ef3c3 42 return err;
umairaftab 4:8d1df60ef3c3 43 }
umairaftab 4:8d1df60ef3c3 44 retryCount ++;
umairaftab 4:8d1df60ef3c3 45 wait_us(2);
umairaftab 4:8d1df60ef3c3 46 } while ((DHT_io==0));
umairaftab 4:8d1df60ef3c3 47
umairaftab 4:8d1df60ef3c3 48
umairaftab 4:8d1df60ef3c3 49 DHT_io.output();
umairaftab 4:8d1df60ef3c3 50 DHT_io = 0;
umairaftab 4:8d1df60ef3c3 51 wait_ms(18);
umairaftab 4:8d1df60ef3c3 52 DHT_io = 1;
umairaftab 4:8d1df60ef3c3 53 wait_us(40);
umairaftab 4:8d1df60ef3c3 54 DHT_io.input();
umairaftab 4:8d1df60ef3c3 55
umairaftab 4:8d1df60ef3c3 56 retryCount = 0;
umairaftab 4:8d1df60ef3c3 57 do {
umairaftab 4:8d1df60ef3c3 58 if (retryCount > 40) {
umairaftab 4:8d1df60ef3c3 59 err = ERROR_NOT_PRESENT;
umairaftab 4:8d1df60ef3c3 60 return err;
umairaftab 4:8d1df60ef3c3 61 }
umairaftab 4:8d1df60ef3c3 62 retryCount++;
umairaftab 4:8d1df60ef3c3 63 wait_us(1);
umairaftab 4:8d1df60ef3c3 64 } while ((DHT_io==1));
umairaftab 4:8d1df60ef3c3 65
umairaftab 4:8d1df60ef3c3 66 if (err != ERROR_NONE) {
umairaftab 4:8d1df60ef3c3 67 return err;
umairaftab 4:8d1df60ef3c3 68 }
umairaftab 4:8d1df60ef3c3 69
umairaftab 4:8d1df60ef3c3 70 wait_us(80);
umairaftab 4:8d1df60ef3c3 71
umairaftab 4:8d1df60ef3c3 72 for (i = 0; i < 5; i++) {
umairaftab 4:8d1df60ef3c3 73 for (j = 0; j < 8; j++) {
umairaftab 4:8d1df60ef3c3 74
umairaftab 4:8d1df60ef3c3 75 retryCount = 0;
umairaftab 4:8d1df60ef3c3 76 do {
umairaftab 4:8d1df60ef3c3 77 if (retryCount > 75) {
umairaftab 4:8d1df60ef3c3 78 err = ERROR_DATA_TIMEOUT;
umairaftab 4:8d1df60ef3c3 79 return err;
umairaftab 4:8d1df60ef3c3 80 }
umairaftab 4:8d1df60ef3c3 81 retryCount++;
umairaftab 4:8d1df60ef3c3 82 wait_us(1);
umairaftab 4:8d1df60ef3c3 83 } while (DHT_io == 0);
umairaftab 4:8d1df60ef3c3 84 wait_us(40);
umairaftab 4:8d1df60ef3c3 85 bitTimes[i*8+j]=DHT_io;
umairaftab 4:8d1df60ef3c3 86
umairaftab 4:8d1df60ef3c3 87 int count = 0;
umairaftab 4:8d1df60ef3c3 88 while (DHT_io == 1 && count < 100) {
umairaftab 4:8d1df60ef3c3 89 wait_us(1);
umairaftab 4:8d1df60ef3c3 90 count++;
umairaftab 4:8d1df60ef3c3 91 }
umairaftab 4:8d1df60ef3c3 92 }
umairaftab 4:8d1df60ef3c3 93 }
umairaftab 4:8d1df60ef3c3 94 DHT_io.output();
umairaftab 4:8d1df60ef3c3 95 DHT_io = 1;
umairaftab 4:8d1df60ef3c3 96 for (i = 0; i < 5; i++) {
umairaftab 4:8d1df60ef3c3 97 b=0;
umairaftab 4:8d1df60ef3c3 98 for (j=0; j<8; j++) {
umairaftab 4:8d1df60ef3c3 99 if (bitTimes[i*8+j+1] > 0) {
umairaftab 4:8d1df60ef3c3 100 b |= ( 1 << (7-j));
umairaftab 4:8d1df60ef3c3 101 }
umairaftab 4:8d1df60ef3c3 102 }
umairaftab 4:8d1df60ef3c3 103 DHT_data[i]=b;
umairaftab 4:8d1df60ef3c3 104 }
umairaftab 4:8d1df60ef3c3 105
umairaftab 4:8d1df60ef3c3 106 if (DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]) & 0xFF)) {
umairaftab 4:8d1df60ef3c3 107 _lastReadTime = currentTime;
umairaftab 4:8d1df60ef3c3 108 _lastTemperature=CalcTemperature();
umairaftab 4:8d1df60ef3c3 109 _lastHumidity=CalcHumidity();
umairaftab 4:8d1df60ef3c3 110
umairaftab 4:8d1df60ef3c3 111 } else {
umairaftab 4:8d1df60ef3c3 112 err = ERROR_CHECKSUM;
umairaftab 4:8d1df60ef3c3 113 }
umairaftab 4:8d1df60ef3c3 114
umairaftab 4:8d1df60ef3c3 115 return err;
umairaftab 4:8d1df60ef3c3 116
umairaftab 4:8d1df60ef3c3 117 }
umairaftab 4:8d1df60ef3c3 118
umairaftab 4:8d1df60ef3c3 119 float DHT::CalcTemperature() {
umairaftab 4:8d1df60ef3c3 120 int v;
umairaftab 4:8d1df60ef3c3 121
umairaftab 4:8d1df60ef3c3 122 switch (_DHTtype) {
umairaftab 4:8d1df60ef3c3 123 case DHT11:
umairaftab 4:8d1df60ef3c3 124 v = DHT_data[2];
umairaftab 4:8d1df60ef3c3 125 return float(v);
umairaftab 4:8d1df60ef3c3 126 case DHT22:
umairaftab 4:8d1df60ef3c3 127 v = DHT_data[2] & 0x7F;
umairaftab 4:8d1df60ef3c3 128 v *= 256;
umairaftab 4:8d1df60ef3c3 129 v += DHT_data[3];
umairaftab 4:8d1df60ef3c3 130 v /= 10;
umairaftab 4:8d1df60ef3c3 131 if (DHT_data[2] & 0x80)
umairaftab 4:8d1df60ef3c3 132 v *= -1;
umairaftab 4:8d1df60ef3c3 133 return float(v);
umairaftab 4:8d1df60ef3c3 134 }
umairaftab 4:8d1df60ef3c3 135 return 0;
umairaftab 4:8d1df60ef3c3 136 }
umairaftab 4:8d1df60ef3c3 137
umairaftab 4:8d1df60ef3c3 138 float DHT::ReadHumidity() {
umairaftab 4:8d1df60ef3c3 139 return _lastHumidity;
umairaftab 4:8d1df60ef3c3 140 }
umairaftab 4:8d1df60ef3c3 141
umairaftab 4:8d1df60ef3c3 142 float DHT::ConvertCelciustoFarenheit(float celsius) {
umairaftab 4:8d1df60ef3c3 143 return celsius * 9 / 5 + 32;
umairaftab 4:8d1df60ef3c3 144 }
umairaftab 4:8d1df60ef3c3 145
umairaftab 4:8d1df60ef3c3 146 float DHT::ConvertCelciustoKelvin(float celsius) {
umairaftab 4:8d1df60ef3c3 147 return celsius + 273.15;
umairaftab 4:8d1df60ef3c3 148 }
umairaftab 4:8d1df60ef3c3 149
umairaftab 4:8d1df60ef3c3 150 // dewPoint function NOAA
umairaftab 4:8d1df60ef3c3 151 // reference: http://wahiduddin.net/calc/density_algorithms.htm
umairaftab 4:8d1df60ef3c3 152 float DHT::CalcdewPoint(float celsius, float humidity) {
umairaftab 4:8d1df60ef3c3 153 float A0= 373.15/(273.15 + celsius);
umairaftab 4:8d1df60ef3c3 154 float SUM = -7.90298 * (A0-1);
umairaftab 4:8d1df60ef3c3 155 SUM += 5.02808 * log10(A0);
umairaftab 4:8d1df60ef3c3 156 SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
umairaftab 4:8d1df60ef3c3 157 SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
umairaftab 4:8d1df60ef3c3 158 SUM += log10(1013.246);
umairaftab 4:8d1df60ef3c3 159 float VP = pow(10, SUM-3) * humidity;
umairaftab 4:8d1df60ef3c3 160 float T = log(VP/0.61078); // temp var
umairaftab 4:8d1df60ef3c3 161 return (241.88 * T) / (17.558-T);
umairaftab 4:8d1df60ef3c3 162 }
umairaftab 4:8d1df60ef3c3 163
umairaftab 4:8d1df60ef3c3 164 // delta max = 0.6544 wrt dewPoint()
umairaftab 4:8d1df60ef3c3 165 // 5x faster than dewPoint()
umairaftab 4:8d1df60ef3c3 166 // reference: http://en.wikipedia.org/wiki/Dew_point
umairaftab 4:8d1df60ef3c3 167 float DHT::CalcdewPointFast(float celsius, float humidity)
umairaftab 4:8d1df60ef3c3 168 {
umairaftab 4:8d1df60ef3c3 169 float a = 17.271;
umairaftab 4:8d1df60ef3c3 170 float b = 237.7;
umairaftab 4:8d1df60ef3c3 171 float temp = (a * celsius) / (b + celsius) + log(humidity/100);
umairaftab 4:8d1df60ef3c3 172 float Td = (b * temp) / (a - temp);
umairaftab 4:8d1df60ef3c3 173 return Td;
umairaftab 4:8d1df60ef3c3 174 }
umairaftab 4:8d1df60ef3c3 175
umairaftab 4:8d1df60ef3c3 176 float DHT::ReadTemperature(eScale Scale) {
umairaftab 4:8d1df60ef3c3 177 if (Scale == FARENHEIT)
umairaftab 4:8d1df60ef3c3 178 return ConvertCelciustoFarenheit(_lastTemperature);
umairaftab 4:8d1df60ef3c3 179 else if (Scale == KELVIN)
umairaftab 4:8d1df60ef3c3 180 return ConvertCelciustoKelvin(_lastTemperature);
umairaftab 4:8d1df60ef3c3 181 else
umairaftab 4:8d1df60ef3c3 182 return _lastTemperature;
umairaftab 4:8d1df60ef3c3 183 }
umairaftab 4:8d1df60ef3c3 184
umairaftab 4:8d1df60ef3c3 185 float DHT::CalcHumidity() {
umairaftab 4:8d1df60ef3c3 186 int v;
umairaftab 4:8d1df60ef3c3 187
umairaftab 4:8d1df60ef3c3 188 switch (_DHTtype) {
umairaftab 4:8d1df60ef3c3 189 case DHT11:
umairaftab 4:8d1df60ef3c3 190 v = DHT_data[0];
umairaftab 4:8d1df60ef3c3 191 return float(v);
umairaftab 4:8d1df60ef3c3 192 case DHT22:
umairaftab 4:8d1df60ef3c3 193 v = DHT_data[0];
umairaftab 4:8d1df60ef3c3 194 v *= 256;
umairaftab 4:8d1df60ef3c3 195 v += DHT_data[1];
umairaftab 4:8d1df60ef3c3 196 v /= 10;
umairaftab 4:8d1df60ef3c3 197 return float(v);
umairaftab 4:8d1df60ef3c3 198 }
umairaftab 4:8d1df60ef3c3 199 return 0;
umairaftab 4:8d1df60ef3c3 200 }
umairaftab 4:8d1df60ef3c3 201