GSOE Webserver ESP01 DHT11

Dependencies:   LCD_i2c_GSOE ESP8266WebserverGSOE

Committer:
jack1930
Date:
Mon Jul 26 12:23:40 2021 +0000
Revision:
1:e6f838f99eeb
Parent:
0:8fe7d36af056
Child:
2:85f2573de3e4
Workshop

Who changed what in which revision?

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