GSOE Webserver ESP01 DHT11
Dependencies: LCD_i2c_GSOE ESP8266WebserverGSOE
main.cpp
00001 00002 #include "mbed.h" 00003 #include "LCD.h" 00004 #include "ESP8266Webserver.h" 00005 00006 00007 #define DHTLIB_OK 0 00008 #define DHTLIB_ERROR_CHECKSUM -1 00009 #define DHTLIB_ERROR_TIMEOUT -2 00010 ESP8266Webserver myWebserver; 00011 Timer tmr; 00012 lcd mylcd; 00013 00014 DigitalInOut data_pin(PC_15); //Anschluss S 00015 //Anschluss - an GND 00016 //mittlerer Anschluss an 3,3V 00017 00018 int humidity,hum; 00019 int temperature,temp; 00020 00021 //######################################## 00022 // DHT11 Library 00023 //######################################## 00024 int dht_read(void){ 00025 00026 // BUFFER TO RECEIVE 00027 uint8_t bits[5]; 00028 uint8_t cnt = 7; 00029 uint8_t idx = 0; 00030 00031 tmr.stop(); 00032 tmr.reset(); 00033 00034 // EMPTY BUFFER 00035 for(int i=0; i< 5; i++) bits[i] = 0; 00036 00037 // REQUEST SAMPLE 00038 data_pin.output(); 00039 data_pin.write(0); 00040 wait_us(18000); 00041 data_pin.write(1); 00042 wait_us(40); 00043 data_pin.input(); 00044 00045 // ACKNOWLEDGE or TIMEOUT 00046 unsigned int loopCnt = 20000; 00047 00048 while(!data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT; 00049 00050 loopCnt = 20000; 00051 00052 while(data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT; 00053 00054 // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT 00055 for(int i=0; i<40; i++){ 00056 00057 loopCnt = 20000; 00058 00059 while(!data_pin.read())if(loopCnt-- == 0)return DHTLIB_ERROR_TIMEOUT; 00060 00061 //unsigned long t = micros(); 00062 tmr.start(); 00063 00064 loopCnt = 20000; 00065 00066 while(data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT; 00067 00068 if(tmr.read_us() > 40) bits[idx] |= (1 << cnt); 00069 00070 tmr.stop(); 00071 tmr.reset(); 00072 00073 if(cnt == 0){ // next byte? 00074 00075 cnt = 7; // restart at MSB 00076 idx++; // next byte! 00077 00078 }else cnt--; 00079 00080 } 00081 00082 // WRITE TO RIGHT VARS 00083 // as bits[1] and bits[3] are allways zero they are omitted in formulas. 00084 humidity = bits[0]; 00085 temperature = bits[2]; 00086 00087 uint8_t sum = bits[0] + bits[2]; 00088 00089 if(bits[4] != sum)return DHTLIB_ERROR_CHECKSUM; 00090 00091 return DHTLIB_OK; 00092 00093 } 00094 00095 char buffer[17]; 00096 00097 //######################################## 00098 // End of DHT11 Library 00099 //######################################## 00100 float poti=0.5; 00101 string getRootPage() 00102 { 00103 string webpage; 00104 webpage="<!DOCTYPE html>"; 00105 //Javascript 00106 webpage+="<script type=\"text/javascript\">"; 00107 webpage+="var x;"; 00108 webpage+="function z(){location.assign(\"http://"; 00109 webpage+=myWebserver.gibIP(); 00110 webpage+="\");}"; 00111 webpage+="function sT(){x=setInterval(z,1000);}"; 00112 webpage+="function spT(){clearInterval(x);}"; 00113 webpage+="onload=sT();"; 00114 webpage+="</script>\n"; 00115 //HTML 00116 webpage+="<html>"; 00117 webpage+="<head>"; 00118 webpage+="<title>STM32 HTTP</title>"; 00119 webpage+="</head>"; 00120 webpage+="<body>"; 00121 webpage+="<h1>WIFI mit STM32 ESP01</h1>\n"; 00122 webpage+="<p>Tmp:"+to_string(temp)+" C</p>\n"; 00123 webpage+="<p>Hum:"+to_string(hum)+"%</p>\n"; 00124 webpage+="</body>"; 00125 webpage+="</html>"; 00126 return webpage; 00127 } 00128 00129 00130 void testfunc() 00131 { 00132 myWebserver.send(200,"text/html",getRootPage()); 00133 } 00134 00135 00136 int main(void){ 00137 myWebserver.on("/",&testfunc); 00138 myWebserver.begin(); 00139 mylcd.clear(); 00140 mylcd.cursorpos(0); 00141 00142 mylcd.printf("Nucleo - DHT11"); 00143 00144 for(;;){ 00145 myWebserver.handleClient(); 00146 int fehler=dht_read(); 00147 00148 00149 00150 mylcd.cursorpos(0x40); 00151 mylcd.printf("Hum %2d%% Tmp %2dc ", humidity, temperature); 00152 hum=humidity; 00153 temp=temperature; 00154 wait_us(500000); 00155 00156 00157 00158 } 00159 00160 }
Generated on Sat Jul 30 2022 08:34:36 by
1.7.2