My_microcontroller

Dependencies:   mbed

Fork of DHT-11Mine by Umair Aftab

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DHT.cpp Source File

DHT.cpp

00001 #include "DHT.h"
00002 #define DHT_DATA_BIT_COUNT 40
00003 
00004 DHT::DHT(PinName pin,int DHTtype) {
00005     _pin = pin;
00006     _DHTtype = DHTtype;
00007     _firsttime=true;
00008 }
00009 
00010 DHT::~DHT() {
00011 }
00012 
00013 int DHT::readData() {
00014     int i, j, retryCount,b;
00015     unsigned int bitTimes[DHT_DATA_BIT_COUNT];
00016 
00017     eError err = ERROR_NONE;
00018     time_t currentTime = time(NULL);
00019 
00020     DigitalInOut DHT_io(_pin);
00021 
00022     for (i = 0; i < DHT_DATA_BIT_COUNT; i++) {
00023         bitTimes[i] = 0;
00024     }
00025 
00026     if (!_firsttime) {
00027         if (int(currentTime - _lastReadTime) < 2) {
00028             err = ERROR_NO_PATIENCE;
00029             return err;
00030         }
00031     } else {
00032         _firsttime=false;
00033         _lastReadTime=currentTime;
00034     }
00035     retryCount = 0;
00036 
00037     do {
00038         if (retryCount > 125) {
00039             err = BUS_BUSY;
00040             return err;
00041         }
00042         retryCount ++;
00043         wait_us(2);
00044     } while ((DHT_io==0));
00045 
00046 
00047     DHT_io.output();
00048     DHT_io = 0;
00049     wait_ms(18);
00050     DHT_io = 1;
00051     wait_us(40);
00052     DHT_io.input();
00053 
00054     retryCount = 0;
00055     do {
00056         if (retryCount > 40)  {
00057             err = ERROR_NOT_PRESENT;
00058             return err;
00059         }
00060         retryCount++;
00061         wait_us(1);
00062     } while ((DHT_io==1));
00063 
00064     if (err != ERROR_NONE) {
00065         return err;
00066     }
00067 
00068     wait_us(80);
00069 
00070     for (i = 0; i < 5; i++) {
00071         for (j = 0; j < 8; j++) {
00072 
00073             retryCount = 0;
00074             do {
00075                 if (retryCount > 75)  {
00076                     err = ERROR_DATA_TIMEOUT;
00077                     return err;
00078                 }
00079                 retryCount++;
00080                 wait_us(1);
00081             } while (DHT_io == 0);
00082             wait_us(40);
00083             bitTimes[i*8+j]=DHT_io;
00084 
00085             int count = 0;
00086             while (DHT_io == 1 && count < 100) {
00087                 wait_us(1);
00088                 count++;
00089             }
00090         }
00091     }
00092     DHT_io.output();
00093     DHT_io = 1;
00094     for (i = 0; i < 5; i++) {
00095         b=0;
00096         for (j=0; j<8; j++) {
00097             if (bitTimes[i*8+j+1] > 0) {
00098                 b |= ( 1 << (7-j));
00099             }
00100         }
00101         DHT_data[i]=b;
00102     }
00103 
00104         _lastReadTime = currentTime;
00105         _lastTemperature=CalcTemperature();
00106         _lastHumidity=CalcHumidity();
00107     return err;
00108 
00109 }
00110 
00111 float DHT::CalcTemperature() {
00112     int v;
00113     v = DHT_data[2];
00114     return float(v);
00115 }
00116 
00117 float DHT::ReadHumidity() {
00118     return _lastHumidity;
00119 }
00120 
00121 float DHT::ConvertCelciustoFarenheit(float celsius) {
00122     return celsius * 9 / 5 + 32;
00123 }
00124 
00125 float DHT::ConvertCelciustoKelvin(float celsius) {
00126     return celsius + 273.15;
00127 }
00128 
00129 
00130 float DHT::ReadTemperature(eScale Scale) {
00131     if (Scale == FARENHEIT)
00132         return ConvertCelciustoFarenheit(_lastTemperature);
00133     else if (Scale == KELVIN)
00134         return ConvertCelciustoKelvin(_lastTemperature);
00135     else
00136         return _lastTemperature;
00137 }
00138 
00139 float DHT::CalcHumidity() {
00140     int v;
00141 
00142     v = DHT_data[0];
00143     return float(v);
00144         
00145 }
00146