GSOE Webserver ESP01 DHT11

Dependencies:   LCD_i2c_GSOE ESP8266WebserverGSOE

Committer:
jack1930
Date:
Tue Jul 27 09:09:23 2021 +0000
Revision:
4:974fa06a3dca
Parent:
2:85f2573de3e4
PC_15

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