Etienne Charbonnier
/
plant_monitoring
Plant Monitoring CS
DHT22.cpp@59:169324c8e604, 2020-03-25 (annotated)
- Committer:
- titi9211
- Date:
- Wed Mar 25 15:46:56 2020 +0000
- Revision:
- 59:169324c8e604
Plant Monitoring Program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
titi9211 | 59:169324c8e604 | 1 | #include "DHT22.h" |
titi9211 | 59:169324c8e604 | 2 | |
titi9211 | 59:169324c8e604 | 3 | |
titi9211 | 59:169324c8e604 | 4 | DHT22::DHT22(PinName pin) { |
titi9211 | 59:169324c8e604 | 5 | _data_pin = pin; |
titi9211 | 59:169324c8e604 | 6 | } |
titi9211 | 59:169324c8e604 | 7 | |
titi9211 | 59:169324c8e604 | 8 | int DHT22::getTemperature() { |
titi9211 | 59:169324c8e604 | 9 | return _temperature; |
titi9211 | 59:169324c8e604 | 10 | } |
titi9211 | 59:169324c8e604 | 11 | |
titi9211 | 59:169324c8e604 | 12 | int DHT22::getHumidity() { |
titi9211 | 59:169324c8e604 | 13 | return _humidity; |
titi9211 | 59:169324c8e604 | 14 | } |
titi9211 | 59:169324c8e604 | 15 | |
titi9211 | 59:169324c8e604 | 16 | bool DHT22::sample() { |
titi9211 | 59:169324c8e604 | 17 | DigitalInOut DHT22(_data_pin); |
titi9211 | 59:169324c8e604 | 18 | int dht22_dat [5]; |
titi9211 | 59:169324c8e604 | 19 | DHT22.output(); |
titi9211 | 59:169324c8e604 | 20 | DHT22.write(0); |
titi9211 | 59:169324c8e604 | 21 | wait_ms(18); |
titi9211 | 59:169324c8e604 | 22 | DHT22.write(1); |
titi9211 | 59:169324c8e604 | 23 | DHT22.input(); |
titi9211 | 59:169324c8e604 | 24 | wait_us(40); |
titi9211 | 59:169324c8e604 | 25 | wait_us(80); |
titi9211 | 59:169324c8e604 | 26 | int i,j,result=0; |
titi9211 | 59:169324c8e604 | 27 | for (i=0; i<5; i++) { |
titi9211 | 59:169324c8e604 | 28 | result=0; |
titi9211 | 59:169324c8e604 | 29 | for (j=0; j<8; j++) { |
titi9211 | 59:169324c8e604 | 30 | while (DHT22); |
titi9211 | 59:169324c8e604 | 31 | while (!DHT22); |
titi9211 | 59:169324c8e604 | 32 | wait_us(50); |
titi9211 | 59:169324c8e604 | 33 | int p; |
titi9211 | 59:169324c8e604 | 34 | p=DHT22; |
titi9211 | 59:169324c8e604 | 35 | p=p <<(7-j); |
titi9211 | 59:169324c8e604 | 36 | result=result|p; |
titi9211 | 59:169324c8e604 | 37 | } |
titi9211 | 59:169324c8e604 | 38 | dht22_dat[i] = result; |
titi9211 | 59:169324c8e604 | 39 | } |
titi9211 | 59:169324c8e604 | 40 | int dht22_check_sum; |
titi9211 | 59:169324c8e604 | 41 | dht22_check_sum=dht22_dat[0]+dht22_dat[1]+dht22_dat[2]+dht22_dat[3]; |
titi9211 | 59:169324c8e604 | 42 | dht22_check_sum= dht22_check_sum%256; |
titi9211 | 59:169324c8e604 | 43 | if (dht22_check_sum==dht22_dat[4]) { |
titi9211 | 59:169324c8e604 | 44 | _humidity=dht22_dat[0]*256+dht22_dat[1]; |
titi9211 | 59:169324c8e604 | 45 | _temperature=dht22_dat[2]*256+dht22_dat[3]; |
titi9211 | 59:169324c8e604 | 46 | return true; |
titi9211 | 59:169324c8e604 | 47 | } |
titi9211 | 59:169324c8e604 | 48 | return false; |
titi9211 | 59:169324c8e604 | 49 | } |