This is an example program to fetch data of DHT11 Sensors

Dependencies:   mbed

Fork of DHT11_with_Nucleo by Adatgy2014

Files at this revision

API Documentation at this revision

Comitter:
ledonger
Date:
Wed Apr 05 13:14:12 2017 +0000
Parent:
11:1379308ecace
Commit message:
Solved problem while reading data, because of an offset of 20?s before the sensor takes control of the data bus

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 1379308ecace -r 31645d013d6c main.cpp
--- a/main.cpp	Tue Jan 20 02:21:22 2015 +0000
+++ b/main.cpp	Wed Apr 05 13:14:12 2017 +0000
@@ -1,5 +1,7 @@
 #include "mbed.h"
 
+//Tested with success on STM32 Nucleo L476
+
 DigitalOut myled(LED1); // Activate LED
 DigitalIn mybutton(USER_BUTTON); // Activate button
 DigitalInOut data_pin(A0); // Activate digital in
@@ -9,30 +11,34 @@
 uint64_t adat; // 64 bit variable for temporary data
 int i;
 
+
 // Function to initialize DHT11
 void dht_read(void) {
-    data_pin.output(); // Set A0 as output
+    data_pin.output(); 
     // Initialize measurement > 18 ms low
     data_pin = 0;
     wait_ms(20);
     // After high and release the pin switch input mode
     data_pin = 1;
+    
+    wait_us(20);//Wait for the sensor to take control and set low level /!\ Important
+    
     data_pin.input();
     // Wait until the end of 80 us low
-    while(!data_pin.read()) {}
+    while(!data_pin.read());
     // Wait until end of 80 us high
-    while(data_pin.read()) {}
+    while(data_pin.read());
     // 40 bit, 40 read out cycle
     for(i=0; i<40; i++) {
         adat = adat << 1; // Shift for new number
         tmr.stop(); // Stop timer if runs
         tmr.reset();  // Reset timer
         // Wait until pin
-        while(!data_pin.read()) {}          
+        while(!data_pin.read());       
         tmr.start();            
-        while(data_pin.read()) {}
+        while(data_pin.read());
         // If DHT11 HIGH longer than 40 micro seconds (hopefully 70 us)
-        if(tmr.read_us() > 40) {
+        if(tmr.read_us() > 50) {
             // bit is 1
             adat++;
         }
@@ -42,20 +48,29 @@
 int main() {
     pc.printf("Read the DHT11 temperature and humidity sensor!\n"); //Welcome message
     while(1) {
-        if (mybutton == 0) { // Button is pressed
+        if(mybutton == 0){ // Button is pressed
             // Reset adat variable
             adat = 0;
             myled = 1; // LED is ON
             dht_read(); // Call the function
             // Send result through UART result
-            pc.printf("%d", (adat  & 0x000000ff00000000) >> 32); // Humidity
-            pc.printf("\n\r"); // Send a new line and carriage return.
-            pc.printf("%d", (adat & 0x0000000000ff0000) >> 16 ); // Temperature
-            pc.printf("\n\r");
-            pc.printf("%d", adat & 0x00000000000000ff); // Checksum.
-            pc.printf("\n\r");
-            wait_ms(200); // Wait 0.2 sec till continue.
-        } else {
+            //pc.printf("Raw data:%10x\n\r",adat);
+            pc.printf("Humidity:%d%%\n\r", (adat  & 0x000000ff00000000) >> 32); // Humidity
+            pc.printf("Temperature:%d degrees\n\r", (adat & 0x0000000000ff0000) >> 16 ); // Temperature
+            if((adat&0x00000000000000ff) == ((adat&0x000000ff00000000) >> 32) + ((adat&0x00000000ff000000) >> 24)
+                + ((adat&0x0000000000ff0000) >> 16) + ((adat&0x000000000000ff00) >> 8)){
+                pc.printf("Checksum : OK\n\r");        
+            }
+            else{
+                pc.printf("Checksum : Error\n\r");   
+            }
+            
+            //pc.printf("Checksum:%d\n\r", adat & 0x00000000000000ff); // Checksum.
+            /*pc.printf("Checksum calculated:%d\n\r",((adat&0x000000ff00000000) >> 32) + ((adat  & 0x00000000ff000000) >> 24)
+                + ((adat & 0x0000000000ff0000) >> 16) + ((adat & 0x000000000000ff00) >> 8));*/
+            wait_ms(300); // Wait 0.2 sec till continue.
+        } 
+        else{
             myled = 0; // LED is OFF
         }
     }
diff -r 1379308ecace -r 31645d013d6c mbed.bld
--- a/mbed.bld	Tue Jan 20 02:21:22 2015 +0000
+++ b/mbed.bld	Wed Apr 05 13:14:12 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5
\ No newline at end of file
+https://mbed.org/users/mbed_official/code/mbed/builds/856d2700e60b
\ No newline at end of file