SMART CLEO Smart Aircon

Committer:
SMART_CLEO
Date:
Wed Dec 06 08:53:58 2017 +0000
Revision:
0:c5ba7cb9a63e
SMART_CLEO

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SMART_CLEO 0:c5ba7cb9a63e 1 #include "mbed.h"
SMART_CLEO 0:c5ba7cb9a63e 2 #include "TextLCD.h"
SMART_CLEO 0:c5ba7cb9a63e 3
SMART_CLEO 0:c5ba7cb9a63e 4 #define DHTLIB_OK 0
SMART_CLEO 0:c5ba7cb9a63e 5 #define DHTLIB_ERROR_CHECKSUM -1
SMART_CLEO 0:c5ba7cb9a63e 6 #define DHTLIB_ERROR_TIMEOUT -2
SMART_CLEO 0:c5ba7cb9a63e 7
SMART_CLEO 0:c5ba7cb9a63e 8 PinName pin_DHT11 = PA_15;
SMART_CLEO 0:c5ba7cb9a63e 9
SMART_CLEO 0:c5ba7cb9a63e 10 // INA, INB
SMART_CLEO 0:c5ba7cb9a63e 11 PinName pin_DC[2] = {PA_6, PA_7};
SMART_CLEO 0:c5ba7cb9a63e 12
SMART_CLEO 0:c5ba7cb9a63e 13 DigitalInOut data_pin(pin_DHT11);
SMART_CLEO 0:c5ba7cb9a63e 14 PwmOut INA(pin_DC[0]);
SMART_CLEO 0:c5ba7cb9a63e 15 PwmOut INB(pin_DC[1]);
SMART_CLEO 0:c5ba7cb9a63e 16
SMART_CLEO 0:c5ba7cb9a63e 17 // rs, rw, e, d0-d3
SMART_CLEO 0:c5ba7cb9a63e 18 TextLCD lcd(PB_12, PB_13, PB_14, PB_15, PA_9, PA_10, PA_11);
SMART_CLEO 0:c5ba7cb9a63e 19
SMART_CLEO 0:c5ba7cb9a63e 20 enum{STOP, CW, CCW};
SMART_CLEO 0:c5ba7cb9a63e 21
SMART_CLEO 0:c5ba7cb9a63e 22 uint8_t Temp_High = 28, Temp_Low = 26, Aircon_flag = 0;
SMART_CLEO 0:c5ba7cb9a63e 23
SMART_CLEO 0:c5ba7cb9a63e 24 Timer tmr;
SMART_CLEO 0:c5ba7cb9a63e 25
SMART_CLEO 0:c5ba7cb9a63e 26 int humi;
SMART_CLEO 0:c5ba7cb9a63e 27 int temp;
SMART_CLEO 0:c5ba7cb9a63e 28
SMART_CLEO 0:c5ba7cb9a63e 29 void DC_Ctrl(uint8_t dir, float Speed);
SMART_CLEO 0:c5ba7cb9a63e 30 int dht_read(void);
SMART_CLEO 0:c5ba7cb9a63e 31
SMART_CLEO 0:c5ba7cb9a63e 32 int main() {
SMART_CLEO 0:c5ba7cb9a63e 33
SMART_CLEO 0:c5ba7cb9a63e 34 lcd.printf("Temp : [C]\n");
SMART_CLEO 0:c5ba7cb9a63e 35 lcd.printf("DC MOTOR : OFF");
SMART_CLEO 0:c5ba7cb9a63e 36
SMART_CLEO 0:c5ba7cb9a63e 37 while(1) {
SMART_CLEO 0:c5ba7cb9a63e 38 if(dht_read() == 0)
SMART_CLEO 0:c5ba7cb9a63e 39 {
SMART_CLEO 0:c5ba7cb9a63e 40 lcd.locate(7, 0);
SMART_CLEO 0:c5ba7cb9a63e 41 lcd.printf("%3d", temp);
SMART_CLEO 0:c5ba7cb9a63e 42 if(Aircon_flag == 0)
SMART_CLEO 0:c5ba7cb9a63e 43 {
SMART_CLEO 0:c5ba7cb9a63e 44 if(Temp_High <= temp)
SMART_CLEO 0:c5ba7cb9a63e 45 {
SMART_CLEO 0:c5ba7cb9a63e 46 DC_Ctrl(CW, 50);
SMART_CLEO 0:c5ba7cb9a63e 47 lcd.locate(11, 1);
SMART_CLEO 0:c5ba7cb9a63e 48 lcd.printf("ON ");
SMART_CLEO 0:c5ba7cb9a63e 49 Aircon_flag = 1;
SMART_CLEO 0:c5ba7cb9a63e 50 }
SMART_CLEO 0:c5ba7cb9a63e 51 }
SMART_CLEO 0:c5ba7cb9a63e 52 else
SMART_CLEO 0:c5ba7cb9a63e 53 {
SMART_CLEO 0:c5ba7cb9a63e 54 if(Temp_Low >= temp)
SMART_CLEO 0:c5ba7cb9a63e 55 {
SMART_CLEO 0:c5ba7cb9a63e 56 DC_Ctrl(STOP, 0);
SMART_CLEO 0:c5ba7cb9a63e 57 lcd.locate(11, 1);
SMART_CLEO 0:c5ba7cb9a63e 58 lcd.printf("OFF");
SMART_CLEO 0:c5ba7cb9a63e 59 Aircon_flag = 0;
SMART_CLEO 0:c5ba7cb9a63e 60 }
SMART_CLEO 0:c5ba7cb9a63e 61 }
SMART_CLEO 0:c5ba7cb9a63e 62 }
SMART_CLEO 0:c5ba7cb9a63e 63 wait(2);
SMART_CLEO 0:c5ba7cb9a63e 64 }
SMART_CLEO 0:c5ba7cb9a63e 65 }
SMART_CLEO 0:c5ba7cb9a63e 66
SMART_CLEO 0:c5ba7cb9a63e 67 void DC_Ctrl(uint8_t dir, float Speed)
SMART_CLEO 0:c5ba7cb9a63e 68 {
SMART_CLEO 0:c5ba7cb9a63e 69 switch(dir)
SMART_CLEO 0:c5ba7cb9a63e 70 {
SMART_CLEO 0:c5ba7cb9a63e 71 case STOP:
SMART_CLEO 0:c5ba7cb9a63e 72 INA = 0;
SMART_CLEO 0:c5ba7cb9a63e 73 INB = 0;
SMART_CLEO 0:c5ba7cb9a63e 74 break;
SMART_CLEO 0:c5ba7cb9a63e 75 case CW:
SMART_CLEO 0:c5ba7cb9a63e 76 INA = Speed/100;
SMART_CLEO 0:c5ba7cb9a63e 77 INB = 0;
SMART_CLEO 0:c5ba7cb9a63e 78 break;
SMART_CLEO 0:c5ba7cb9a63e 79 case CCW:
SMART_CLEO 0:c5ba7cb9a63e 80 INA = 0;
SMART_CLEO 0:c5ba7cb9a63e 81 INB = Speed/100;
SMART_CLEO 0:c5ba7cb9a63e 82 break;
SMART_CLEO 0:c5ba7cb9a63e 83 }
SMART_CLEO 0:c5ba7cb9a63e 84 }
SMART_CLEO 0:c5ba7cb9a63e 85
SMART_CLEO 0:c5ba7cb9a63e 86 int dht_read(void){
SMART_CLEO 0:c5ba7cb9a63e 87
SMART_CLEO 0:c5ba7cb9a63e 88 // BUFFER TO RECEIVE
SMART_CLEO 0:c5ba7cb9a63e 89 uint8_t bits[5];
SMART_CLEO 0:c5ba7cb9a63e 90 uint8_t cnt = 7;
SMART_CLEO 0:c5ba7cb9a63e 91 uint8_t idx = 0;
SMART_CLEO 0:c5ba7cb9a63e 92
SMART_CLEO 0:c5ba7cb9a63e 93 tmr.stop();
SMART_CLEO 0:c5ba7cb9a63e 94 tmr.reset();
SMART_CLEO 0:c5ba7cb9a63e 95
SMART_CLEO 0:c5ba7cb9a63e 96 // EMPTY BUFFER
SMART_CLEO 0:c5ba7cb9a63e 97 for(int i=0; i< 5; i++) bits[i] = 0;
SMART_CLEO 0:c5ba7cb9a63e 98
SMART_CLEO 0:c5ba7cb9a63e 99 // REQUEST SAMPLE
SMART_CLEO 0:c5ba7cb9a63e 100 data_pin.output();
SMART_CLEO 0:c5ba7cb9a63e 101 data_pin.write(0);
SMART_CLEO 0:c5ba7cb9a63e 102 wait_ms(18);
SMART_CLEO 0:c5ba7cb9a63e 103 data_pin.write(1);
SMART_CLEO 0:c5ba7cb9a63e 104 wait_us(10);
SMART_CLEO 0:c5ba7cb9a63e 105 data_pin.input();
SMART_CLEO 0:c5ba7cb9a63e 106 wait_us(40);
SMART_CLEO 0:c5ba7cb9a63e 107
SMART_CLEO 0:c5ba7cb9a63e 108 // ACKNOWLEDGE or TIMEOUT
SMART_CLEO 0:c5ba7cb9a63e 109 unsigned long loopCnt = 10000;
SMART_CLEO 0:c5ba7cb9a63e 110
SMART_CLEO 0:c5ba7cb9a63e 111 while(data_pin.read() == 0)if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
SMART_CLEO 0:c5ba7cb9a63e 112
SMART_CLEO 0:c5ba7cb9a63e 113 loopCnt = 10000;
SMART_CLEO 0:c5ba7cb9a63e 114
SMART_CLEO 0:c5ba7cb9a63e 115 while(data_pin.read() == 1)if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
SMART_CLEO 0:c5ba7cb9a63e 116
SMART_CLEO 0:c5ba7cb9a63e 117 // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
SMART_CLEO 0:c5ba7cb9a63e 118
SMART_CLEO 0:c5ba7cb9a63e 119 for(int i=0; i<40; i++){
SMART_CLEO 0:c5ba7cb9a63e 120
SMART_CLEO 0:c5ba7cb9a63e 121 loopCnt = 10000;
SMART_CLEO 0:c5ba7cb9a63e 122
SMART_CLEO 0:c5ba7cb9a63e 123 while(data_pin.read() == 0)if(loopCnt-- == 0)return DHTLIB_ERROR_TIMEOUT;
SMART_CLEO 0:c5ba7cb9a63e 124
SMART_CLEO 0:c5ba7cb9a63e 125 //unsigned long t = micros();
SMART_CLEO 0:c5ba7cb9a63e 126 tmr.start();
SMART_CLEO 0:c5ba7cb9a63e 127
SMART_CLEO 0:c5ba7cb9a63e 128 loopCnt = 10000;
SMART_CLEO 0:c5ba7cb9a63e 129
SMART_CLEO 0:c5ba7cb9a63e 130 while(data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
SMART_CLEO 0:c5ba7cb9a63e 131
SMART_CLEO 0:c5ba7cb9a63e 132 if(tmr.read_us() > 40) bits[idx] |= (1 << cnt);
SMART_CLEO 0:c5ba7cb9a63e 133
SMART_CLEO 0:c5ba7cb9a63e 134 tmr.stop();
SMART_CLEO 0:c5ba7cb9a63e 135 tmr.reset();
SMART_CLEO 0:c5ba7cb9a63e 136
SMART_CLEO 0:c5ba7cb9a63e 137 if(cnt == 0){ // next byte?
SMART_CLEO 0:c5ba7cb9a63e 138
SMART_CLEO 0:c5ba7cb9a63e 139 cnt = 7; // restart at MSB
SMART_CLEO 0:c5ba7cb9a63e 140 idx++; // next byte!
SMART_CLEO 0:c5ba7cb9a63e 141
SMART_CLEO 0:c5ba7cb9a63e 142 }else cnt--;
SMART_CLEO 0:c5ba7cb9a63e 143
SMART_CLEO 0:c5ba7cb9a63e 144 }
SMART_CLEO 0:c5ba7cb9a63e 145
SMART_CLEO 0:c5ba7cb9a63e 146 // WRITE TO RIGHT VARS
SMART_CLEO 0:c5ba7cb9a63e 147 // as bits[1] and bits[3] are allways zero they are omitted in formulas.
SMART_CLEO 0:c5ba7cb9a63e 148 humi = bits[0];
SMART_CLEO 0:c5ba7cb9a63e 149 temp = bits[2];
SMART_CLEO 0:c5ba7cb9a63e 150
SMART_CLEO 0:c5ba7cb9a63e 151 uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
SMART_CLEO 0:c5ba7cb9a63e 152
SMART_CLEO 0:c5ba7cb9a63e 153 if(bits[4] != sum){return DHTLIB_ERROR_CHECKSUM;}
SMART_CLEO 0:c5ba7cb9a63e 154
SMART_CLEO 0:c5ba7cb9a63e 155 return DHTLIB_OK;
SMART_CLEO 0:c5ba7cb9a63e 156
SMART_CLEO 0:c5ba7cb9a63e 157 }