GSOE Webserver ESP01 DHT11

Dependencies:   LCD_i2c_GSOE ESP8266WebserverGSOE

Committer:
mueller1930
Date:
Tue Jul 27 09:37:12 2021 +0000
Revision:
3:9fac8ba757be
Parent:
2:85f2573de3e4
Child:
6:410b258da1a4
Einheiten

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