amir gahroosi / Mbed 2 deprecated DHT-11Mine

Dependencies:   mbed

Fork of DHT-11Mine by Umair Aftab

Files at this revision

API Documentation at this revision

Comitter:
amirgahroosi
Date:
Wed Dec 10 06:43:09 2014 +0000
Parent:
0:8464dce1d116
Commit message:
Last version

Changed in this revision

Airhumiditysensors/SEN11301P/DHT.cpp Show annotated file Show diff for this revision Revisions of this file
Airhumiditysensors/SEN11301P/DHT.h Show annotated file Show diff for this revision Revisions of this file
Airhumiditysensors/airhumidity.cpp Show annotated file Show diff for this revision Revisions of this file
Airhumiditysensors/airhumidity.h Show annotated file Show diff for this revision Revisions of this file
DHT.lib Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Airhumiditysensors/SEN11301P/DHT.cpp	Wed Dec 10 06:43:09 2014 +0000
@@ -0,0 +1,146 @@
+#include "DHT.h"
+#define DHT_DATA_BIT_COUNT 40
+
+DHT::DHT(PinName pin,int DHTtype) {
+    _pin = pin;
+    _DHTtype = DHTtype;
+    _firsttime=true;
+}
+
+DHT::~DHT() {
+}
+
+int DHT::readData() {
+    int i, j, retryCount,b;
+    unsigned int bitTimes[DHT_DATA_BIT_COUNT];
+
+    eError err = ERROR_NONE;
+    time_t currentTime = time(NULL);
+
+    DigitalInOut DHT_io(_pin);
+
+    for (i = 0; i < DHT_DATA_BIT_COUNT; i++) {
+        bitTimes[i] = 0;
+    }
+
+    if (!_firsttime) {
+        if (int(currentTime - _lastReadTime) < 2) {
+            err = ERROR_NO_PATIENCE;
+            return err;
+        }
+    } else {
+        _firsttime=false;
+        _lastReadTime=currentTime;
+    }
+    retryCount = 0;
+
+    do {
+        if (retryCount > 125) {
+            err = BUS_BUSY;
+            return err;
+        }
+        retryCount ++;
+        wait_us(2);
+    } while ((DHT_io==0));
+
+
+    DHT_io.output();
+    DHT_io = 0;
+    wait_ms(18);
+    DHT_io = 1;
+    wait_us(40);
+    DHT_io.input();
+
+    retryCount = 0;
+    do {
+        if (retryCount > 40)  {
+            err = ERROR_NOT_PRESENT;
+            return err;
+        }
+        retryCount++;
+        wait_us(1);
+    } while ((DHT_io==1));
+
+    if (err != ERROR_NONE) {
+        return err;
+    }
+
+    wait_us(80);
+
+    for (i = 0; i < 5; i++) {
+        for (j = 0; j < 8; j++) {
+
+            retryCount = 0;
+            do {
+                if (retryCount > 75)  {
+                    err = ERROR_DATA_TIMEOUT;
+                    return err;
+                }
+                retryCount++;
+                wait_us(1);
+            } while (DHT_io == 0);
+            wait_us(40);
+            bitTimes[i*8+j]=DHT_io;
+
+            int count = 0;
+            while (DHT_io == 1 && count < 100) {
+                wait_us(1);
+                count++;
+            }
+        }
+    }
+    DHT_io.output();
+    DHT_io = 1;
+    for (i = 0; i < 5; i++) {
+        b=0;
+        for (j=0; j<8; j++) {
+            if (bitTimes[i*8+j+1] > 0) {
+                b |= ( 1 << (7-j));
+            }
+        }
+        DHT_data[i]=b;
+    }
+
+        _lastReadTime = currentTime;
+        _lastTemperature=CalcTemperature();
+        _lastHumidity=CalcHumidity();
+    return err;
+
+}
+
+float DHT::CalcTemperature() {
+    int v;
+    v = DHT_data[2];
+    return float(v);
+}
+
+float DHT::ReadHumidity() {
+    return _lastHumidity;
+}
+
+float DHT::ConvertCelciustoFarenheit(float celsius) {
+    return celsius * 9 / 5 + 32;
+}
+
+float DHT::ConvertCelciustoKelvin(float celsius) {
+    return celsius + 273.15;
+}
+
+
+float DHT::ReadTemperature(eScale Scale) {
+    if (Scale == FARENHEIT)
+        return ConvertCelciustoFarenheit(_lastTemperature);
+    else if (Scale == KELVIN)
+        return ConvertCelciustoKelvin(_lastTemperature);
+    else
+        return _lastTemperature;
+}
+
+float DHT::CalcHumidity() {
+    int v;
+
+    v = DHT_data[0];
+    return float(v);
+        
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Airhumiditysensors/SEN11301P/DHT.h	Wed Dec 10 06:43:09 2014 +0000
@@ -0,0 +1,57 @@
+
+#ifndef MBED_DHT_H
+#define MBED_DHT_H
+
+#include "mbed.h"
+
+enum eType{
+        DHT11     = 11,
+        SEN11301P = 11,
+        RHT01     = 11,
+    } ;
+
+enum eError {
+    ERROR_NONE = 0,
+    BUS_BUSY =1,
+    ERROR_NOT_PRESENT =2 ,
+    ERROR_ACK_TOO_LONG =3 ,
+    ERROR_SYNC_TIMEOUT = 4,
+    ERROR_DATA_TIMEOUT =5 ,
+    ERROR_CHECKSUM = 6,
+    ERROR_NO_PATIENCE =7
+} ;
+
+typedef enum {
+    CELCIUS =0 ,
+    FARENHEIT =1,
+    KELVIN=2
+} eScale;
+
+
+class DHT {
+
+public:
+
+    DHT(PinName pin,int DHTtype);
+    ~DHT();
+    int readData(void);
+    float ReadHumidity(void);
+    float ReadTemperature(eScale Scale);
+    
+
+private:
+    time_t  _lastReadTime;
+    float _lastTemperature;
+    float _lastHumidity;
+    PinName _pin;
+    bool _firsttime;
+    int _DHTtype;
+    int DHT_data[6];
+    float CalcTemperature();
+    float CalcHumidity();
+    float ConvertCelciustoFarenheit(float);
+    float ConvertCelciustoKelvin(float);
+
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Airhumiditysensors/airhumidity.cpp	Wed Dec 10 06:43:09 2014 +0000
@@ -0,0 +1,35 @@
+#include "mbed.h"
+#include "airhumidity.h"
+#include "DHT.h"
+
+int error_inside;
+
+DHT sensor_inside(PTD1,SEN11301P); 
+
+
+//INSIDE FUNCTIONS
+float get_air_temp_inside(int tempunit){
+    wait(1); 
+    error_inside = sensor_inside.readData();
+    switch (tempunit){
+        case 0:
+            return sensor_inside.ReadTemperature(CELCIUS);
+          //  break;
+        case 1:
+            return sensor_inside.ReadTemperature(FARENHEIT);
+           // break;
+        case 2:
+            return sensor_inside.ReadTemperature(KELVIN);
+        //    break;
+        default:
+            return sensor_inside.ReadTemperature(CELCIUS);
+        //    break;
+    }
+}
+
+
+float get_air_humid_inside(){
+    wait(1); 
+    error_inside = sensor_inside.readData();
+    return sensor_inside.ReadHumidity();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Airhumiditysensors/airhumidity.h	Wed Dec 10 06:43:09 2014 +0000
@@ -0,0 +1,7 @@
+#include "DHT.h"
+
+extern int error_inside;
+
+float get_air_temp_inside(int tempunit);
+float get_air_humid_inside();
+
--- a/DHT.lib	Tue Mar 18 23:49:51 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/umairaftab/code/DHT/#21dc04090457
--- a/main.cpp	Tue Mar 18 23:49:51 2014 +0000
+++ b/main.cpp	Wed Dec 10 06:43:09 2014 +0000
@@ -1,28 +1,335 @@
 #include "mbed.h"
-#include "DHT.h"
+#include "airhumidity.h"
+#include <string>
+
+DigitalOut myled(LED1);
+Serial pc(USBTX, USBRX); // tx, rx
+Serial bt(PTE22, PTE23);
+AnalogIn gas_output(PTB1);
+ 
+ ///////////////////sara's code///////////////////
+ 
+using namespace std;
+ 
+DigitalOut R1 (D6);
+DigitalOut R2 (D7);
+DigitalOut R3 (D8);
+DigitalOut R4 (D9);
+ 
+InterruptIn C1 (D10);
+DigitalIn C2 (D11);
+DigitalIn C3 (D12);
+ 
+DigitalOut keyled(LED1);
+DigitalOut flash(LED4);
+ 
+AnalogIn ain(PTB0);
+ 
+Timer t;
+float tm ;
  
-DigitalOut myled(LED1);
+char key;
+int s=0;
+
+float value_adc =0.0f;
+int dig_value = 0;
+Timer timeout;
+float current_time = 0.0;
+
+
+
+unsigned char arrkey[12]={ '1' , '2' , '3' ,
+                            '4' , '5' , '6' ,
+                            '7' , '8' , '9' ,
+                            '*' , '0' , '#' };
+
+///////////////sara's part//////////////
+
+char msg;
+
+
+float temp=0.0;
+float humid=0.0;
+int unit =0;
+string room_safety_status = "issafe";
+bool someone_in = false;
+bool door_status = false;
+string room_status = "yes";
+string room_gas_status = "safe";
+float gas_sensor_value = 0.0;
+
+bool authorized_person_inside = false;
+bool authorized_person_entered = false;
+string last_person = " ";
+
+                            
+void unlock (){
+    printf("DOOR UNLOCK: Welcome!\n\r");
+    door_status = true;
+}
+ 
+void lock (){
+    printf("DOOR LOCK\n\r");
+} 
  
-DHT sensor(PTC7,SEN11301P); // Use the SEN11301P sensor
+unsigned char char_string[4]= {'*' ,'*' ,'*' ,'*'};
+unsigned char password[4]={'1' , '2' , '3' , '4'};
+unsigned char passwordA[4]={'4' , '5' , '6' , '7'};
+
+unsigned char keypad(){
+    unsigned char r,b;
+      
+    while(current_time<5){
+        current_time = timeout.read();
+        for (r=0; r<4; r++){
+            b=4;
+            R1 = 1; R2 = 1 ; R3 = 1 ; R4 = 1;
+            if (r == 0) R1 = 0;
+            if (r == 1) R2 = 0;
+            if (r == 2) R3 = 0;
+            if (r == 3) R4 = 0;
+              
+            if(C1==0) b=0;
+            if(C2==0) b=1;
+            if(C3==0) b=2;
+             
+            if ((!(b==4))){
+                key=arrkey[(r*3)+b];
+                while(C1==0);
+                while(C2==0);
+                while(C3==0);
+           
+                wait(0.2);
+            
+                char_string[s] = key;
+                s++;
+                return key;
+            }
+          
+        }
+    }
+    return 'Q';
+}
+ 
+void handleKeypad (){
+    
+    bool finished = false ; 
+    printf("In Intruppt\n\r");
+    __disable_irq();
+    C1.disable_irq ();
  
+    int firsttime = 0;
+
+    if (firsttime == 0)
+    {
+        s = -1;
+        timeout.reset();
+        timeout.start();
+    }
+    
+    current_time = timeout.read();
+        
+    while ((! finished) && (current_time < 5.0)){
+            key = keypad();
+            current_time = timeout.read();
+            if (s == 4){
+                finished = true;
+                firsttime = 1;
+                
+                printf("Password: ");  
+                for (int i = 0 ; i < 4 ; i++)
+                    printf("%c", char_string[i]);
+                    printf("\n\r");
+                s = 0; 
+                
+                
+                /////////////check password functions//////////
+                int j = 0;
+                int matchSara = 1;
+                while (j < 4){
+                    if (char_string [j] == password[j])
+                        j++;
+                    else{
+                        matchSara = 0;
+                        break;
+                        }
+                    if(matchSara && j == 3)
+                    {
+                        last_person = "Sara";
+                        authorized_person_inside = true;    
+                        room_safety_status = "issafe";
+                    }       
+                }
+                
+                int k = 0;
+                int matchAmir = 1;
+                while (k < 4){
+                    if (char_string [k] == passwordA[k])
+                        k++;
+                    else{
+                        matchAmir = 0;
+                        break;
+                        } 
+                    if(matchAmir && k == 3)
+                    {
+                        last_person = "Amir";
+                        authorized_person_inside = true;    
+                        room_safety_status = "issafe";
+                    }          
+                }
+                
+                if (matchSara == 1){
+                    printf("MATCH Sara\n\r");
+                    unlock();
+                    C1.enable_irq();
+                    __enable_irq();
+                    break;
+                }
+                else if (matchAmir == 1){
+                    printf("MATCH Amir\n\r");
+                    unlock();
+                    C1.enable_irq();
+                    __enable_irq();
+                    break;
+                }
+                else{
+                    printf("WRONG\n\r");
+                    printf("lock for wrong password\n\r");
+                    C1.enable_irq();
+                    __enable_irq();
+                    break;
+                }
+                /////////////check password functions//////////
+            
+            } // end s=4
+
+    } //end while                
+       
+    
+    timeout.stop();    
+    C1.enable_irq ();
+    __enable_irq();
+}
+
+
+
+
+void uart_interrupt_fun() {
+    __disable_irq();
+    msg = bt.getc();
+    //pc.putc(msg);
+    if(msg == 's'){
+        msg = 'n';
+        wait(1);
+        //pc.printf(" \n\r") ;
+        bt.printf("info %4.2f %4.2f %s %s %s %s end\n\r", temp, humid, room_status,room_gas_status, room_safety_status, last_person) ; 
+        wait(3);
+        for(int i=0; i<2; i++)
+        {
+            myled = 1;
+            wait(0.2);
+            myled = 0;
+            wait(0.2);
+        }
+        
+    }
+    __enable_irq();
+    
+}
+
+void check_rooms_presence_safety()
+{
+    if(someone_in)
+    {
+        if(authorized_person_inside)
+        {
+            room_safety_status = "issafe";
+            authorized_person_entered = true;
+        }
+        else
+        {
+            room_safety_status = "notsafe";    
+        }
+    }
+    if(! someone_in)
+    {
+        if(authorized_person_entered)
+        {
+            authorized_person_inside = false;
+            room_safety_status = "notsafe"; 
+        }
+    }
+    
+}
+
+
 int main() {
-    int err;
-    printf("\r\nDHT Test program");
-    printf("\r\n******************\r\n");
-    wait(1); // wait 1 second for device stable status
-    while (1) {
+        
+    
+    while(1) {
+
         myled = 1;
-        err = sensor.readData();
-        if (err == 0) {
-            printf("Temperature is %4.2f C \r\n",sensor.ReadTemperature(CELCIUS));
-            printf("Temperature is %4.2f F \r\n",sensor.ReadTemperature(FARENHEIT));
-            printf("Temperature is %4.2f K \r\n",sensor.ReadTemperature(KELVIN));
-            printf("Humidity is %4.2f \r\n",sensor.ReadHumidity());
-            printf("Dew point is %4.2f  \r\n",sensor.CalcdewPoint(sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity()));
-            printf("Dew point (fast) is %4.2f  \r\n",sensor.CalcdewPointFast(sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity()));
-        } else
-            printf("\r\nErr %i \n",err);
+        wait(0.2);
         myled = 0;
-        wait(15);
+        wait(0.2);
+
+        temp = get_air_temp_inside(unit);
+        humid = get_air_humid_inside();
+        
+        ///////////////////sara's code////////////
+        value_adc = ain.read();
+        gas_sensor_value = gas_output.read()*50;
+        check_rooms_presence_safety();
+        
+        
+        if(gas_sensor_value < 45)
+        {
+            room_gas_status = "safe";    
+        }
+        else
+        {
+            room_gas_status = "danger";    
+        }
+        
+        printf("Analog %5.2f\n\r",value_adc);  
+       
+        if (value_adc < 0.2){
+            //printf("Nobody is here!\n\r");
+            someone_in = false;
+        }
+        else {
+            //printf("Somebody is here!\n\r");
+            someone_in = true;
+        }
+        ////////////////////////
+        
+        
+        
+        bt.baud(38400);
+        bt.attach(&uart_interrupt_fun);
+        
+        R4 = 0;
+        C1.fall(&handleKeypad);
+        
+        
+        
+        wait(1);
+        
+        
+        if(someone_in)
+        {
+            room_status = "yes";    
+        }
+        else
+        {
+            room_status = "no";    
+        }
+        
+        printf("Temperature is %4.2f C \r\n",temp);
+        printf("Humid is %4.2f  \r\n",humid);
+        printf("Presence: %s  \r\n",room_status);
+        printf("gas: %f\r\n", gas_sensor_value);
+        printf("room safety: %s\r\n",room_safety_status);
+            
     }
-}
\ No newline at end of file
+}
--- a/mbed.bld	Tue Mar 18 23:49:51 2014 +0000
+++ b/mbed.bld	Wed Dec 10 06:43:09 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/869cf507173a
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5
\ No newline at end of file