smart dustbin using hcsr04
Dependencies: HCSR04 TextLCD mbed-rtos mbed
main.cpp@0:4273965739dc, 2017-04-19 (annotated)
- Committer:
- kumarlalit
- Date:
- Wed Apr 19 08:15:32 2017 +0000
- Revision:
- 0:4273965739dc
smart dustbin using sim900a;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kumarlalit | 0:4273965739dc | 1 | #include "hcsr04.h" |
kumarlalit | 0:4273965739dc | 2 | #include "TextLCD.h" |
kumarlalit | 0:4273965739dc | 3 | #include "rtos.h" |
kumarlalit | 0:4273965739dc | 4 | #include <string> |
kumarlalit | 0:4273965739dc | 5 | |
kumarlalit | 0:4273965739dc | 6 | DigitalOut myled(LED1); |
kumarlalit | 0:4273965739dc | 7 | DigitalOut myled1(LED2); |
kumarlalit | 0:4273965739dc | 8 | DigitalOut myled2(LED3); |
kumarlalit | 0:4273965739dc | 9 | Serial pc(USBTX, USBRX); |
kumarlalit | 0:4273965739dc | 10 | //TextLCD lcd(p21,p22,p23,p24,p25,p26); |
kumarlalit | 0:4273965739dc | 11 | HCSR04 usensor(p15,p16); |
kumarlalit | 0:4273965739dc | 12 | HCSR04 usensor1(p21,p22); |
kumarlalit | 0:4273965739dc | 13 | HCSR04 usensor2(p23,p24); |
kumarlalit | 0:4273965739dc | 14 | |
kumarlalit | 0:4273965739dc | 15 | Serial SIM900(p13, p14); //tx, rx SIM 900 |
kumarlalit | 0:4273965739dc | 16 | string result; |
kumarlalit | 0:4273965739dc | 17 | char x; |
kumarlalit | 0:4273965739dc | 18 | Thread thread; |
kumarlalit | 0:4273965739dc | 19 | unsigned int dist; |
kumarlalit | 0:4273965739dc | 20 | unsigned int dist1; |
kumarlalit | 0:4273965739dc | 21 | unsigned int dist2; |
kumarlalit | 0:4273965739dc | 22 | |
kumarlalit | 0:4273965739dc | 23 | void clearString() |
kumarlalit | 0:4273965739dc | 24 | { |
kumarlalit | 0:4273965739dc | 25 | result.clear(); |
kumarlalit | 0:4273965739dc | 26 | } |
kumarlalit | 0:4273965739dc | 27 | |
kumarlalit | 0:4273965739dc | 28 | void callback_rx() { |
kumarlalit | 0:4273965739dc | 29 | |
kumarlalit | 0:4273965739dc | 30 | while (SIM900.readable()) { |
kumarlalit | 0:4273965739dc | 31 | x = SIM900.getc(); |
kumarlalit | 0:4273965739dc | 32 | result += x; |
kumarlalit | 0:4273965739dc | 33 | pc.putc(x); // print the answer from SIM900 |
kumarlalit | 0:4273965739dc | 34 | |
kumarlalit | 0:4273965739dc | 35 | } |
kumarlalit | 0:4273965739dc | 36 | } |
kumarlalit | 0:4273965739dc | 37 | |
kumarlalit | 0:4273965739dc | 38 | void sendSMS1() |
kumarlalit | 0:4273965739dc | 39 | { |
kumarlalit | 0:4273965739dc | 40 | |
kumarlalit | 0:4273965739dc | 41 | clearString(); |
kumarlalit | 0:4273965739dc | 42 | SIM900.printf("AT+CMGF=1\r\n"); //at command for send sms |
kumarlalit | 0:4273965739dc | 43 | wait_ms(1000); |
kumarlalit | 0:4273965739dc | 44 | clearString(); |
kumarlalit | 0:4273965739dc | 45 | wait_ms(1000); |
kumarlalit | 0:4273965739dc | 46 | SIM900.printf("AT+CMGS="); |
kumarlalit | 0:4273965739dc | 47 | SIM900.putc('"'); |
kumarlalit | 0:4273965739dc | 48 | SIM900.printf("+919405204727"); |
kumarlalit | 0:4273965739dc | 49 | SIM900.putc('"'); |
kumarlalit | 0:4273965739dc | 50 | SIM900.printf("\r"); |
kumarlalit | 0:4273965739dc | 51 | SIM900.printf("\n"); |
kumarlalit | 0:4273965739dc | 52 | wait_ms(1000); |
kumarlalit | 0:4273965739dc | 53 | SIM900.printf("Bin 1 is full"); |
kumarlalit | 0:4273965739dc | 54 | wait_ms(1000); |
kumarlalit | 0:4273965739dc | 55 | SIM900.putc(0x1A); |
kumarlalit | 0:4273965739dc | 56 | wait_ms(5000); |
kumarlalit | 0:4273965739dc | 57 | } |
kumarlalit | 0:4273965739dc | 58 | |
kumarlalit | 0:4273965739dc | 59 | void sendSMS2() |
kumarlalit | 0:4273965739dc | 60 | { |
kumarlalit | 0:4273965739dc | 61 | |
kumarlalit | 0:4273965739dc | 62 | clearString(); |
kumarlalit | 0:4273965739dc | 63 | SIM900.printf("AT+CMGF=1\r\n"); //at command for send sms |
kumarlalit | 0:4273965739dc | 64 | wait_ms(1000); |
kumarlalit | 0:4273965739dc | 65 | clearString(); |
kumarlalit | 0:4273965739dc | 66 | wait_ms(1000); |
kumarlalit | 0:4273965739dc | 67 | SIM900.printf("AT+CMGS="); |
kumarlalit | 0:4273965739dc | 68 | SIM900.putc('"'); |
kumarlalit | 0:4273965739dc | 69 | SIM900.printf("+919405204727"); |
kumarlalit | 0:4273965739dc | 70 | SIM900.putc('"'); |
kumarlalit | 0:4273965739dc | 71 | SIM900.printf("\r"); |
kumarlalit | 0:4273965739dc | 72 | SIM900.printf("\n"); |
kumarlalit | 0:4273965739dc | 73 | wait_ms(1000); |
kumarlalit | 0:4273965739dc | 74 | SIM900.printf("Bin 2 is full"); |
kumarlalit | 0:4273965739dc | 75 | wait_ms(1000); |
kumarlalit | 0:4273965739dc | 76 | SIM900.putc(0x1A); |
kumarlalit | 0:4273965739dc | 77 | wait_ms(5000); |
kumarlalit | 0:4273965739dc | 78 | } |
kumarlalit | 0:4273965739dc | 79 | |
kumarlalit | 0:4273965739dc | 80 | void sendSMS3() |
kumarlalit | 0:4273965739dc | 81 | { |
kumarlalit | 0:4273965739dc | 82 | |
kumarlalit | 0:4273965739dc | 83 | clearString(); |
kumarlalit | 0:4273965739dc | 84 | SIM900.printf("AT+CMGF=1\r\n"); //at command for send sms |
kumarlalit | 0:4273965739dc | 85 | wait_ms(1000); |
kumarlalit | 0:4273965739dc | 86 | clearString(); |
kumarlalit | 0:4273965739dc | 87 | wait_ms(1000); |
kumarlalit | 0:4273965739dc | 88 | SIM900.printf("AT+CMGS="); |
kumarlalit | 0:4273965739dc | 89 | SIM900.putc('"'); |
kumarlalit | 0:4273965739dc | 90 | SIM900.printf("+919405204727"); |
kumarlalit | 0:4273965739dc | 91 | SIM900.putc('"'); |
kumarlalit | 0:4273965739dc | 92 | SIM900.printf("\r"); |
kumarlalit | 0:4273965739dc | 93 | SIM900.printf("\n"); |
kumarlalit | 0:4273965739dc | 94 | wait_ms(1000); |
kumarlalit | 0:4273965739dc | 95 | SIM900.printf("Bin 3 is full"); |
kumarlalit | 0:4273965739dc | 96 | wait_ms(1000); |
kumarlalit | 0:4273965739dc | 97 | SIM900.putc(0x1A); |
kumarlalit | 0:4273965739dc | 98 | wait_ms(5000); |
kumarlalit | 0:4273965739dc | 99 | } |
kumarlalit | 0:4273965739dc | 100 | |
kumarlalit | 0:4273965739dc | 101 | |
kumarlalit | 0:4273965739dc | 102 | void led1_thread() { |
kumarlalit | 0:4273965739dc | 103 | while (true) { |
kumarlalit | 0:4273965739dc | 104 | wait_ms(10); |
kumarlalit | 0:4273965739dc | 105 | sendSMS1(); // SEND SMS |
kumarlalit | 0:4273965739dc | 106 | wait_ms(10); |
kumarlalit | 0:4273965739dc | 107 | |
kumarlalit | 0:4273965739dc | 108 | |
kumarlalit | 0:4273965739dc | 109 | Thread::wait(1000); |
kumarlalit | 0:4273965739dc | 110 | } |
kumarlalit | 0:4273965739dc | 111 | } |
kumarlalit | 0:4273965739dc | 112 | |
kumarlalit | 0:4273965739dc | 113 | void led2_thread() { |
kumarlalit | 0:4273965739dc | 114 | while (true) { |
kumarlalit | 0:4273965739dc | 115 | wait_ms(10); |
kumarlalit | 0:4273965739dc | 116 | sendSMS2(); // SEND SMS |
kumarlalit | 0:4273965739dc | 117 | wait_ms(100); |
kumarlalit | 0:4273965739dc | 118 | |
kumarlalit | 0:4273965739dc | 119 | |
kumarlalit | 0:4273965739dc | 120 | Thread::wait(1000); |
kumarlalit | 0:4273965739dc | 121 | } |
kumarlalit | 0:4273965739dc | 122 | } |
kumarlalit | 0:4273965739dc | 123 | void led3_thread() { |
kumarlalit | 0:4273965739dc | 124 | while (true) { |
kumarlalit | 0:4273965739dc | 125 | wait_ms(10); |
kumarlalit | 0:4273965739dc | 126 | sendSMS3(); // SEND SMS |
kumarlalit | 0:4273965739dc | 127 | wait_ms(100); |
kumarlalit | 0:4273965739dc | 128 | |
kumarlalit | 0:4273965739dc | 129 | Thread::wait(1000); |
kumarlalit | 0:4273965739dc | 130 | } |
kumarlalit | 0:4273965739dc | 131 | } |
kumarlalit | 0:4273965739dc | 132 | |
kumarlalit | 0:4273965739dc | 133 | |
kumarlalit | 0:4273965739dc | 134 | int main() |
kumarlalit | 0:4273965739dc | 135 | { |
kumarlalit | 0:4273965739dc | 136 | SIM900.attach(&callback_rx); |
kumarlalit | 0:4273965739dc | 137 | SIM900.baud(9600); // |
kumarlalit | 0:4273965739dc | 138 | wait_ms(100); |
kumarlalit | 0:4273965739dc | 139 | |
kumarlalit | 0:4273965739dc | 140 | while(1) |
kumarlalit | 0:4273965739dc | 141 | { |
kumarlalit | 0:4273965739dc | 142 | usensor.start(); |
kumarlalit | 0:4273965739dc | 143 | usensor1.start(); |
kumarlalit | 0:4273965739dc | 144 | usensor2.start(); |
kumarlalit | 0:4273965739dc | 145 | wait_ms(500); |
kumarlalit | 0:4273965739dc | 146 | dist=usensor.get_dist_cm(); |
kumarlalit | 0:4273965739dc | 147 | dist1=usensor1.get_dist_cm(); |
kumarlalit | 0:4273965739dc | 148 | dist2=usensor2.get_dist_cm(); |
kumarlalit | 0:4273965739dc | 149 | // lcd.locate(0,0); |
kumarlalit | 0:4273965739dc | 150 | // lcd.printf("distance"); |
kumarlalit | 0:4273965739dc | 151 | if(dist<20) |
kumarlalit | 0:4273965739dc | 152 | { |
kumarlalit | 0:4273965739dc | 153 | myled=1; |
kumarlalit | 0:4273965739dc | 154 | thread.start(led1_thread); |
kumarlalit | 0:4273965739dc | 155 | // lcd.locate(0,0); |
kumarlalit | 0:4273965739dc | 156 | // lcd.printf("dist=cm:%ld\r\n",dist ); |
kumarlalit | 0:4273965739dc | 157 | } |
kumarlalit | 0:4273965739dc | 158 | else |
kumarlalit | 0:4273965739dc | 159 | { |
kumarlalit | 0:4273965739dc | 160 | myled=0; |
kumarlalit | 0:4273965739dc | 161 | // lcd.printf("abnormal"); |
kumarlalit | 0:4273965739dc | 162 | } |
kumarlalit | 0:4273965739dc | 163 | |
kumarlalit | 0:4273965739dc | 164 | if(dist1<20) |
kumarlalit | 0:4273965739dc | 165 | { |
kumarlalit | 0:4273965739dc | 166 | myled1=1; |
kumarlalit | 0:4273965739dc | 167 | thread.start(led2_thread); |
kumarlalit | 0:4273965739dc | 168 | //lcd.locate(0,0); |
kumarlalit | 0:4273965739dc | 169 | //lcd.printf("dist=cm:%ld\r\n",dist ); |
kumarlalit | 0:4273965739dc | 170 | } |
kumarlalit | 0:4273965739dc | 171 | else |
kumarlalit | 0:4273965739dc | 172 | { |
kumarlalit | 0:4273965739dc | 173 | myled1=0; |
kumarlalit | 0:4273965739dc | 174 | |
kumarlalit | 0:4273965739dc | 175 | } |
kumarlalit | 0:4273965739dc | 176 | |
kumarlalit | 0:4273965739dc | 177 | if(dist2<20) |
kumarlalit | 0:4273965739dc | 178 | { |
kumarlalit | 0:4273965739dc | 179 | myled2=1; |
kumarlalit | 0:4273965739dc | 180 | thread.start(led3_thread); |
kumarlalit | 0:4273965739dc | 181 | //lcd.locate(0,0); |
kumarlalit | 0:4273965739dc | 182 | //lcd.printf("dist=cm:%ld\r\n",dist ); |
kumarlalit | 0:4273965739dc | 183 | } |
kumarlalit | 0:4273965739dc | 184 | else |
kumarlalit | 0:4273965739dc | 185 | { |
kumarlalit | 0:4273965739dc | 186 | myled2=0; |
kumarlalit | 0:4273965739dc | 187 | //lcd.printf("abnormal"); |
kumarlalit | 0:4273965739dc | 188 | } |
kumarlalit | 0:4273965739dc | 189 | } |
kumarlalit | 0:4273965739dc | 190 | |
kumarlalit | 0:4273965739dc | 191 | |
kumarlalit | 0:4273965739dc | 192 | |
kumarlalit | 0:4273965739dc | 193 | |
kumarlalit | 0:4273965739dc | 194 | } |