SMART CLEO Temp humi

Committer:
SMART_CLEO
Date:
Thu Sep 28 02:11:59 2017 +0000
Revision:
0:fa2b2a1e9a8f
SMART_CLEO

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SMART_CLEO 0:fa2b2a1e9a8f 1 #include "mbed.h"
SMART_CLEO 0:fa2b2a1e9a8f 2 #include "TextLCD.h"
SMART_CLEO 0:fa2b2a1e9a8f 3
SMART_CLEO 0:fa2b2a1e9a8f 4 #define DHTLIB_OK 0
SMART_CLEO 0:fa2b2a1e9a8f 5 #define DHTLIB_ERROR_CHECKSUM -1
SMART_CLEO 0:fa2b2a1e9a8f 6 #define DHTLIB_ERROR_TIMEOUT -2
SMART_CLEO 0:fa2b2a1e9a8f 7
SMART_CLEO 0:fa2b2a1e9a8f 8 PinName pin_DHT11 = PA_15;
SMART_CLEO 0:fa2b2a1e9a8f 9
SMART_CLEO 0:fa2b2a1e9a8f 10 DigitalInOut data_pin(pin_DHT11);
SMART_CLEO 0:fa2b2a1e9a8f 11
SMART_CLEO 0:fa2b2a1e9a8f 12 // rs, rw, e, d0-d3
SMART_CLEO 0:fa2b2a1e9a8f 13 TextLCD lcd(PB_12, PB_13, PB_14, PB_15, PA_9, PA_10, PA_11);
SMART_CLEO 0:fa2b2a1e9a8f 14
SMART_CLEO 0:fa2b2a1e9a8f 15 Timer tmr;
SMART_CLEO 0:fa2b2a1e9a8f 16
SMART_CLEO 0:fa2b2a1e9a8f 17 int humi;
SMART_CLEO 0:fa2b2a1e9a8f 18 int temp;
SMART_CLEO 0:fa2b2a1e9a8f 19
SMART_CLEO 0:fa2b2a1e9a8f 20 int dht_read(void);
SMART_CLEO 0:fa2b2a1e9a8f 21
SMART_CLEO 0:fa2b2a1e9a8f 22 int main() {
SMART_CLEO 0:fa2b2a1e9a8f 23
SMART_CLEO 0:fa2b2a1e9a8f 24 lcd.printf(" Temp : c\n");
SMART_CLEO 0:fa2b2a1e9a8f 25 lcd.printf(" Humi : %%");
SMART_CLEO 0:fa2b2a1e9a8f 26
SMART_CLEO 0:fa2b2a1e9a8f 27 while(1) {
SMART_CLEO 0:fa2b2a1e9a8f 28 if(dht_read() == 0)
SMART_CLEO 0:fa2b2a1e9a8f 29 {
SMART_CLEO 0:fa2b2a1e9a8f 30 lcd.locate(10, 0);
SMART_CLEO 0:fa2b2a1e9a8f 31 lcd.printf("%3d", temp);
SMART_CLEO 0:fa2b2a1e9a8f 32 lcd.locate(10, 1);
SMART_CLEO 0:fa2b2a1e9a8f 33 lcd.printf("%3d", humi);
SMART_CLEO 0:fa2b2a1e9a8f 34 }
SMART_CLEO 0:fa2b2a1e9a8f 35 else
SMART_CLEO 0:fa2b2a1e9a8f 36 {
SMART_CLEO 0:fa2b2a1e9a8f 37 lcd.locate(10, 0);
SMART_CLEO 0:fa2b2a1e9a8f 38 lcd.printf("%3d", 0);
SMART_CLEO 0:fa2b2a1e9a8f 39 lcd.locate(10, 1);
SMART_CLEO 0:fa2b2a1e9a8f 40 lcd.printf("%3d", 0);
SMART_CLEO 0:fa2b2a1e9a8f 41 }
SMART_CLEO 0:fa2b2a1e9a8f 42 wait(2);
SMART_CLEO 0:fa2b2a1e9a8f 43 }
SMART_CLEO 0:fa2b2a1e9a8f 44 }
SMART_CLEO 0:fa2b2a1e9a8f 45
SMART_CLEO 0:fa2b2a1e9a8f 46 int dht_read(void){
SMART_CLEO 0:fa2b2a1e9a8f 47
SMART_CLEO 0:fa2b2a1e9a8f 48 // BUFFER TO RECEIVE
SMART_CLEO 0:fa2b2a1e9a8f 49 uint8_t bits[5];
SMART_CLEO 0:fa2b2a1e9a8f 50 uint8_t cnt = 7;
SMART_CLEO 0:fa2b2a1e9a8f 51 uint8_t idx = 0;
SMART_CLEO 0:fa2b2a1e9a8f 52
SMART_CLEO 0:fa2b2a1e9a8f 53 tmr.stop();
SMART_CLEO 0:fa2b2a1e9a8f 54 tmr.reset();
SMART_CLEO 0:fa2b2a1e9a8f 55
SMART_CLEO 0:fa2b2a1e9a8f 56 // EMPTY BUFFER
SMART_CLEO 0:fa2b2a1e9a8f 57 for(int i=0; i< 5; i++) bits[i] = 0;
SMART_CLEO 0:fa2b2a1e9a8f 58
SMART_CLEO 0:fa2b2a1e9a8f 59 // REQUEST SAMPLE
SMART_CLEO 0:fa2b2a1e9a8f 60 data_pin.output();
SMART_CLEO 0:fa2b2a1e9a8f 61 data_pin.write(0);
SMART_CLEO 0:fa2b2a1e9a8f 62 wait_ms(18);
SMART_CLEO 0:fa2b2a1e9a8f 63 data_pin.write(1);
SMART_CLEO 0:fa2b2a1e9a8f 64 wait_us(10);
SMART_CLEO 0:fa2b2a1e9a8f 65 data_pin.input();
SMART_CLEO 0:fa2b2a1e9a8f 66 wait_us(40);
SMART_CLEO 0:fa2b2a1e9a8f 67
SMART_CLEO 0:fa2b2a1e9a8f 68 // ACKNOWLEDGE or TIMEOUT
SMART_CLEO 0:fa2b2a1e9a8f 69 unsigned long loopCnt = 10000;
SMART_CLEO 0:fa2b2a1e9a8f 70
SMART_CLEO 0:fa2b2a1e9a8f 71 while(data_pin.read() == 0)if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
SMART_CLEO 0:fa2b2a1e9a8f 72
SMART_CLEO 0:fa2b2a1e9a8f 73 loopCnt = 10000;
SMART_CLEO 0:fa2b2a1e9a8f 74
SMART_CLEO 0:fa2b2a1e9a8f 75 while(data_pin.read() == 1)if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
SMART_CLEO 0:fa2b2a1e9a8f 76
SMART_CLEO 0:fa2b2a1e9a8f 77 // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
SMART_CLEO 0:fa2b2a1e9a8f 78
SMART_CLEO 0:fa2b2a1e9a8f 79 for(int i=0; i<40; i++){
SMART_CLEO 0:fa2b2a1e9a8f 80
SMART_CLEO 0:fa2b2a1e9a8f 81 loopCnt = 10000;
SMART_CLEO 0:fa2b2a1e9a8f 82
SMART_CLEO 0:fa2b2a1e9a8f 83 while(data_pin.read() == 0)if(loopCnt-- == 0)return DHTLIB_ERROR_TIMEOUT;
SMART_CLEO 0:fa2b2a1e9a8f 84
SMART_CLEO 0:fa2b2a1e9a8f 85 //unsigned long t = micros();
SMART_CLEO 0:fa2b2a1e9a8f 86 tmr.start();
SMART_CLEO 0:fa2b2a1e9a8f 87
SMART_CLEO 0:fa2b2a1e9a8f 88 loopCnt = 10000;
SMART_CLEO 0:fa2b2a1e9a8f 89
SMART_CLEO 0:fa2b2a1e9a8f 90 while(data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
SMART_CLEO 0:fa2b2a1e9a8f 91
SMART_CLEO 0:fa2b2a1e9a8f 92 if(tmr.read_us() > 40) bits[idx] |= (1 << cnt);
SMART_CLEO 0:fa2b2a1e9a8f 93
SMART_CLEO 0:fa2b2a1e9a8f 94 tmr.stop();
SMART_CLEO 0:fa2b2a1e9a8f 95 tmr.reset();
SMART_CLEO 0:fa2b2a1e9a8f 96
SMART_CLEO 0:fa2b2a1e9a8f 97 if(cnt == 0){ // next byte?
SMART_CLEO 0:fa2b2a1e9a8f 98
SMART_CLEO 0:fa2b2a1e9a8f 99 cnt = 7; // restart at MSB
SMART_CLEO 0:fa2b2a1e9a8f 100 idx++; // next byte!
SMART_CLEO 0:fa2b2a1e9a8f 101
SMART_CLEO 0:fa2b2a1e9a8f 102 }else cnt--;
SMART_CLEO 0:fa2b2a1e9a8f 103
SMART_CLEO 0:fa2b2a1e9a8f 104 }
SMART_CLEO 0:fa2b2a1e9a8f 105
SMART_CLEO 0:fa2b2a1e9a8f 106 // WRITE TO RIGHT VARS
SMART_CLEO 0:fa2b2a1e9a8f 107 // as bits[1] and bits[3] are allways zero they are omitted in formulas.
SMART_CLEO 0:fa2b2a1e9a8f 108 humi = bits[0];
SMART_CLEO 0:fa2b2a1e9a8f 109 temp = bits[2];
SMART_CLEO 0:fa2b2a1e9a8f 110
SMART_CLEO 0:fa2b2a1e9a8f 111 uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
SMART_CLEO 0:fa2b2a1e9a8f 112
SMART_CLEO 0:fa2b2a1e9a8f 113 if(bits[4] != sum){return DHTLIB_ERROR_CHECKSUM;}
SMART_CLEO 0:fa2b2a1e9a8f 114
SMART_CLEO 0:fa2b2a1e9a8f 115 return DHTLIB_OK;
SMART_CLEO 0:fa2b2a1e9a8f 116
SMART_CLEO 0:fa2b2a1e9a8f 117 }