An eddied version of http://mbed.org/users/crazystick/code/DHT22/ for LPC11U24. All printf statements are removed and features requiring the real time clock are removed.

Dependents:   RHT03_HelloWorld IOT_sensor_nfc CanSat_Alex cansat_alex_v1 ... more

Committer:
tristanjph
Date:
Tue Aug 28 14:27:50 2012 +0000
Revision:
0:24f59e3759a1
Child:
1:2bd5cffd60d0
First commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tristanjph 0:24f59e3759a1 1 /*
tristanjph 0:24f59e3759a1 2 DHT22.cpp - DHT22 sensor library
tristanjph 0:24f59e3759a1 3 Developed by HO WING KIT
tristanjph 0:24f59e3759a1 4 Modifications by Paul Adams
tristanjph 0:24f59e3759a1 5
tristanjph 0:24f59e3759a1 6 This library is free software; you can redistribute it and / or
tristanjph 0:24f59e3759a1 7 modify it under the terms of the GNU Leser General Public
tristanjph 0:24f59e3759a1 8 License as published by the Free Software Foundation; either
tristanjph 0:24f59e3759a1 9 version 2.1 of the License, or (at your option) any later version.
tristanjph 0:24f59e3759a1 10
tristanjph 0:24f59e3759a1 11 This library is distributed in the hope that it will be useful,
tristanjph 0:24f59e3759a1 12 but WITHOUT ANY WARRENTY; without even the implied warranty of
tristanjph 0:24f59e3759a1 13 MERCHANTABILITY or FITNESS FOR A PATRICULAR PURPOSE. See the GNU
tristanjph 0:24f59e3759a1 14 Lesser General Public License for more details.
tristanjph 0:24f59e3759a1 15
tristanjph 0:24f59e3759a1 16 You should have received a copy of the GNU Lesser General Public
tristanjph 0:24f59e3759a1 17 License along with this library; if not, write to the Free Software
tristanjph 0:24f59e3759a1 18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
tristanjph 0:24f59e3759a1 19
tristanjph 0:24f59e3759a1 20
tristanjph 0:24f59e3759a1 21 Humidity and Temperature Sensor DHT22 info found at
tristanjph 0:24f59e3759a1 22 http://www.sparkfun.com/products/10167
tristanjph 0:24f59e3759a1 23 same as RHT03 http://www.humiditycn.com
tristanjph 0:24f59e3759a1 24
tristanjph 0:24f59e3759a1 25 Version 0.1: 8-Jan-2011 by Ho Wing Kit
tristanjph 0:24f59e3759a1 26 Version 0.2: 29-Mar-2012 by Paul Adams
tristanjph 0:24f59e3759a1 27
tristanjph 0:24f59e3759a1 28 Additional Credit: All the commenters on http://www.sparkfun.com/products/10167
tristanjph 0:24f59e3759a1 29
tristanjph 0:24f59e3759a1 30 */
tristanjph 0:24f59e3759a1 31
tristanjph 0:24f59e3759a1 32 #include "RHT03.h"
tristanjph 0:24f59e3759a1 33
tristanjph 0:24f59e3759a1 34
tristanjph 0:24f59e3759a1 35 // This should be 40, but the sensor is adding an extra bit at the start
tristanjph 0:24f59e3759a1 36 #define DHT22_DATA_BIT_COUNT 41
tristanjph 0:24f59e3759a1 37
tristanjph 0:24f59e3759a1 38 DHT22::DHT22(PinName Data) {
tristanjph 0:24f59e3759a1 39
tristanjph 0:24f59e3759a1 40 _data = Data; // Set Data Pin
tristanjph 0:24f59e3759a1 41 _lastReadTime = time(NULL);
tristanjph 0:24f59e3759a1 42 _lastHumidity = 0;
tristanjph 0:24f59e3759a1 43 _lastTemperature = DHT22_ERROR_VALUE;
tristanjph 0:24f59e3759a1 44 }
tristanjph 0:24f59e3759a1 45
tristanjph 0:24f59e3759a1 46 DHT22::~DHT22() {
tristanjph 0:24f59e3759a1 47 }
tristanjph 0:24f59e3759a1 48
tristanjph 0:24f59e3759a1 49 DHT22_ERROR DHT22::readData() {
tristanjph 0:24f59e3759a1 50 int i, j, retryCount;
tristanjph 0:24f59e3759a1 51 int currentTemperature=0;
tristanjph 0:24f59e3759a1 52 int currentHumidity=0;
tristanjph 0:24f59e3759a1 53 unsigned int checkSum = 0, csPart1, csPart2, csPart3, csPart4;
tristanjph 0:24f59e3759a1 54 unsigned int bitTimes[DHT22_DATA_BIT_COUNT];
tristanjph 0:24f59e3759a1 55 DHT22_ERROR err = DHT_ERROR_NONE;
tristanjph 0:24f59e3759a1 56 time_t currentTime = time(NULL);
tristanjph 0:24f59e3759a1 57
tristanjph 0:24f59e3759a1 58 DigitalInOut DATA(_data);
tristanjph 0:24f59e3759a1 59
tristanjph 0:24f59e3759a1 60 for (i = 0; i < DHT22_DATA_BIT_COUNT; i++) {
tristanjph 0:24f59e3759a1 61 bitTimes[i] = 0;
tristanjph 0:24f59e3759a1 62 }
tristanjph 0:24f59e3759a1 63
tristanjph 0:24f59e3759a1 64 // if (int(currentTime - _lastReadTime) < 2) {
tristanjph 0:24f59e3759a1 65 // printf("DHT22 Error Access Time < 2s");
tristanjph 0:24f59e3759a1 66 // err = DHT_ERROR_TOO_QUICK;
tristanjph 0:24f59e3759a1 67 // }
tristanjph 0:24f59e3759a1 68 retryCount = 0;
tristanjph 0:24f59e3759a1 69 // Pin needs to start HIGH, wait unit it is HIGH with a timeout
tristanjph 0:24f59e3759a1 70 do {
tristanjph 0:24f59e3759a1 71 if (retryCount > 125) {
tristanjph 0:24f59e3759a1 72 //pc1.printf("DHT22 Bus busy!");
tristanjph 0:24f59e3759a1 73 err = DHT_BUS_HUNG;
tristanjph 0:24f59e3759a1 74 return err;
tristanjph 0:24f59e3759a1 75 }
tristanjph 0:24f59e3759a1 76 retryCount ++;
tristanjph 0:24f59e3759a1 77 wait_us(2);
tristanjph 0:24f59e3759a1 78 } while (DATA==0); // exit on DHT22 return 'High' Signal within 250us
tristanjph 0:24f59e3759a1 79
tristanjph 0:24f59e3759a1 80 // Send the activate pulse
tristanjph 0:24f59e3759a1 81 // Step 1: MCU send out start signal to DHT22 and DHT22 send
tristanjph 0:24f59e3759a1 82 // response signal to MCU.
tristanjph 0:24f59e3759a1 83 // If always signal high-voltage-level, it means DHT22 is not
tristanjph 0:24f59e3759a1 84 // working properly, please check the electrical connection status.
tristanjph 0:24f59e3759a1 85 //
tristanjph 0:24f59e3759a1 86 DATA.output(); // set pin to output data
tristanjph 0:24f59e3759a1 87 DATA = 0; // MCU send out start signal to DHT22
tristanjph 0:24f59e3759a1 88 wait_ms(18); // 18 ms wait (spec: at least 1ms)
tristanjph 0:24f59e3759a1 89 DATA = 1; // MCU pull up
tristanjph 0:24f59e3759a1 90 wait_us(40);
tristanjph 0:24f59e3759a1 91 DATA.input(); // set pin to receive data
tristanjph 0:24f59e3759a1 92 // Find the start of the ACK Pulse
tristanjph 0:24f59e3759a1 93 retryCount = 0;
tristanjph 0:24f59e3759a1 94 do {
tristanjph 0:24f59e3759a1 95 if (retryCount > 40) { // (Spec is 20-40 us high)
tristanjph 0:24f59e3759a1 96 //pc1.printf("DHT22 not responding!");
tristanjph 0:24f59e3759a1 97 err = DHT_ERROR_NOT_PRESENT;
tristanjph 0:24f59e3759a1 98 return err;
tristanjph 0:24f59e3759a1 99 }
tristanjph 0:24f59e3759a1 100 retryCount++;
tristanjph 0:24f59e3759a1 101 wait_us(1);
tristanjph 0:24f59e3759a1 102 } while (DATA==1); // Exit on DHT22 pull low within 40us
tristanjph 0:24f59e3759a1 103 if (err != DHT_ERROR_NONE) {
tristanjph 0:24f59e3759a1 104 // initialisation failed
tristanjph 0:24f59e3759a1 105 return err;
tristanjph 0:24f59e3759a1 106 }
tristanjph 0:24f59e3759a1 107 wait_us(80); // DHT pull up ready to transmit data
tristanjph 0:24f59e3759a1 108
tristanjph 0:24f59e3759a1 109 /*
tristanjph 0:24f59e3759a1 110 if (DATA == 0) {
tristanjph 0:24f59e3759a1 111 printf("DHT22 not ready!");
tristanjph 0:24f59e3759a1 112 err = DHT_ERROR_ACK_TOO_LONG;
tristanjph 0:24f59e3759a1 113 return err;
tristanjph 0:24f59e3759a1 114 }
tristanjph 0:24f59e3759a1 115 */
tristanjph 0:24f59e3759a1 116
tristanjph 0:24f59e3759a1 117 // Reading the 5 byte data stream
tristanjph 0:24f59e3759a1 118 // Step 2: DHT22 send data to MCU
tristanjph 0:24f59e3759a1 119 // Start bit -> low volage within 50us (actually could be anything from 35-75us)
tristanjph 0:24f59e3759a1 120 // 0 -> high volage within 26-28us (actually could be 10-40us)
tristanjph 0:24f59e3759a1 121 // 1 -> high volage within 70us (actually could be 60-85us)
tristanjph 0:24f59e3759a1 122 // See http://www.sparkfun.com/products/10167#comment-4f118d6e757b7f536e000000
tristanjph 0:24f59e3759a1 123
tristanjph 0:24f59e3759a1 124
tristanjph 0:24f59e3759a1 125 for (i = 0; i < 5; i++) {
tristanjph 0:24f59e3759a1 126 for (j = 0; j < 8; j++) {
tristanjph 0:24f59e3759a1 127 // Instead of relying on the data sheet, just wait while the RHT03 pin is low
tristanjph 0:24f59e3759a1 128 retryCount = 0;
tristanjph 0:24f59e3759a1 129 do {
tristanjph 0:24f59e3759a1 130 if (retryCount > 75) {
tristanjph 0:24f59e3759a1 131 //pc1.printf("DHT22 timeout waiting for data!");
tristanjph 0:24f59e3759a1 132 err = DHT_ERROR_DATA_TIMEOUT;
tristanjph 0:24f59e3759a1 133 }
tristanjph 0:24f59e3759a1 134 retryCount++;
tristanjph 0:24f59e3759a1 135 wait_us(1);
tristanjph 0:24f59e3759a1 136 } while (DATA == 0);
tristanjph 0:24f59e3759a1 137 // We now wait for 40us
tristanjph 0:24f59e3759a1 138 wait_us(40);
tristanjph 0:24f59e3759a1 139 if (DATA == 1) {
tristanjph 0:24f59e3759a1 140 // If pin is still high, bit value is a 1
tristanjph 0:24f59e3759a1 141 bitTimes[i*8+j] = 1;
tristanjph 0:24f59e3759a1 142 } else {
tristanjph 0:24f59e3759a1 143 // The bit value is a 0
tristanjph 0:24f59e3759a1 144 bitTimes[i*8+j] = 0;
tristanjph 0:24f59e3759a1 145 }
tristanjph 0:24f59e3759a1 146 int count = 0;
tristanjph 0:24f59e3759a1 147 while (DATA == 1 && count < 100) {
tristanjph 0:24f59e3759a1 148 wait_us(1); // Delay for 1 microsecond
tristanjph 0:24f59e3759a1 149 count++;
tristanjph 0:24f59e3759a1 150 }
tristanjph 0:24f59e3759a1 151 }
tristanjph 0:24f59e3759a1 152 }
tristanjph 0:24f59e3759a1 153 // Re-init DHT22 pin
tristanjph 0:24f59e3759a1 154 DATA.output();
tristanjph 0:24f59e3759a1 155 DATA = 1;
tristanjph 0:24f59e3759a1 156
tristanjph 0:24f59e3759a1 157 // Now bitTimes have the actual bits
tristanjph 0:24f59e3759a1 158 // that were needed to find the end of each data bit
tristanjph 0:24f59e3759a1 159 // Note: the bits are offset by one from the data sheet, not sure why
tristanjph 0:24f59e3759a1 160 currentHumidity = 0;
tristanjph 0:24f59e3759a1 161 currentTemperature = 0;
tristanjph 0:24f59e3759a1 162 checkSum = 0;
tristanjph 0:24f59e3759a1 163 // First 16 bits is Humidity
tristanjph 0:24f59e3759a1 164 for (i=0; i<16; i++) {
tristanjph 0:24f59e3759a1 165 //printf("bit %d: %d ", i, bitTimes[i+1]);
tristanjph 0:24f59e3759a1 166 if (bitTimes[i+1] > 0) {
tristanjph 0:24f59e3759a1 167 currentHumidity |= ( 1 << (15-i));
tristanjph 0:24f59e3759a1 168 }
tristanjph 0:24f59e3759a1 169 }
tristanjph 0:24f59e3759a1 170
tristanjph 0:24f59e3759a1 171 // Second 16 bits is Temperature
tristanjph 0:24f59e3759a1 172 for (i=0; i<16; i ++) {
tristanjph 0:24f59e3759a1 173 //printf("bit %d: %d ", i+16, bitTimes[i+17]);
tristanjph 0:24f59e3759a1 174 if (bitTimes[i+17] > 0) {
tristanjph 0:24f59e3759a1 175 currentTemperature |= (1 <<(15-i));
tristanjph 0:24f59e3759a1 176 }
tristanjph 0:24f59e3759a1 177 }
tristanjph 0:24f59e3759a1 178
tristanjph 0:24f59e3759a1 179 // Last 8 bit is Checksum
tristanjph 0:24f59e3759a1 180 for (i=0; i<8; i++) {
tristanjph 0:24f59e3759a1 181 //printf("bit %d: %d ", i+32, bitTimes[i+33]);
tristanjph 0:24f59e3759a1 182 if (bitTimes[i+33] > 0) {
tristanjph 0:24f59e3759a1 183 checkSum |= (1 << (7-i));
tristanjph 0:24f59e3759a1 184 }
tristanjph 0:24f59e3759a1 185 }
tristanjph 0:24f59e3759a1 186
tristanjph 0:24f59e3759a1 187 _lastHumidity = (float(currentHumidity) / 10.0);
tristanjph 0:24f59e3759a1 188
tristanjph 0:24f59e3759a1 189 // if first bit of currentTemperature is 1, it is negative value.
tristanjph 0:24f59e3759a1 190 if ((currentTemperature & 0x8000)==0x8000) {
tristanjph 0:24f59e3759a1 191 _lastTemperature = (float(currentTemperature & 0x7FFF) / 10.0) * -1.0;
tristanjph 0:24f59e3759a1 192 } else {
tristanjph 0:24f59e3759a1 193 _lastTemperature = float(currentTemperature) / 10.0;
tristanjph 0:24f59e3759a1 194 }
tristanjph 0:24f59e3759a1 195
tristanjph 0:24f59e3759a1 196 // Calculate Check Sum
tristanjph 0:24f59e3759a1 197 csPart1 = currentHumidity >> 8;
tristanjph 0:24f59e3759a1 198 csPart2 = currentHumidity & 0xFF;
tristanjph 0:24f59e3759a1 199 csPart3 = currentTemperature >> 8;
tristanjph 0:24f59e3759a1 200 csPart4 = currentTemperature & 0xFF;
tristanjph 0:24f59e3759a1 201 if (checkSum == ((csPart1 + csPart2 + csPart3 + csPart4) & 0xFF)) {
tristanjph 0:24f59e3759a1 202 _lastReadTime = currentTime;
tristanjph 0:24f59e3759a1 203 //pc1.printf("OK-->Temperature:%f, Humidity:%f\r\n", _lastTemperature, _lastHumidity);
tristanjph 0:24f59e3759a1 204 err = DHT_ERROR_NONE;
tristanjph 0:24f59e3759a1 205 } else {
tristanjph 0:24f59e3759a1 206 //pc1.printf("DHT22 Checksum error!\n");
tristanjph 0:24f59e3759a1 207 //pc1.printf("Calculate check sum is %d\n",(csPart1 + csPart2 + csPart3 + csPart4) & 0xFF);
tristanjph 0:24f59e3759a1 208 //pc1.printf("Reading check sum is %d",checkSum);
tristanjph 0:24f59e3759a1 209 err = DHT_ERROR_CHECKSUM;
tristanjph 0:24f59e3759a1 210 }
tristanjph 0:24f59e3759a1 211 return err;
tristanjph 0:24f59e3759a1 212 }
tristanjph 0:24f59e3759a1 213
tristanjph 0:24f59e3759a1 214 float DHT22::getTemperatureC() {
tristanjph 0:24f59e3759a1 215 return _lastTemperature;
tristanjph 0:24f59e3759a1 216 }
tristanjph 0:24f59e3759a1 217
tristanjph 0:24f59e3759a1 218 float DHT22::getHumidity() {
tristanjph 0:24f59e3759a1 219 return _lastHumidity;
tristanjph 0:24f59e3759a1 220 }