DHT11 used for Temperature & Humidity sensor.

Dependents:   LoRaWAN_mbed_lmic_agriculture_app

Fork of DHT by Components

Revision:
4:0c34c98a87ec
Parent:
3:6937e130feca
Child:
5:e7e3db429c9e
diff -r 6937e130feca -r 0c34c98a87ec DHT.cpp
--- a/DHT.cpp	Sat May 28 11:11:34 2016 +0000
+++ b/DHT.cpp	Mon Apr 02 12:06:29 2018 +0000
@@ -1,4 +1,4 @@
-/*
+/*******************************************************************************
  *  DHT Library for  Digital-output Humidity and Temperature sensors
  *
  *  Works with DHT11, DHT22
@@ -28,26 +28,65 @@
  * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
- */
+ *
+ * /////////////////////////////////////////////////////////////////////////////
+ *
+ * Used by Giorgos Tsapparellas for Internet of Things (IoT) smart monitoring
+ * device for agriculture using LoRaWAN technology.
+ * 
+ * Sensor Used: DHT11 - Digital-output Humidity and Temperature sensor.
+ *
+ * Date of issued copy: 10 January 2018
+ *
+ * Modifications: 
+ * - No external modifications of the existing "AS IT IS" software.
+ * - Added some external comments for meeting good principles of 
+ *   source code re-usability.   
+ ******************************************************************************/
 
 #include "DHT.h"
 
-#define DHT_DATA_BIT_COUNT 40
+#define DHT_DATA_BIT_COUNT 40 // Data bit count set to 40
+
+/* Uncomment this line for configuring pc variable for UART usage. */
+//Serial pc(USBTX, USBRX); // UART tranmitter(tx) and receiver(rx).
 
+/* 
+* DHT constructor.
+* Input parameters: PinName pin
+*                   eType DHTtype
+*
+*/ 
 DHT::DHT(PinName pin, eType DHTtype)
 {
+    
     _pin = pin;
     _DHTtype = DHTtype;
     _firsttime = true;
-}
+    
+} // end of DHT constructor. 
 
+/* 
+* Null DHT destructor.
+*
+*/ 
 DHT::~DHT()
 {
     
-}
+} // end of DHT destructor. 
 
+/* 
+* stall DHT function of type eError.
+*
+* Input parameters: DigitalInOut io
+*                   int const level
+*                   int const max_time
+*
+* Return: ERROR_NO_PATIENCE or ERROR_NONE
+*/ 
 eError DHT::stall(DigitalInOut &io, int const level, int const max_time)
 {
+    
     int cnt = 0;
     while (level == io) {
         if (cnt > max_time) {
@@ -57,8 +96,17 @@
         wait_us(1);
     }
     return ERROR_NONE;
-}
+    
+} // end of stall function.
 
+/* 
+* readData DHT function of type eError.
+*
+* Input parameters: None
+*
+* Return: Either BUS_BUSY, ERROR_NOT_PRESENT, ERROR_SYNC_TIMEOUT, 
+*         ERROR_NO_PATIENCE, ERROR_DATA_TIMEOUT or ERROR_CHECKSUM.     
+*/ 
 eError DHT::readData()
 {
     uint8_t i = 0, j = 0, b = 0, data_valid = 0;
@@ -69,36 +117,37 @@
 
     DigitalInOut DHT_io(_pin);
 
-    // IO must be in hi state to start
+    // I/O must be in high state to start
     if (ERROR_NONE != stall(DHT_io, 0, 250)) {
         return BUS_BUSY;
     }
 
-    // start the transfer
+    // Start the transfer
     DHT_io.output();
     DHT_io = 0;
     wait_ms(18);
     DHT_io = 1;
     wait_us(30);
     DHT_io.input();
-    // wait till the sensor grabs the bus
+    
+    // Wait until the sensor grabs the bus
     if (ERROR_NONE != stall(DHT_io, 1, 100)) {
         return ERROR_NOT_PRESENT;
     }
-    // sensor should signal low 80us and then hi 80us
+    // Sensor should signal low 80us and then high 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
+    // Capture the data
     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
+            // Logic 0 is 28us max, 1 is 70us
             wait_us(40);
             bit_value[i*8+j] = DHT_io;
             if (ERROR_NONE != stall(DHT_io, 1, 50)) {
@@ -106,7 +155,7 @@
             }
         }
     }
