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