Project aiming to do a Bluetooth Low Energy IoT devices, which measure temperature and humidity. Bluetooth achieved with the IDB05A1 shield. Temperature/humidity achieved with a DHT11 sensor. Project working, tested on an STM32 L476 board.

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

DHT11.h

Committer:
ledonger
Date:
2017-04-18
Revision:
2:c0bd998cb02f
Parent:
1:023e1eae2048

File content as of revision 2:c0bd998cb02f:

#ifndef DHT11_H
#define DHT11_H

//Class to manage DHT11 sensor
class DHT11{
  
    public:
        /* Takes the data pin of the sensor in parameter */
        DHT11(PinName pin):dataPin(pin)
        {
            iBit = 0;
            data = 0;
        }  
        
        /*  Request and receive new data from sensor
            Returns : 
                1 : OK
                -1 : Checksum Error
        */
        int readData(void){
            this->data = 0;
            this->dataPin.output(); 
            // Request a measurement (low during t > 18ms)
            this->dataPin = 0;
            wait_ms(20);
            this->dataPin = 1;
            
            //Wait for the sensor to take control and set low level /!\ Important
            wait_us(20);
            
            this->dataPin.input();
            
        //TODO Check if timing is correct (low : 80µs ; high 80µs)
            // Wait until end of 80µs low
            while(!this->dataPin.read());
            // Wait until end of 80 µs high
            while(this->dataPin.read());
            
            // Sensor reply 40bits 
            for(iBit=0; iBit<40; iBit++) {
                this->data = this->data << 1; // Shift for new number
                this->timer.stop(); 
                this->timer.reset(); 
                
                // Wait for low level to end
                while(!this->dataPin.read());       
                this->timer.start();     
                // Count time while high level     
                while(this->dataPin.read());
                
                if(this->timer.read_us() > 50) 
                {
                    //This bit is '1'
                    this->data++;
                }
            }
            
            wait_ms(250);
            
            //Checking checksum
            if((this->data & 0x00000000000000ff) != ((this->data & 0x000000ff00000000) >> 32) + 
                                                    ((this->data & 0x00000000ff000000) >> 24) + 
                                                    ((this->data & 0x0000000000ff0000) >> 16) + 
                                                    ((this->data & 0x000000000000ff00) >> 8))
            {
                return -1;        
            }
            
            return 1;
        }
        
        int getTemperature(void){
            return (int)((this->data & 0x0000000000ff0000) >> 16);
        }
        
        int getHumidity(void){
            return (int)((this->data & 0x000000ff00000000) >> 32);
        }
        
    
    private:
        DigitalInOut dataPin;//Communication with the sensor
        Timer timer; //initialize timer
        uint64_t data; // 64 bit variable for temporary data
        int iBit;
};


#endif