GSOE Webserver ESP01 DHT11

Dependencies:   LCD_i2c_GSOE ESP8266WebserverGSOE

Committer:
jack1930
Date:
Mon Jul 26 12:27:29 2021 +0000
Revision:
2:85f2573de3e4
Parent:
1:e6f838f99eeb
Child:
3:9fac8ba757be
Child:
4:974fa06a3dca
Kommentare

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