w mx
/
sensors_2
sr501+BH1750+mq
Fork of sensors by
Diff: sensors.cpp
- Revision:
- 3:31aec950f7dc
- Parent:
- 2:cd0ea77c7d66
--- a/sensors.cpp Thu Sep 13 05:52:55 2018 +0000 +++ b/sensors.cpp Fri Sep 14 10:52:15 2018 +0000 @@ -115,7 +115,7 @@ //-------- float mq::getairdata() { - + return signallevel.read(); } //----------- void mq::reset() @@ -125,4 +125,95 @@ int mq::read() { return signal.read(); +} +//=========================================== +dht11::dht11(PinName pin) + :datapin(pin) +{ + starttime.start(); + usb2pc.printf("dht11 start!\r\n"); +} +//---------- +int dht11::getdata() +{ + int timeout=10000; + uint8_t data[5]; + uint8_t bit=7; + uint8_t count=0; + + for(int i=0;i<5;i++) + { + data[i]=0; + } + if(starttime.read_ms()<1500){while(starttime.read_ms()<1500){}starttime.stop();} + + datapin.output(); + datapin=0; + wait_ms(19); + datapin=1; + wait_us(30); + datapin.input(); + + while(!datapin) + { + if(timeout--==0){usb2pc.printf("timeout!no reset\r\n");return 0;} + } + timeout=10000; + while(datapin) + { + if(timeout--==0){usb2pc.printf("timeout!no respanse\r\n");return 0;} + } + timeout=10000; + for(int i=0;i<40;i++) + { + while(!datapin) + { + if(timeout--==0){usb2pc.printf("timeout!\r\n");return 0;} + } + timer.start(); + timeout=10000; + while(datapin) + { + if(timeout--==0){usb2pc.printf("timeout!n\r\n");return 0;} + } + timeout=10000; + long t=timer.read_us(); + timer.stop(); + timer.reset(); + + if(bit==0) + { + if(t>40){data[count]|=(1<<bit);} + bit=7; + count++; + } + else + { + if(t>40) + { + data[count]|=(1<<bit); + } + bit--; + } + } + datapin=1; + if(data[4]==data[0]+data[1]+data[2]+data[3]) + { + //usb2pc.printf("Humidity (%):%f\r\n",(float)data[0]); + //usb2pc.printf("Temperature (oC): %f\r\n",(float)data[2]); + H=data[0]+data[1]/10.0; + T=data[2]+data[3]/10.0; + return 1; + } + else{usb2pc.printf("error!\r\ndata0:%d\tdata1:%d\r\ndata2:%d\tdata3:%d\r\ndata4:%d\r\n",(int)data[0],(int)data[1],(int)data[2],(int)data[3],(int)data[4]);return 0;} +} +//------- +float dht11::gethumidity() +{ + return H; +} +//------- +float dht11::gettemperature() +{ + return T; } \ No newline at end of file