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