Library for DHT22/RHT03 Temperature and Humidity Sensor

Committer:
crazystick
Date:
Thu Mar 29 06:14:07 2012 +0000
Revision:
0:42ffca439fcc
Updates from around the web, still some issues but at least it now reads reliably for me

Who changed what in which revision?

UserRevisionLine numberNew contents of line
crazystick 0:42ffca439fcc 1 /* mbed DHT22 Library
crazystick 0:42ffca439fcc 2 * Copyright (c) 2011, sford, http://mbed.org
crazystick 0:42ffca439fcc 3 *
crazystick 0:42ffca439fcc 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
crazystick 0:42ffca439fcc 5 * of this software and associated documnetation files (the "Software"), to deal
crazystick 0:42ffca439fcc 6 * in the Software without restriction, including without limitation the rights
crazystick 0:42ffca439fcc 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
crazystick 0:42ffca439fcc 8 * copies of the Software, and to permit persons to whom the Software is
crazystick 0:42ffca439fcc 9 * furished to do so, subject to the following conditions:
crazystick 0:42ffca439fcc 10 *
crazystick 0:42ffca439fcc 11 * The above copyright notice and this permission notice shall be included in
crazystick 0:42ffca439fcc 12 * all copies or substantial portions of the Software.
crazystick 0:42ffca439fcc 13 *
crazystick 0:42ffca439fcc 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
crazystick 0:42ffca439fcc 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
crazystick 0:42ffca439fcc 16 * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
crazystick 0:42ffca439fcc 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
crazystick 0:42ffca439fcc 18 * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
crazystick 0:42ffca439fcc 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
crazystick 0:42ffca439fcc 20 * THE SOFTWARE.
crazystick 0:42ffca439fcc 21 */
crazystick 0:42ffca439fcc 22
crazystick 0:42ffca439fcc 23 #ifndef MBED_DHT22_H
crazystick 0:42ffca439fcc 24 #define MBED_DHT22_H
crazystick 0:42ffca439fcc 25
crazystick 0:42ffca439fcc 26 #include "mbed.h"
crazystick 0:42ffca439fcc 27
crazystick 0:42ffca439fcc 28 /**
crazystick 0:42ffca439fcc 29 * Currently supports DHT22 (SparkFun Electronics)
crazystick 0:42ffca439fcc 30 * Humidity and Temperature Sensor
crazystick 0:42ffca439fcc 31 *
crazystick 0:42ffca439fcc 32 * Features:
crazystick 0:42ffca439fcc 33 * > 3.3-6V Input
crazystick 0:42ffca439fcc 34 * > 1-1.5mA measuring current
crazystick 0:42ffca439fcc 35 * > 40-50uA standby current
crazystick 0:42ffca439fcc 36 * > Humidity from 0-100% RH
crazystick 0:42ffca439fcc 37 * > -40 - 80 degrees C temperature range
crazystick 0:42ffca439fcc 38 * > +-2% RH accuracy
crazystick 0:42ffca439fcc 39 * > +-0.5 degrees C
crazystick 0:42ffca439fcc 40 *
crazystick 0:42ffca439fcc 41 * Usages
crazystick 0:42ffca439fcc 42 * #include "DHT22.h"
crazystick 0:42ffca439fcc 43 *
crazystick 0:42ffca439fcc 44 * DTH22 dth22(PinName sda, PinName scl, int addr); // pin
crazystick 0:42ffca439fcc 45 *
crazystick 0:42ffca439fcc 46 * int main() {
crazystick 0:42ffca439fcc 47 * err=dth22.readData();
crazystick 0:42ffca439fcc 48 * if (err==0) {
crazystick 0:42ffca439fcc 49 printf("Temperature is %f.2 C\n",dht22.getTemperature());
crazystick 0:42ffca439fcc 50 printf("Humidity is %f.2 \%\n",dht22.getHumidity());
crazystick 0:42ffca439fcc 51 }
crazystick 0:42ffca439fcc 52 *
crazystick 0:42ffca439fcc 53 *
crazystick 0:42ffca439fcc 54 */
crazystick 0:42ffca439fcc 55
crazystick 0:42ffca439fcc 56 #define DHT22_ERROR_VALUE -99.5
crazystick 0:42ffca439fcc 57
crazystick 0:42ffca439fcc 58 typedef enum {
crazystick 0:42ffca439fcc 59 DHT_ERROR_NONE = 0,
crazystick 0:42ffca439fcc 60 DHT_BUS_HUNG,
crazystick 0:42ffca439fcc 61 DHT_ERROR_NOT_PRESENT,
crazystick 0:42ffca439fcc 62 DHT_ERROR_ACK_TOO_LONG,
crazystick 0:42ffca439fcc 63 DHT_ERROR_SYNC_TIMEOUT,
crazystick 0:42ffca439fcc 64 DHT_ERROR_DATA_TIMEOUT,
crazystick 0:42ffca439fcc 65 DHT_ERROR_CHECKSUM,
crazystick 0:42ffca439fcc 66 DHT_ERROR_TOO_QUICK
crazystick 0:42ffca439fcc 67 } DHT22_ERROR;
crazystick 0:42ffca439fcc 68
crazystick 0:42ffca439fcc 69 class DHT22 {
crazystick 0:42ffca439fcc 70 private:
crazystick 0:42ffca439fcc 71 time_t _lastReadTime;
crazystick 0:42ffca439fcc 72 PinName _data;
crazystick 0:42ffca439fcc 73 float _lastHumidity;
crazystick 0:42ffca439fcc 74 float _lastTemperature;
crazystick 0:42ffca439fcc 75 public:
crazystick 0:42ffca439fcc 76 DHT22(PinName Data);
crazystick 0:42ffca439fcc 77 ~DHT22();
crazystick 0:42ffca439fcc 78 DHT22_ERROR readData(void);
crazystick 0:42ffca439fcc 79 float getHumidity();
crazystick 0:42ffca439fcc 80 float getTemperatureC();
crazystick 0:42ffca439fcc 81 void clockReset();
crazystick 0:42ffca439fcc 82 };
crazystick 0:42ffca439fcc 83
crazystick 0:42ffca439fcc 84 #endif /*_DHT22_H_*/