Dependents:   Interfacage_Disco_DHT22

Committer:
dacamposol
Date:
Thu Apr 16 08:55:38 2020 +0000
Revision:
1:3385bd532812
Parent:
0:a4f404326ee0
Child:
2:8c7fa818f329
Remove debugging variable "results".

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 for (int j = 0; j < 8; j++) {
dacamposol 0:a4f404326ee0 61 // We wait the start of the bit with wait just in case we have been carrying error
dacamposol 0:a4f404326ee0 62 // wait_us(50);
dacamposol 0:a4f404326ee0 63 while(!dht); // Low signal of 50us. Each bit has to start with it.
dacamposol 0:a4f404326ee0 64 period.reset();
dacamposol 0:a4f404326ee0 65 period.start();
dacamposol 0:a4f404326ee0 66 while(dht);
dacamposol 0:a4f404326ee0 67 period.stop();
dacamposol 0:a4f404326ee0 68
dacamposol 0:a4f404326ee0 69 // According to the documentation, it's 26us vs 70us
dacamposol 1:3385bd532812 70 packets[i][j] = period.read_us() > 60;
dacamposol 0:a4f404326ee0 71 }
dacamposol 0:a4f404326ee0 72 }
dacamposol 0:a4f404326ee0 73
dacamposol 0:a4f404326ee0 74 // Now we stop the sensor
dacamposol 0:a4f404326ee0 75 dht.output();
dacamposol 0:a4f404326ee0 76 dht.write(1);
dacamposol 0:a4f404326ee0 77
dacamposol 0:a4f404326ee0 78 // And we treat the received data
dacamposol 0:a4f404326ee0 79
dacamposol 0:a4f404326ee0 80 // First, we treat the MSB (bit 0) of the temp, if it's 1, then it's negative.
dacamposol 0:a4f404326ee0 81 // The real value for the temperature is stored in the bits 1 ~ 16
dacamposol 0:a4f404326ee0 82 int MSB = 1;
dacamposol 0:a4f404326ee0 83 if (packets[2][0])
dacamposol 0:a4f404326ee0 84 MSB = -1;
dacamposol 0:a4f404326ee0 85 packets[2][0] = 0;
dacamposol 0:a4f404326ee0 86
dacamposol 0:a4f404326ee0 87 short high_humidity = shortFromBits(packets[0]);
dacamposol 0:a4f404326ee0 88 short low_humidity = shortFromBits(packets[1]);
dacamposol 0:a4f404326ee0 89 short high_temp = shortFromBits(packets[2]);
dacamposol 0:a4f404326ee0 90 short low_temp = shortFromBits(packets[3]);
dacamposol 0:a4f404326ee0 91
dacamposol 0:a4f404326ee0 92 // If the values coincide with the checksum, then change the last values of temperature and humidity
dacamposol 0:a4f404326ee0 93 // Otherwise, print the problem and exit the method
dacamposol 0:a4f404326ee0 94 if ((high_humidity + low_humidity + high_temp + low_temp) % 256 == shortFromBits(packets[4])) {
dacamposol 0:a4f404326ee0 95 m_humidity = (high_humidity * 256 + low_humidity) / 10;
dacamposol 0:a4f404326ee0 96 m_temperature = MSB * (high_temp * 256 + low_temp) / 10;
dacamposol 0:a4f404326ee0 97 } else {
dacamposol 0:a4f404326ee0 98 return -1;
dacamposol 0:a4f404326ee0 99 }
dacamposol 0:a4f404326ee0 100
dacamposol 0:a4f404326ee0 101 return 1;
dacamposol 0:a4f404326ee0 102 }
dacamposol 0:a4f404326ee0 103
dacamposol 0:a4f404326ee0 104 int DHT22::getTemperature()
dacamposol 0:a4f404326ee0 105 {
dacamposol 0:a4f404326ee0 106 return m_temperature;
dacamposol 0:a4f404326ee0 107 }
dacamposol 0:a4f404326ee0 108
dacamposol 0:a4f404326ee0 109 int DHT22::getHumidity()
dacamposol 0:a4f404326ee0 110 {
dacamposol 0:a4f404326ee0 111 return m_humidity;
dacamposol 0:a4f404326ee0 112 }