GSOE Webserver ESP01 DHT11

Dependencies:   LCD_i2c_GSOE ESP8266WebserverGSOE

Committer:
mueller1930
Date:
Mon Aug 09 10:09:32 2021 +0000
Revision:
6:410b258da1a4
Parent:
3:9fac8ba757be
anschluss geaendert

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JENG 0:8fe7d36af056 1
mueller1930 6:410b258da1a4 2 //based on DHT11 Library of Jeng: https://os.mbed.com/users/JENG/code/Nucleo_dht11/
JENG 0:8fe7d36af056 3 #include "mbed.h"
jack1930 1:e6f838f99eeb 4 #include "LCD.h"
mueller1930 3:9fac8ba757be 5 #include "ESP8266Webserver.h"
JENG 0:8fe7d36af056 6
JENG 0:8fe7d36af056 7
JENG 0:8fe7d36af056 8 #define DHTLIB_OK 0
JENG 0:8fe7d36af056 9 #define DHTLIB_ERROR_CHECKSUM -1
JENG 0:8fe7d36af056 10 #define DHTLIB_ERROR_TIMEOUT -2
mueller1930 3:9fac8ba757be 11 ESP8266Webserver myWebserver;
JENG 0:8fe7d36af056 12 Timer tmr;
jack1930 1:e6f838f99eeb 13 lcd mylcd;
JENG 0:8fe7d36af056 14
mueller1930 6:410b258da1a4 15 DigitalInOut data_pin(PB_0); //Anschluss S (Data)
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 //########################################
mueller1930 3:9fac8ba757be 101 float poti=0.5;
mueller1930 3:9fac8ba757be 102 string getRootPage()
mueller1930 3:9fac8ba757be 103 {
mueller1930 3:9fac8ba757be 104 string webpage;
mueller1930 3:9fac8ba757be 105 webpage="<!DOCTYPE html>";
mueller1930 3:9fac8ba757be 106 //Javascript
mueller1930 3:9fac8ba757be 107 webpage+="<script type=\"text/javascript\">";
mueller1930 3:9fac8ba757be 108 webpage+="var x;";
mueller1930 3:9fac8ba757be 109 webpage+="function z(){location.assign(\"http://";
mueller1930 3:9fac8ba757be 110 webpage+=myWebserver.gibIP();
mueller1930 3:9fac8ba757be 111 webpage+="\");}";
mueller1930 3:9fac8ba757be 112 webpage+="function sT(){x=setInterval(z,1000);}";
mueller1930 3:9fac8ba757be 113 webpage+="function spT(){clearInterval(x);}";
mueller1930 3:9fac8ba757be 114 webpage+="onload=sT();";
mueller1930 3:9fac8ba757be 115 webpage+="</script>\n";
mueller1930 3:9fac8ba757be 116 //HTML
mueller1930 3:9fac8ba757be 117 webpage+="<html>";
mueller1930 3:9fac8ba757be 118 webpage+="<head>";
mueller1930 3:9fac8ba757be 119 webpage+="<title>STM32 HTTP</title>";
mueller1930 3:9fac8ba757be 120 webpage+="</head>";
mueller1930 3:9fac8ba757be 121 webpage+="<body>";
mueller1930 3:9fac8ba757be 122 webpage+="<h1>WIFI mit STM32 ESP01</h1>\n";
mueller1930 3:9fac8ba757be 123 webpage+="<p>Tmp:"+to_string(temperature)+" C</p>\n";
mueller1930 3:9fac8ba757be 124 webpage+="<p>Hum:"+to_string(humidity)+"%</p>\n";
mueller1930 3:9fac8ba757be 125 webpage+="</body>";
mueller1930 3:9fac8ba757be 126 webpage+="</html>";
mueller1930 3:9fac8ba757be 127 return webpage;
mueller1930 3:9fac8ba757be 128 }
mueller1930 3:9fac8ba757be 129
mueller1930 3:9fac8ba757be 130
mueller1930 3:9fac8ba757be 131 void testfunc()
mueller1930 3:9fac8ba757be 132 {
mueller1930 3:9fac8ba757be 133 myWebserver.send(200,"text/html",getRootPage());
mueller1930 3:9fac8ba757be 134 }
mueller1930 3:9fac8ba757be 135
JENG 0:8fe7d36af056 136
JENG 0:8fe7d36af056 137 int main(void){
mueller1930 3:9fac8ba757be 138 myWebserver.on("/",&testfunc);
mueller1930 3:9fac8ba757be 139 myWebserver.begin();
jack1930 1:e6f838f99eeb 140 mylcd.clear();
jack1930 1:e6f838f99eeb 141 mylcd.cursorpos(0);
JENG 0:8fe7d36af056 142
jack1930 1:e6f838f99eeb 143 mylcd.printf("Nucleo - DHT11");
JENG 0:8fe7d36af056 144
JENG 0:8fe7d36af056 145 for(;;){
mueller1930 3:9fac8ba757be 146 myWebserver.handleClient();
jack1930 1:e6f838f99eeb 147 int fehler=dht_read();
JENG 0:8fe7d36af056 148
JENG 0:8fe7d36af056 149
jack1930 1:e6f838f99eeb 150
jack1930 1:e6f838f99eeb 151 mylcd.cursorpos(0x40);
jack1930 1:e6f838f99eeb 152 mylcd.printf("Hum %2d%% Tmp %2dc ", humidity, temperature);
jack1930 1:e6f838f99eeb 153 wait_us(500000);
JENG 0:8fe7d36af056 154
jack1930 1:e6f838f99eeb 155
JENG 0:8fe7d36af056 156
JENG 0:8fe7d36af056 157 }
JENG 0:8fe7d36af056 158
JENG 0:8fe7d36af056 159 }