sr501+BH1750+mq

Dependencies:   mbed

Fork of sensors by w mx

Files at this revision

API Documentation at this revision

Comitter:
xmwmx
Date:
Fri Sep 14 10:52:15 2018 +0000
Parent:
2:cd0ea77c7d66
Commit message:
sr501/BH1750/mq-2/dht11

Changed in this revision

example.cpp Show annotated file Show diff for this revision Revisions of this file
sensors.cpp Show annotated file Show diff for this revision Revisions of this file
sensors.h Show annotated file Show diff for this revision Revisions of this file
diff -r cd0ea77c7d66 -r 31aec950f7dc example.cpp
--- a/example.cpp	Thu Sep 13 05:52:55 2018 +0000
+++ b/example.cpp	Fri Sep 14 10:52:15 2018 +0000
@@ -12,6 +12,7 @@
     sr501 x(PB_1);
     BH1750 y(PB_7,PB_6);
     mq z(PA_7,PA_6);
+    dht11 w(PA_5);
     
     while(1)
     {
@@ -33,11 +34,11 @@
         float light=y.getlightdata();
         usb2pc.printf("Light intensity: %.4f Lux\r\n",light);
         */
-        
+        /*
         if(z.operator==(true))
         {
             usb2pc.printf("mq on\r\n");
-            while(!z.read()){usb2pc.printf("aaaaaaaaaaaaa\r\n");wait(0.1);myled=1;}
+            while(!z.read()){usb2pc.printf("%.3f\r\n",z.getairdata());wait(0.1);myled=1;}
             myled = 1; // LED is ON
             wait(0.2); // 200 ms
             z.reset();
@@ -48,6 +49,14 @@
             myled = 0; // LED is OFF
             wait(1.0); // 1 sec
         }
-        
+        */
+        ///*
+        if(w.getdata())
+        {
+            usb2pc.printf("Humidity (%):%.3f\r\n",w.gethumidity());
+            usb2pc.printf("Temperature (oC): %.3f\r\n",w.gettemperature());
+        }
+        wait(2);
+        //*/
     }
 }
\ No newline at end of file
diff -r cd0ea77c7d66 -r 31aec950f7dc sensors.cpp
--- 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
diff -r cd0ea77c7d66 -r 31aec950f7dc sensors.h
--- a/sensors.h	Thu Sep 13 05:52:55 2018 +0000
+++ b/sensors.h	Fri Sep 14 10:52:15 2018 +0000
@@ -77,8 +77,27 @@
     bool operator==(const bool &target);
     float getairdata(); 
     void reset();
-    int mq::read();
+    int read();
 };
 
 //===========================================
+class dht11
+{
+/*****
+*
+*****/
+  private:
+    Timer timer;
+    Timer starttime;
+    DigitalInOut datapin;
+    float H;
+    float T;                                   
+  public:
+    dht11(PinName pin);
+    int getdata();  
+    float gethumidity();
+    float gettemperature();
+
+};
+//=======================
 #endif