Jim Mueller / Mbed OS Nucleo_dht11_Webserver

Dependencies:   LCD_i2c_GSOE ESP8266WebserverGSOE

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

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