-    // store the data
+    // Store the data
     for (i = 0; i < 5; i++) {
         b=0;
         for (j=0; j<8; j++) {
@@ -117,8 +166,8 @@
         DHT_data[i]=b;
     }
 
-    // uncomment to see the checksum error if it exists
-    //printf(" 0x%02x + 0x%02x + 0x%02x + 0x%02x = 0x%02x \n", DHT_data[0], DHT_data[1], DHT_data[2], DHT_data[3], DHT_data[4]);
+    // Uncomment to see the checksum error if it exists
+    //pc.printf(" 0x%02x + 0x%02x + 0x%02x + 0x%02x = 0x%02x \n", DHT_data[0], DHT_data[1], DHT_data[2], DHT_data[3], DHT_data[4]);
     data_valid = DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3];
     if (DHT_data[4] == data_valid) {
         _lastReadTime = currentTime;
@@ -130,11 +179,19 @@
     }
 
     return err;
+    
+} // end of readData function.
 
-}
-
+/* 
+* CalcTemperature DHT function of type float.
+*
+* Input parameters: None
+*
+* Return: float temperature value.     
+*/ 
 float DHT::CalcTemperature()
 {
+    
     int v;
 
     switch (_DHTtype) {
@@ -151,25 +208,54 @@
             return float(v);
     }
     return 0;
-}
+    
+} // end of CalcTemperature function.
 
+/*
+* ReadHumidity DHT function of type float.
+*
+* Input parameters: None
+*
+* Return: float last humidity value.     
+*/ 
 float DHT::ReadHumidity()
 {
     return _lastHumidity;
-}
+} // end of ReadHumidity function.
 
+/*
+* ConvertCelsiustoFarenheit DHT function of type float.
+*
+* Input parameters: float const celsius
+*
+* Return: float farenheit temperature value.     
+*/ 
 float DHT::ConvertCelciustoFarenheit(float const celsius)
 {
     return celsius * 9 / 5 + 32;
-}
+} // end of ConvertCelciustoFarenheit function.
 
+/*
+* ConvertCelsiustoKelvin DHT function of type float.
+*
+* Input parameters: float const celsius
+*
+* Return: float kelvin temperature value.     
+*/ 
 float DHT::ConvertCelciustoKelvin(float const celsius)
 {
     return celsius + 273.15f;
-}
+} // end of ConvertCelciusKelvin function.
 
-// dewPoint function NOAA
-// reference: http://wahiduddin.net/calc/density_algorithms.htm
+/*
+* CalcdewPoint DHT function of type float.
+*
+* Input parameters: float const celsius
+*                   float const humidity
+*
+* Return: dewPoint.     
+* Reference: http://wahiduddin.net/calc/density_algorithms.htm
+*/ 
 float DHT::CalcdewPoint(float const celsius, float const humidity)
 {
     float A0= 373.15f/(273.15f + celsius);
@@ -181,11 +267,18 @@
     float VP = pow(10, SUM-3) * humidity;
     float T = log(VP/0.61078f);   // temp var
     return (241.88f * T) / (17.558f-T);
-}
+    
+} // end of CalcdewPoint function.
 
-// delta max = 0.6544 wrt dewPoint()
-// 5x faster than dewPoint()
-// reference: http://en.wikipedia.org/wiki/Dew_point
+/*
+* CalcdewPointFast DHT function of type float.
+*
+* Input parameters: float const celsius
+*                   float const humidity
+*
+* Return: 5x faster dewPoint.     
+* Reference: http://en.wikipedia.org/wiki/Dew_point
+*/ 
 float DHT::CalcdewPointFast(float const celsius, float const humidity)
 {
     float a = 17.271;
@@ -193,8 +286,17 @@
     float temp = (a * celsius) / (b + celsius) + log(humidity/100);
     float Td = (b * temp) / (a - temp);
     return Td;
-}
+    
+} // end of CalcdewPointFast function 
 
+/*
+* ReadTemperature DHT function of type float.
+*
+* Input parameters: eScale Scale
+*
+* Return: float scaled temperature value.     
+* Reference: http://en.wikipedia.org/wiki/Dew_point
+*/ 
 float DHT::ReadTemperature(eScale Scale)
 {
     if (Scale == FARENHEIT)
@@ -203,8 +305,15 @@
         return ConvertCelciustoKelvin(_lastTemperature);
     else
         return _lastTemperature;
-}
+} // end of ReadTemperature function.
 
+/*
+* Calc DHT function of type float.
+*
+* Input parameters: None
+*
+* Return: float humidity value.     
+*/ 
 float DHT::CalcHumidity()
 {
     int v;
@@ -221,6 +330,4 @@
             return float(v);
     }
     return 0;
-}
-
-
+} // end of CalcHumidity function. 
\ No newline at end of file