Serial printf removed in other to use the nRF52 without BLE Serial

Dependents:   BLE_Bike

Fork of DHT22 by HO WING KIT

DHT22.cpp

Committer:
hwkit
Date:
2011-07-04
Revision:
1:5b20ff4fd227
Parent:
0:547e68daeb1b
Child:
2:340957cc8fef

File content as of revision 1:5b20ff4fd227:

/*
    DHT22.cpp - DHT22 sensor library
    Developed by Ben Adams = 2011

    This library is free software; you can redistribute it and / or
    modify it under the terms of the GNU Leser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRENTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PATRICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA


    Humidity and Temperature Sensor DHT22 info found at
    http://www.sparkfun.com/products/10167

    Version 0.4: 24-Jan-2011 by Ben Adams
    Added return code constants to keywords.txt
    Return DHT_ERROR_CHECKSUM on check sum mismatch

    Version 0.3: 17-Jan-2011 by Ben Adams
    This version reads data
    Needs check sum code added at the end of readData

    Version 0.2: 16-Jan-2011 by Ben Adams
    Changed coding style to match other Arduino Libraries.
    This version will not read data either!

*/

#include "DHT22.h"


// This should be 40, but the sensor is adding an extra bit at the start
#define DHT22_DATA_BIT_COUNT 41

Serial pc(USBTX, USBRX);   // Tx, Rx Using USB Virtual Serial Port
                           // Read Data From /etc/ttyACM* (linux port)

DHT22::DHT22(PinName Data) {
    _data = Data;                // Set Data Pin
    _lastReadTime = time(NULL);
    _lastHumidity = 0;
    _lastTemperature = DHT22_ERROR_VALUE;
}

DHT22::~DHT22() {
}

DHT22_ERROR DHT22::readData() {
    int i, retryCount;
    int currentTemperature=0;
    int currentHumidity=0;
    unsigned int checkSum = 0, csPart1, csPart2, csPart3, csPart4;
    unsigned int bitTimes[DHT22_DATA_BIT_COUNT];
    time_t currentTime = time(NULL);

    DigitalInOut  DATA(_data);

       
    for (i = 0; i < DHT22_DATA_BIT_COUNT; i++) {
        bitTimes[i] = 0;
    }
    
    if (currentTime - _lastReadTime < 2) {
        // Caller needs to wait 2 seconds between each call to read Data        
        pc.printf("Current Time: %s\n",currentTime);
        pc.printf("Last Read Time: %s\n", _lastReadTime);
        return DHT_ERROR_TOOQUICK;
    }
    _lastReadTime = currentTime;
    retryCount = 0;
    // Pin needs to start HIGH, wait unit it is HIGH with a timeout
    do {
        if (retryCount > 125) {
            return DHT_BUS_HUNG;
        }
        retryCount ++;
        wait_us(2);
    } while (DATA==1);
    // Send the activate pulse
    DATA = 0;        // MCU send out start signal to DHT22
    wait_us(1100);   // 1.1 ms
    // Find the start of the ACK Pulse
    retryCount = 0;
    do {
        if (retryCount > 25)  {// (Spec is 20 to 40 us, 25*2 == 50us
            return DHT_ERROR_NOT_PRESENT;
        }
        retryCount ++;
        wait_us(2);
    } while (DATA==1);
    // Find the end of the ACK Pulse
    retryCount = 0;
    do {
        if (retryCount > 50) {// (Spec is 80 us, 50 * 2 == 100us)
            return DHT_ERROR_ACK_TOO_LONG;
        }
        retryCount++;
        wait_us(2);
    } while (DATA==0);
    // Read the 40 bit data stream
    for (i=0; i < DHT22_DATA_BIT_COUNT; i++) {
        retryCount = 0;
        do {
            if (retryCount > 35) { // spec is 50 u, 35*2 = 70 us
                return DHT_ERROR_SYNC_TIMEOUT;
            }
            retryCount ++;
            wait_us(2);
        } while (DATA==1);
        // Measure the width of the data pulse
        retryCount = 0;
        do {
            if (retryCount > 50) { // spec is 80us, 50*2 == 100us
                return DHT_ERROR_DATA_TIMEOUT;
            }
            retryCount++;
            wait_us(2);
        } while (DATA==0);
        bitTimes[i] = retryCount;
    }

    // Now bitTimes have the number of retries (us *2)
    // that were needed to find the end of each data bit
    // Spec: 0 is 26 to 28 us
    // Spec: 1 is 70 us
    // bitTimes[x] <= 11 is a 0
    // bitTimes[x] > 11 is a 1
    // Note: the bits are offset by one from the data sheet, not sure why
    for (i=0; i<16; i++) {
        if (bitTimes[i+1] > 11) {
            currentHumidity |= ( 1 << (15-i));
        }
    }

    for (i=0; i<16; i ++) {
        if (bitTimes[i+17] > 11) {
            currentTemperature |= (1 <<(15-i));
        }
    }

    for (i=0; i<8; i++) {
        if (bitTimes[i+33]>11) {
            checkSum |= (1 << (7-i));
        }
    }
    _lastHumidity = (float(currentHumidity & 0x7FFF) / 10.0);
    if (currentTemperature &= 0x8000) {
        currentTemperature &= 0x7FFF;
        _lastTemperature = (float(currentTemperature) / 10.0) * -1.0;
    } else {
        _lastTemperature = float(currentTemperature) / 10.0;
    }

    // Calculate Check Sum
    csPart1 = currentHumidity >> 8;
    csPart2 = currentHumidity & 0xFF;
    csPart3 = currentTemperature >> 8;
    csPart4 = currentTemperature & 0xFF;
    if (checkSum == ((csPart1 + csPart2 + csPart3 + csPart4) & 0xFF)) {
        return DHT_ERROR_NONE;
    }
    return DHT_ERROR_CHECKSUM;
}

float DHT22::getTemperatureC() {
    return _lastTemperature;
}

float DHT22::getHumidity() {
    return _lastHumidity;
}