![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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@0:d2c18f736df1, 2017-04-07 (annotated)
- Committer:
- ledonger
- Date:
- Fri Apr 07 11:36:47 2017 +0000
- Revision:
- 0:d2c18f736df1
- Child:
- 1:023e1eae2048
Working version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ledonger | 0:d2c18f736df1 | 1 | #include "mbed.h" |
ledonger | 0:d2c18f736df1 | 2 | |
ledonger | 0:d2c18f736df1 | 3 | //Class to manage DHT11 sensor |
ledonger | 0:d2c18f736df1 | 4 | class DHT11{ |
ledonger | 0:d2c18f736df1 | 5 | |
ledonger | 0:d2c18f736df1 | 6 | public: |
ledonger | 0:d2c18f736df1 | 7 | /* Takes the data pin of the sensor in parameter */ |
ledonger | 0:d2c18f736df1 | 8 | DHT11(PinName pin):dataPin(pin) |
ledonger | 0:d2c18f736df1 | 9 | { |
ledonger | 0:d2c18f736df1 | 10 | iBit = 0; |
ledonger | 0:d2c18f736df1 | 11 | data = 0; |
ledonger | 0:d2c18f736df1 | 12 | } |
ledonger | 0:d2c18f736df1 | 13 | |
ledonger | 0:d2c18f736df1 | 14 | /* Request and receive new data from sensor |
ledonger | 0:d2c18f736df1 | 15 | Returns : |
ledonger | 0:d2c18f736df1 | 16 | 1 : OK |
ledonger | 0:d2c18f736df1 | 17 | -1 : Checksum Error |
ledonger | 0:d2c18f736df1 | 18 | */ |
ledonger | 0:d2c18f736df1 | 19 | int readData(void){ |
ledonger | 0:d2c18f736df1 | 20 | this->data = 0; |
ledonger | 0:d2c18f736df1 | 21 | this->dataPin.output(); |
ledonger | 0:d2c18f736df1 | 22 | // Request a measurement (low during t > 18ms) |
ledonger | 0:d2c18f736df1 | 23 | this->dataPin = 0; |
ledonger | 0:d2c18f736df1 | 24 | wait_ms(20); |
ledonger | 0:d2c18f736df1 | 25 | this->dataPin = 1; |
ledonger | 0:d2c18f736df1 | 26 | |
ledonger | 0:d2c18f736df1 | 27 | //Wait for the sensor to take control and set low level /!\ Important |
ledonger | 0:d2c18f736df1 | 28 | wait_us(20); |
ledonger | 0:d2c18f736df1 | 29 | |
ledonger | 0:d2c18f736df1 | 30 | this->dataPin.input(); |
ledonger | 0:d2c18f736df1 | 31 | |
ledonger | 0:d2c18f736df1 | 32 | //TODO Check if timing is correct (low : 80µs ; high 80µs) |
ledonger | 0:d2c18f736df1 | 33 | // Wait until end of 80µs low |
ledonger | 0:d2c18f736df1 | 34 | while(!this->dataPin.read()); |
ledonger | 0:d2c18f736df1 | 35 | // Wait until end of 80 µs high |
ledonger | 0:d2c18f736df1 | 36 | while(this->dataPin.read()); |
ledonger | 0:d2c18f736df1 | 37 | |
ledonger | 0:d2c18f736df1 | 38 | // Sensor reply 40bits |
ledonger | 0:d2c18f736df1 | 39 | for(iBit=0; iBit<40; iBit++) { |
ledonger | 0:d2c18f736df1 | 40 | this->data = this->data << 1; // Shift for new number |
ledonger | 0:d2c18f736df1 | 41 | this->timer.stop(); |
ledonger | 0:d2c18f736df1 | 42 | this->timer.reset(); |
ledonger | 0:d2c18f736df1 | 43 | |
ledonger | 0:d2c18f736df1 | 44 | // Wait for low level to end |
ledonger | 0:d2c18f736df1 | 45 | while(!this->dataPin.read()); |
ledonger | 0:d2c18f736df1 | 46 | this->timer.start(); |
ledonger | 0:d2c18f736df1 | 47 | // Count time while high level |
ledonger | 0:d2c18f736df1 | 48 | while(this->dataPin.read()); |
ledonger | 0:d2c18f736df1 | 49 | |
ledonger | 0:d2c18f736df1 | 50 | if(this->timer.read_us() > 50) |
ledonger | 0:d2c18f736df1 | 51 | { |
ledonger | 0:d2c18f736df1 | 52 | //This bit is '1' |
ledonger | 0:d2c18f736df1 | 53 | this->data++; |
ledonger | 0:d2c18f736df1 | 54 | } |
ledonger | 0:d2c18f736df1 | 55 | } |
ledonger | 0:d2c18f736df1 | 56 | |
ledonger | 0:d2c18f736df1 | 57 | wait_ms(250); |
ledonger | 0:d2c18f736df1 | 58 | |
ledonger | 0:d2c18f736df1 | 59 | //Checking checksum |
ledonger | 0:d2c18f736df1 | 60 | if((this->data & 0x00000000000000ff) != ((this->data & 0x000000ff00000000) >> 32) + |
ledonger | 0:d2c18f736df1 | 61 | ((this->data & 0x00000000ff000000) >> 24) + |
ledonger | 0:d2c18f736df1 | 62 | ((this->data & 0x0000000000ff0000) >> 16) + |
ledonger | 0:d2c18f736df1 | 63 | ((this->data & 0x000000000000ff00) >> 8)) |
ledonger | 0:d2c18f736df1 | 64 | { |
ledonger | 0:d2c18f736df1 | 65 | return -1; |
ledonger | 0:d2c18f736df1 | 66 | } |
ledonger | 0:d2c18f736df1 | 67 | |
ledonger | 0:d2c18f736df1 | 68 | return 1; |
ledonger | 0:d2c18f736df1 | 69 | } |
ledonger | 0:d2c18f736df1 | 70 | |
ledonger | 0:d2c18f736df1 | 71 | int getTemperature(void){ |
ledonger | 0:d2c18f736df1 | 72 | return (int)((this->data & 0x0000000000ff0000) >> 16); |
ledonger | 0:d2c18f736df1 | 73 | } |
ledonger | 0:d2c18f736df1 | 74 | |
ledonger | 0:d2c18f736df1 | 75 | int getHumidity(void){ |
ledonger | 0:d2c18f736df1 | 76 | return (int)((this->data & 0x000000ff00000000) >> 32); |
ledonger | 0:d2c18f736df1 | 77 | } |
ledonger | 0:d2c18f736df1 | 78 | |
ledonger | 0:d2c18f736df1 | 79 | |
ledonger | 0:d2c18f736df1 | 80 | private: |
ledonger | 0:d2c18f736df1 | 81 | DigitalInOut dataPin;//Communication with the sensor |
ledonger | 0:d2c18f736df1 | 82 | Timer timer; //initialize timer |
ledonger | 0:d2c18f736df1 | 83 | uint64_t data; // 64 bit variable for temporary data |
ledonger | 0:d2c18f736df1 | 84 | int iBit; |
ledonger | 0:d2c18f736df1 | 85 | }; |