Sensor de temperatura y humedad (DHT22)
Dependencies: mbed RGBLed TextLCD Keypad
DHT22.cpp@2:bfdeba134cab, 2019-05-31 (annotated)
- Committer:
- MayraPeA
- Date:
- Fri May 31 00:38:55 2019 +0000
- Revision:
- 2:bfdeba134cab
- Parent:
- 1:6d44a2138e64
Sensor de temperatura y humedad
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MayraPeA | 1:6d44a2138e64 | 1 | |
MayraPeA | 1:6d44a2138e64 | 2 | #include "DHT22.h" |
MayraPeA | 1:6d44a2138e64 | 3 | |
MayraPeA | 1:6d44a2138e64 | 4 | |
MayraPeA | 1:6d44a2138e64 | 5 | DHT22::DHT22(PinName pin) |
MayraPeA | 1:6d44a2138e64 | 6 | { |
MayraPeA | 1:6d44a2138e64 | 7 | _data_pin = pin; |
MayraPeA | 1:6d44a2138e64 | 8 | } |
MayraPeA | 1:6d44a2138e64 | 9 | |
MayraPeA | 1:6d44a2138e64 | 10 | int DHT22::leeTemperatura() |
MayraPeA | 1:6d44a2138e64 | 11 | { |
MayraPeA | 1:6d44a2138e64 | 12 | return _temperatura; |
MayraPeA | 1:6d44a2138e64 | 13 | } |
MayraPeA | 1:6d44a2138e64 | 14 | |
MayraPeA | 1:6d44a2138e64 | 15 | int DHT22::leeHumedad() |
MayraPeA | 1:6d44a2138e64 | 16 | { |
MayraPeA | 1:6d44a2138e64 | 17 | return _humedad; |
MayraPeA | 1:6d44a2138e64 | 18 | } |
MayraPeA | 1:6d44a2138e64 | 19 | |
MayraPeA | 1:6d44a2138e64 | 20 | bool DHT22::inicio() |
MayraPeA | 1:6d44a2138e64 | 21 | { |
MayraPeA | 1:6d44a2138e64 | 22 | /* ## F U N C I O N A M I E N T O ## */ |
MayraPeA | 1:6d44a2138e64 | 23 | DigitalInOut DHT22(_data_pin); //Tenemos que fijar un pin que pueda comportarse como entrada/salida para comunicarnos con el sensor |
MayraPeA | 1:6d44a2138e64 | 24 | int dht22_dat [5]; //fijamos los 5 bytes que resiviremos de informacion |
MayraPeA | 1:6d44a2138e64 | 25 | DHT22.output(); //Se configura el pin como salida |
MayraPeA | 1:6d44a2138e64 | 26 | DHT22.write(0); //Pulso en bajo, para activar la comunicación |
MayraPeA | 1:6d44a2138e64 | 27 | wait_ms(18); //Duración del pulso en bajo de 18ms |
MayraPeA | 1:6d44a2138e64 | 28 | DHT22.write(1); //Pulso en alto |
MayraPeA | 1:6d44a2138e64 | 29 | DHT22.input(); //Se configura el pin como entrada |
MayraPeA | 1:6d44a2138e64 | 30 | wait_us(40); //Duracion del pulso en alto que puede durar desde 20us a 40us |
MayraPeA | 1:6d44a2138e64 | 31 | wait_us(80); // Respuesta del sensor |
MayraPeA | 1:6d44a2138e64 | 32 | int i,j,result=0; |
MayraPeA | 1:6d44a2138e64 | 33 | for (i=0; i<5; i++) { |
MayraPeA | 1:6d44a2138e64 | 34 | result=0; |
MayraPeA | 1:6d44a2138e64 | 35 | for (j=0; j<8; j++) { |
MayraPeA | 1:6d44a2138e64 | 36 | while (DHT22); |
MayraPeA | 1:6d44a2138e64 | 37 | while (!DHT22); |
MayraPeA | 1:6d44a2138e64 | 38 | wait_us(50); |
MayraPeA | 1:6d44a2138e64 | 39 | int p; |
MayraPeA | 1:6d44a2138e64 | 40 | p=DHT22; |
MayraPeA | 1:6d44a2138e64 | 41 | p=p <<(7-j); |
MayraPeA | 1:6d44a2138e64 | 42 | result=result|p; |
MayraPeA | 1:6d44a2138e64 | 43 | } |
MayraPeA | 1:6d44a2138e64 | 44 | dht22_dat[i] = result; |
MayraPeA | 1:6d44a2138e64 | 45 | } |
MayraPeA | 1:6d44a2138e64 | 46 | /*40 bits que contienen la informacion |
MayraPeA | 1:6d44a2138e64 | 47 | 1er byte= Humedad // Parte entera // dat[0] |
MayraPeA | 1:6d44a2138e64 | 48 | 2do byte= Humedad // Parte decimal // dat[1] |
MayraPeA | 1:6d44a2138e64 | 49 | 3er byte= Temperatura // Parte entera // dat[2] |
MayraPeA | 1:6d44a2138e64 | 50 | 4to byte= Temperatura // Parte decimal //dat[3] |
MayraPeA | 1:6d44a2138e64 | 51 | 5to byte= Confirmacion (Cheksum) // dat[4] |
MayraPeA | 1:6d44a2138e64 | 52 | */ |
MayraPeA | 1:6d44a2138e64 | 53 | int dht22_check_sum; |
MayraPeA | 1:6d44a2138e64 | 54 | dht22_check_sum=dht22_dat[0]+dht22_dat[1]+dht22_dat[2]+dht22_dat[3]; |
MayraPeA | 1:6d44a2138e64 | 55 | dht22_check_sum= dht22_check_sum%256; |
MayraPeA | 1:6d44a2138e64 | 56 | if (dht22_check_sum==dht22_dat[4]) |
MayraPeA | 1:6d44a2138e64 | 57 | { |
MayraPeA | 1:6d44a2138e64 | 58 | _humedad=dht22_dat[0]*256+dht22_dat[1]; |
MayraPeA | 1:6d44a2138e64 | 59 | _temperatura=dht22_dat[2]*256+dht22_dat[3]; |
MayraPeA | 1:6d44a2138e64 | 60 | return true; |
MayraPeA | 1:6d44a2138e64 | 61 | } |
MayraPeA | 1:6d44a2138e64 | 62 | return false; |
MayraPeA | 1:6d44a2138e64 | 63 | } |
MayraPeA | 1:6d44a2138e64 | 64 | |
MayraPeA | 1:6d44a2138e64 | 65 | int DHT22::convertirCelsiusaFarenheit() |
MayraPeA | 1:6d44a2138e64 | 66 | { |
MayraPeA | 1:6d44a2138e64 | 67 | int F=0; |
MayraPeA | 1:6d44a2138e64 | 68 | F=((_temperatura/10)*1.8)+32; |
MayraPeA | 1:6d44a2138e64 | 69 | return F; |
MayraPeA | 1:6d44a2138e64 | 70 | } |
MayraPeA | 1:6d44a2138e64 | 71 | |
MayraPeA | 1:6d44a2138e64 | 72 | int DHT22::convertirCelsiusaKelvin() |
MayraPeA | 1:6d44a2138e64 | 73 | { |
MayraPeA | 1:6d44a2138e64 | 74 | int K=0; |
MayraPeA | 1:6d44a2138e64 | 75 | K=((_temperatura/10)+273.15); |
MayraPeA | 1:6d44a2138e64 | 76 | return K; |
MayraPeA | 1:6d44a2138e64 | 77 | } |
MayraPeA | 1:6d44a2138e64 | 78 | int DHT22::calculaPuntodeRocio() |
MayraPeA | 1:6d44a2138e64 | 79 | { |
MayraPeA | 1:6d44a2138e64 | 80 | float A0= 373.15/(273.15 + (_temperatura/10)); |
MayraPeA | 1:6d44a2138e64 | 81 | float SUM = -7.90298 * (A0-1); |
MayraPeA | 1:6d44a2138e64 | 82 | SUM += 5.02808 * log10(A0); |
MayraPeA | 1:6d44a2138e64 | 83 | SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ; |
MayraPeA | 1:6d44a2138e64 | 84 | SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; |
MayraPeA | 1:6d44a2138e64 | 85 | SUM += log10(1013.246); |
MayraPeA | 1:6d44a2138e64 | 86 | float VP = pow(10, SUM-3) * (_humedad/10); |
MayraPeA | 1:6d44a2138e64 | 87 | float T = log(VP/0.61078); // temp var |
MayraPeA | 1:6d44a2138e64 | 88 | return (241.88 * T) / (17.558-T); |
MayraPeA | 1:6d44a2138e64 | 89 | } |