Temp and Humidity sensor device driver. had to fork under a new name, don't know why yet.
Dependents: Wio-example-SORACOM-Harvest
Diff: DHT.cpp
- Revision:
- 6:49350e4b126d
- Parent:
- 4:860d5d055148
- Child:
- 7:a43e070f753f
--- a/DHT.cpp Thu Feb 21 13:03:47 2019 -0400 +++ b/DHT.cpp Wed May 08 13:47:41 2019 -0400 @@ -1,14 +1,15 @@ -/* - * DHT Library for Digital-output Humidity and Temperature sensors - * - * Works with DHT11, DHT22 +/* + * DHT Library for Digital-output Humidity and Temperature sensors + * + * Works with DHT11, DHT21, DHT22 * SEN11301P, Grove - Temperature&Humidity Sensor (Seeed Studio) * SEN51035P, Grove - Temperature&Humidity Sensor Pro (Seeed Studio) - * AM2302 , temperature-humidity sensor - * HM2303 , Digital-output humidity and temperature sensor + * AM2302 , temperature-humidity sensor + * RHT01,RHT02, RHT03 , Humidity and Temperature Sensor (Sparkfun) * - * Copyright (C) Wim De Roeve - * based on DHT22 sensor library by HO WING KIT + * Copyright (C) Luis Amell + * Based on AM2302/DHT22 sensor library by Wim De Roeve + * Based on DHT22 sensor library by HO WING KIT * Arduino DHT11 library * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -32,173 +33,194 @@ #include "DHT.h" -#define DHT_DATA_BIT_COUNT 40 +#define DHT_DATA_BIT_COUNT 41 -DHT::DHT(PinName pin, eType DHTtype) -{ +DHT::DHT(PinName pin,int DHTtype) { _pin = pin; _DHTtype = DHTtype; - _firsttime = true; + _firsttime=true; } -DHT::~DHT() -{ - +DHT::~DHT() { } -eError DHT::stall(DigitalInOut &io, int const level, int const max_time) -{ - int cnt = 0; - while (level == io) { - if (cnt > max_time) { - return ERROR_NO_PATIENCE; - } - cnt++; - wait_us(1); - } - return ERROR_NONE; -} +int DHT::readData() { + int i, j, retryCount,b; + unsigned int bitTimes[DHT_DATA_BIT_COUNT]; -eError DHT::readData() -{ - uint8_t i = 0, j = 0, b = 0, data_valid = 0; - uint32_t bit_value[DHT_DATA_BIT_COUNT] = {0}; - + int retries=0; + eError err = ERROR_NONE; time_t currentTime = time(NULL); - +retry_point: DigitalInOut DHT_io(_pin); - // IO must be in hi state to start - if (ERROR_NONE != stall(DHT_io, 0, 250)) { - return BUS_BUSY; + for (i = 0; i < DHT_DATA_BIT_COUNT; i++) { + bitTimes[i] = 0; } - // start the transfer + if (!_firsttime) { + if (int(currentTime - _lastReadTime) < 2) { + err = ERROR_NO_PATIENCE; + return err; + } + } else { + _firsttime=false; + _lastReadTime=currentTime; + } + retryCount = 0; + + do { + if (retryCount > 125) { + err = BUS_BUSY; + return err; + } + retryCount ++; + wait_us(2); + } while ((DHT_io==0)); + + DHT_io.output(); DHT_io = 0; wait_ms(18); DHT_io = 1; - wait_us(30); + wait_us(40); DHT_io.input(); - // wait till the sensor grabs the bus - if (ERROR_NONE != stall(DHT_io, 1, 100)) { - return ERROR_NOT_PRESENT; + + retryCount = 0; + do { + if (retryCount > 40) { + err = ERROR_NOT_PRESENT; + return err; + } + retryCount++; + wait_us(1); + } while ((DHT_io==1)); + + if (err != ERROR_NONE) { + return err; } - // sensor should signal low 80us and then hi 80us - if (ERROR_NONE != stall(DHT_io, 0, 100)) { - return ERROR_SYNC_TIMEOUT; - } - if (ERROR_NONE != stall(DHT_io, 1, 100)) { - return ERROR_NO_PATIENCE; - } - // capture the data + + wait_us(80); + for (i = 0; i < 5; i++) { for (j = 0; j < 8; j++) { - if (ERROR_NONE != stall(DHT_io, 0, 75)) { - return ERROR_DATA_TIMEOUT; - } - // logic 0 is 28us max, 1 is 70us + + retryCount = 0; + do { + if (retryCount > 75) { + err = ERROR_DATA_TIMEOUT; + return err; + } + retryCount++; + wait_us(1); + } while (DHT_io == 0); wait_us(40); - bit_value[i*8+j] = DHT_io; - if (ERROR_NONE != stall(DHT_io, 1, 50)) { - return ERROR_DATA_TIMEOUT; + bitTimes[i*8+j]=DHT_io; + + int count = 0; + while (DHT_io == 1 && count < 100) { + wait_us(1); + count++; } } } - // store the data + DHT_io.output(); + DHT_io = 1; for (i = 0; i < 5; i++) { b=0; for (j=0; j<8; j++) { - if (bit_value[i*8+j] == 1) { - b |= (1 << (7-j)); + if (bitTimes[i*8+j+1] > 0) { + b |= ( 1 << (7-j)); } } DHT_data[i]=b; } - // uncomment to see the checksum error if it exists - data_valid = DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]; - - //printf(" 0x%02X + 0x%02X + 0x%02X + 0x%02X => Snsr Chk: 0x%02X Rcvd Chk: 0x%02X\n", DHT_data[0], DHT_data[1], - // DHT_data[2], DHT_data[3], DHT_data[4], data_valid); - - if (DHT_data[4] == data_valid) { + //if (DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]) & 0xFF)) { + int8_t errCalc=DHT_data[4]-(DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]); + if (errCalc>-2 && errCalc<2) { _lastReadTime = currentTime; - _lastTemperature = CalcTemperature(); - _lastHumidity = CalcHumidity(); + _lastTemperature=CalcTemperature(); + _lastHumidity=CalcHumidity(); } else { - err = ERROR_CHECKSUM; + retries+=1; + + if (retries > 3) { + _lastReadTime = currentTime; + _lastTemperature=CalcTemperature(); + _lastHumidity=CalcHumidity(); + //err = ERROR_CHECKSUM; + return (DHT_data[4]-(DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3])); + } else { + wait_ms(200); + goto retry_point; + } } return err; - + } -float DHT::CalcTemperature() -{ +float DHT::CalcTemperature() { int v; - float result; - + switch (_DHTtype) { case DHT11: v = DHT_data[2]; return float(v); case DHT22: - //v = DHT_data[0]; - //v *= 256; - //v += DHT_data[1]; - result = ((DHT_data[2] * 256)+ DHT_data[3]) /10.0f; - return float(result); + v = DHT_data[2] & 0x7F; + v *= 256; + v += DHT_data[3]; +// v /= 10; + if (DHT_data[2] & 0x80) + v *= -1; + return (float(v)/10); } return 0; } -float DHT::ReadHumidity() -{ +float DHT::ReadHumidity() { return _lastHumidity; } -float DHT::ConvertCelciustoFarenheit(float const celsius) -{ +float DHT::ConvertCelciustoFarenheit(float celsius) { return celsius * 9 / 5 + 32; } -float DHT::ConvertCelciustoKelvin(float const celsius) -{ - return celsius + 273.15f; +float DHT::ConvertCelciustoKelvin(float celsius) { + return celsius + 273.15; } // dewPoint function NOAA // reference: http://wahiduddin.net/calc/density_algorithms.htm -float DHT::CalcdewPoint(float const celsius, float const humidity) -{ - float A0= 373.15f/(273.15f + celsius); +float DHT::CalcdewPoint(float celsius, float humidity) { + float A0= 373.15/(273.15 + celsius); float SUM = -7.90298 * (A0-1); - SUM += 5.02808f * log10(A0); - SUM += -1.3816e-7 * (pow(10, (11.344f*(1-1/A0)))-1) ; + SUM += 5.02808 * log10(A0); + SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ; SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; SUM += log10(1013.246); float VP = pow(10, SUM-3) * humidity; - float T = log(VP/0.61078f); // temp var - return (241.88f * T) / (17.558f-T); + float T = log(VP/0.61078); // temp var + return (241.88 * T) / (17.558-T); } // delta max = 0.6544 wrt dewPoint() // 5x faster than dewPoint() // reference: http://en.wikipedia.org/wiki/Dew_point -float DHT::CalcdewPointFast(float const celsius, float const humidity) +float DHT::CalcdewPointFast(float celsius, float humidity) { - float a = 17.271; - float b = 237.7; - float temp = (a * celsius) / (b + celsius) + log(humidity/100); - float Td = (b * temp) / (a - temp); - return Td; + float a = 17.271; + float b = 237.7; + float temp = (a * celsius) / (b + celsius) + log(humidity/100); + float Td = (b * temp) / (a - temp); + return Td; } -float DHT::ReadTemperature(eScale Scale) -{ +float DHT::ReadTemperature(eScale Scale) { if (Scale == FARENHEIT) return ConvertCelciustoFarenheit(_lastTemperature); else if (Scale == KELVIN) @@ -207,21 +229,19 @@ return _lastTemperature; } -float DHT::CalcHumidity() -{ +float DHT::CalcHumidity() { int v; - float result; - + switch (_DHTtype) { case DHT11: v = DHT_data[0]; return float(v); case DHT22: - //v = DHT_data[0]; - //v *= 256; - //v += DHT_data[1]; - result = ((DHT_data[0] * 256)+ DHT_data[1]) /10.0f; - return float(result); + v = DHT_data[0]; + v *= 256; + v += DHT_data[1]; + //v /= 10; + return (float(v)/10); } return 0; }