Library for the control of the DHT22.

Dependents:   Interfacage_Disco_DHT22

Committer:
dacamposol
Date:
Wed Apr 15 18:45:36 2020 +0000
Revision:
0:a4f404326ee0
Child:
1:3385bd532812
Library for the control of the sensor DHT22.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dacamposol 0:a4f404326ee0 1 #include "DHT22.h"
dacamposol 0:a4f404326ee0 2
dacamposol 0:a4f404326ee0 3 DHT22::DHT22(PinName pin)
dacamposol 0:a4f404326ee0 4 {
dacamposol 0:a4f404326ee0 5 pin_name = pin;
dacamposol 0:a4f404326ee0 6
dacamposol 0:a4f404326ee0 7 DigitalInOut dht(pin_name);
dacamposol 0:a4f404326ee0 8 dht.output();
dacamposol 0:a4f404326ee0 9 dht.write(1);
dacamposol 0:a4f404326ee0 10 }
dacamposol 0:a4f404326ee0 11
dacamposol 0:a4f404326ee0 12 short DHT22::shortFromBits(bool bits[])
dacamposol 0:a4f404326ee0 13 {
dacamposol 0:a4f404326ee0 14 short result = 0;
dacamposol 0:a4f404326ee0 15 for (int i = 0; i<8; i++) {
dacamposol 0:a4f404326ee0 16 result += bits[i]*(1 << (7-i));
dacamposol 0:a4f404326ee0 17 }
dacamposol 0:a4f404326ee0 18 return result;
dacamposol 0:a4f404326ee0 19 }
dacamposol 0:a4f404326ee0 20
dacamposol 0:a4f404326ee0 21 // Implementation for reading a DHT22 according to the datasheet in
dacamposol 0:a4f404326ee0 22 // https://www.sparkfun.com/datasheets/Sensors/Temperature/DHT22.pdf
dacamposol 0:a4f404326ee0 23 int DHT22::read()
dacamposol 0:a4f404326ee0 24 {
dacamposol 0:a4f404326ee0 25 Timer period;
dacamposol 0:a4f404326ee0 26
dacamposol 0:a4f404326ee0 27 DigitalInOut dht(pin_name);
dacamposol 0:a4f404326ee0 28
dacamposol 0:a4f404326ee0 29 // First, we send a low signal for 1ms to the sensor
dacamposol 0:a4f404326ee0 30 dht.output();
dacamposol 0:a4f404326ee0 31 dht.write(0);
dacamposol 0:a4f404326ee0 32 thread_sleep_for(2);
dacamposol 0:a4f404326ee0 33
dacamposol 0:a4f404326ee0 34 // Now, we pull-up the voltage and wait for the MCU answer
dacamposol 0:a4f404326ee0 35 dht.write(1);
dacamposol 0:a4f404326ee0 36 dht.input();
dacamposol 0:a4f404326ee0 37 // According to the documentation, the DHT will take between 20-40us to answer, so we just wait the minimum time
dacamposol 0:a4f404326ee0 38 // Also, if the sensor doesn't change its status from 1, then it's broken.
dacamposol 0:a4f404326ee0 39 period.start();
dacamposol 0:a4f404326ee0 40 while (dht) {
dacamposol 0:a4f404326ee0 41 if (period.read() > 1) {
dacamposol 0:a4f404326ee0 42 return -1;
dacamposol 0:a4f404326ee0 43 }
dacamposol 0:a4f404326ee0 44 }
dacamposol 0:a4f404326ee0 45 period.stop();
dacamposol 0:a4f404326ee0 46
dacamposol 0:a4f404326ee0 47 // Now the DHT has set to zero, it shall last 80us and then be another 80us in up
dacamposol 0:a4f404326ee0 48 wait_us(80);
dacamposol 0:a4f404326ee0 49 wait_us(80);
dacamposol 0:a4f404326ee0 50
dacamposol 0:a4f404326ee0 51 // Now it starts the data transmision, we expect first 50us before the first bit
dacamposol 0:a4f404326ee0 52 // We expect 5 packets of 8 bits:
dacamposol 0:a4f404326ee0 53 // - Integral RH Data
dacamposol 0:a4f404326ee0 54 // - Decimal RH Data
dacamposol 0:a4f404326ee0 55 // - Integral Temp Data
dacamposol 0:a4f404326ee0 56 // - Decimal Temp Data
dacamposol 0:a4f404326ee0 57 // - Checksum
dacamposol 0:a4f404326ee0 58 bool packets[5][8];
dacamposol 0:a4f404326ee0 59 for (int i = 0; i < 5; i++) {
dacamposol 0:a4f404326ee0 60 bool result[8];
dacamposol 0:a4f404326ee0 61 for (int j = 0; j < 8; j++) {
dacamposol 0:a4f404326ee0 62 // We wait the start of the bit with wait just in case we have been carrying error
dacamposol 0:a4f404326ee0 63 // wait_us(50);
dacamposol 0:a4f404326ee0 64 while(!dht); // Low signal of 50us. Each bit has to start with it.
dacamposol 0:a4f404326ee0 65 period.reset();
dacamposol 0:a4f404326ee0 66 period.start();
dacamposol 0:a4f404326ee0 67 while(dht);
dacamposol 0:a4f404326ee0 68 period.stop();
dacamposol 0:a4f404326ee0 69
dacamposol 0:a4f404326ee0 70 // According to the documentation, it's 26us vs 70us
dacamposol 0:a4f404326ee0 71 result[j] = period.read_us() > 60;
dacamposol 0:a4f404326ee0 72 }
dacamposol 0:a4f404326ee0 73 packets[i][0] = result[0];
dacamposol 0:a4f404326ee0 74 packets[i][1] = result[1];
dacamposol 0:a4f404326ee0 75 packets[i][2] = result[2];
dacamposol 0:a4f404326ee0 76 packets[i][3] = result[3];
dacamposol 0:a4f404326ee0 77 packets[i][4] = result[4];
dacamposol 0:a4f404326ee0 78 packets[i][5] = result[5];
dacamposol 0:a4f404326ee0 79 packets[i][6] = result[6];
dacamposol 0:a4f404326ee0 80 packets[i][7] = result[7];
dacamposol 0:a4f404326ee0 81 }
dacamposol 0:a4f404326ee0 82
dacamposol 0:a4f404326ee0 83 // Now we stop the sensor
dacamposol 0:a4f404326ee0 84 dht.output();
dacamposol 0:a4f404326ee0 85 dht.write(1);
dacamposol 0:a4f404326ee0 86
dacamposol 0:a4f404326ee0 87 // And we treat the received data
dacamposol 0:a4f404326ee0 88
dacamposol 0:a4f404326ee0 89 // First, we treat the MSB (bit 0) of the temp, if it's 1, then it's negative.
dacamposol 0:a4f404326ee0 90 // The real value for the temperature is stored in the bits 1 ~ 16
dacamposol 0:a4f404326ee0 91 int MSB = 1;
dacamposol 0:a4f404326ee0 92 if (packets[2][0])
dacamposol 0:a4f404326ee0 93 MSB = -1;
dacamposol 0:a4f404326ee0 94 packets[2][0] = 0;
dacamposol 0:a4f404326ee0 95
dacamposol 0:a4f404326ee0 96 short high_humidity = shortFromBits(packets[0]);
dacamposol 0:a4f404326ee0 97 short low_humidity = shortFromBits(packets[1]);
dacamposol 0:a4f404326ee0 98 short high_temp = shortFromBits(packets[2]);
dacamposol 0:a4f404326ee0 99 short low_temp = shortFromBits(packets[3]);
dacamposol 0:a4f404326ee0 100
dacamposol 0:a4f404326ee0 101 // If the values coincide with the checksum, then change the last values of temperature and humidity
dacamposol 0:a4f404326ee0 102 // Otherwise, print the problem and exit the method
dacamposol 0:a4f404326ee0 103 if ((high_humidity + low_humidity + high_temp + low_temp) % 256 == shortFromBits(packets[4])) {
dacamposol 0:a4f404326ee0 104 m_humidity = (high_humidity * 256 + low_humidity) / 10;
dacamposol 0:a4f404326ee0 105 m_temperature = MSB * (high_temp * 256 + low_temp) / 10;
dacamposol 0:a4f404326ee0 106 } else {
dacamposol 0:a4f404326ee0 107 return -1;
dacamposol 0:a4f404326ee0 108 }
dacamposol 0:a4f404326ee0 109
dacamposol 0:a4f404326ee0 110 return 1;
dacamposol 0:a4f404326ee0 111 }
dacamposol 0:a4f404326ee0 112
dacamposol 0:a4f404326ee0 113 int DHT22::getTemperature()
dacamposol 0:a4f404326ee0 114 {
dacamposol 0:a4f404326ee0 115 return m_temperature;
dacamposol 0:a4f404326ee0 116 }
dacamposol 0:a4f404326ee0 117
dacamposol 0:a4f404326ee0 118 int DHT22::getHumidity()
dacamposol 0:a4f404326ee0 119 {
dacamposol 0:a4f404326ee0 120 return m_humidity;
dacamposol 0:a4f404326ee0 121 }